Search code examples
pythonmachine-learningscikit-learnpreprocessortrain-test-split

How to print the classified points based on SVM classifier


I was using "svm" classifier to classify it was a bike or car. So, my features were 0,1,2 columns and dependents was 3rd column.I can able to clearly see the classification,but i don't know how to print all the points based on classification in diagram.

    import numpy as np

    import operator
    from matplotlib import pyplot as plt
    from sklearn import svm
    from matplotlib.colors import ListedColormap
    from sklearn.model_selection import train_test_split
    from sklearn import preprocessing
    from sklearn.svm import SVC
    dataframe=pd.read_csv(DATASET_PATH)
    dataframe = dataframe.dropna(how='any',axis=0)
    SVM_Trained_Model = preprocessing.LabelEncoder()

    train_data=dataframe[0:len(dataframe)]
    le=preprocessing.LabelEncoder()
    col=dataframe.columns[START_TRAIN_COLUMN:].astype('U') 
    col_name=["no_of_wheels","dimensions","windows","vehicle_type"]
    for i in range(0,len(col_name)):
     self.train_data[col_name[i]]=le.fit_transform(self.train_data[col_name[i]])
    train_column=np.array(train_data[col]).astype('U')

    data=train_data.iloc[:,[0,1,2]].values

    target=train_data.iloc[:,3].values

    data_train, data_test, target_train, target_test = train_test_split(data,target, test_size = 0.30, 
    random_state = 0) `split test and test train`

    svc_model=SVC(kernel='rbf', probability=True))'classifier model'

    svc_model.fit(data_train, target_train)

    all_labels =svc_model.predict(data_test)

    X_set, y_set = data_train, target_train

    X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 
    0.01),np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))

    Xpred = np.array([X1.ravel(), X2.ravel()] + [np.repeat(0, X1.ravel().size) for _ in range(1)]).T

    pred = svc_model.predict(Xpred).reshape(X1.shape)

    plt.contourf(X1, X2, pred,alpha = 0.75, cmap = ListedColormap(('white','orange','pink')))

    plt.xlim(X1.min(),X1.max())

    plt.ylim(X2.min(), X2.max())


    colors=['red','yellow','cyan','blue']
    for i, j in enumerate(np.unique(y_set)):
       plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],c = ListedColormap((colors[i]))(i), label 
       = j)

    plt.title('Multiclass Classifier ')
    plt.xlabel('Features')
    plt.ylabel('Dependents')
    plt.legend()
    plt.show()

Image

So here is my diagram I need to print the points using python print() based on pink and white region in the diagram.Please help me to get this points.


Solution

  • You need to select and use only 2 features in order to make a 2D surface plot.

    from sklearn.svm import SVC
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import svm, datasets
    
    iris = datasets.load_iris()
    X = iris.data[:, :2]  # we only take the first two features.
    y = iris.target
    
    def make_meshgrid(x, y, h=.02):
        x_min, x_max = x.min() - 1, x.max() + 1
        y_min, y_max = y.min() - 1, y.max() + 1
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
        return xx, yy
    
    def plot_contours(ax, clf, xx, yy, **params):
        Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)
        out = ax.contourf(xx, yy, Z, **params)
        return out
    
    model = svm.SVC(kernel='linear')
    clf = model.fit(X, y)
    
    fig, ax = plt.subplots()
    # title for the plots
    title = ('Decision surface of linear SVC ')
    # Set-up grid for plotting.
    X0, X1 = X[:, 0], X[:, 1]
    xx, yy = make_meshgrid(X0, X1)
    
    plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
    ax.set_ylabel('y label here')
    ax.set_xlabel('x label here')
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)
    ax.legend()
    plt.show()
    

    enter image description here