Linear SVC Machine learning SVM example with Python
The objective of a Linear SVC (Support Vector Classifier) isto obtain a "best fit" hyperplane to categorizes the data. After getting the hyperplane, the classifier can predict the label (or class,類別) based on the input features. A simple supervised learning example working with Linear SVC isdescribedin this article (Fig. 01).
Import packages
Numpy is for array conversion.
Maitipzlotlib is for data visualization that can show how linear SVC works.
Define features
Considering the data points with (x,y) coordinate values of (1,2), (5,8), (1.5,1.8) (8,8), (1,0.6) and (9,11). These data has two dimension feature, i.e., x and y coordinate values.
Therefore, these data serves as two-feature data example, and they can be expressed as.
Graph the data![](../assets/Graph the data.png)
Fig. 02 The distribution of data features
From Fig. 02, two groups can be easily divided. But, to draw the exact dividing line need further calculation.
Compile an array
The array formats are required when running the machine learning algorithm. Thus, the features (x and y) are stored in an array of two elements. (A variable)
In the supervised learning, data sets are labeled (or classed) for training purposes. The labeling rule is: 0 are assigned to lower feature pairs and 1 to the higher ones. (L variable)
Define the classifier
The SVC in SVM (support vector machine) module is used. The kernel is defined as linear and C, a valuation value, is defined as 1.0.
The classifier does learning with “clf.fit(A, L)”.
Two features derive a 2D graph. Thus, the problem occurs when there are thousands of more features.
Draw the exact dividing line (Fig. 03)![](../assets/Draw the exact dividing line1.png)
![](../assets/Draw the exact dividing line2.png)
Fig. 03 The distribution of data features and the dividing line
The learned classifier can be test as follows:
Fig. 04 The distribution of the test points (the blue and red points)
Environment: KaggleFig. 04 The distribution of the test points (the blue and red points)
REF:
[0] Main contents
https://pythonprogramming.net/linear-svc-example-scikit-learn-svm-python/
[1] Errors: “Expected 2D array got 1D array instead”