我正在尝试绘制此代码的图形,但出现此错误

时间:2020-02-24 12:19:25

标签: python pandas numpy plot scikit-learn

我正在尝试绘制SVM分类器的图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap
from sklearn.svm import SVC
df = pd.read_csv(r'C:\Users\Home\Desktop\result.csv')
x = df.iloc[:, 1:-1].values
y = df.iloc[:, 20].values
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.2)
scx = StandardScaler()
x_train = scx.fit_transform(x_train)
x_test = scx.transform(x_test)
classifier = SVC(kernel='linear')
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)

这是情节代码:

 x_set, y_set = x_train, y_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))
plt.contour(x1, x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape), alpha=0.75,
            cmap=ListedColormap(('red', 'green')))
plt.xlim(x1.min(), x1.max())
plt.ylim(x2.min(), x2.max())
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(('red', 'green'))(i), label=j)
    plt.title('SVM (training set)')
    plt.legend()
    plt.show()

,我遇到了这个错误: ValueError: X.shape[1] = 2 should be equal to 19, the number of features at training time

要删除此错误,我应该编辑什么?

0 个答案:

没有答案