如何根据SVM分类器打印分类点

我使用“svm”分类器来区分是自行车还是汽车。因此,我的特征是第0、1、2列,依赖变量是第3列。我能够清楚地看到分类结果,但不知道如何根据分类在图表中打印所有点。

    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()

图片

这是我的图表,我需要使用Python的print()函数根据图表中的粉红色和白色区域打印点。请帮助我获取这些点。


回答:

你需要选择并使用两个特征来制作一个2D表面图。

from sklearn.svm import SVCimport numpy as npimport matplotlib.pyplot as pltfrom sklearn import svm, datasetsiris = datasets.load_iris()X = iris.data[:, :2]  # we only take the first two features.y = iris.targetdef 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, yydef 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 outmodel = svm.SVC(kernel='linear')clf = model.fit(X, y)fig, ax = plt.subplots()# title for the plotstitle = ('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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注