Vis*_*ish 2 python machine-learning matplotlib python-3.x
我试图通过使用 scikit-learn 的 SVM 文档分类器来预测肺癌数据,我使用以下代码但出现一些错误。我已用于matplotlib.pyplot as plt数据绘图但出现错误。
在这里,我明智地使用了肺癌数据风险因素。
输入文件
GENDER AGE SMOKING YELLOW_FINGERS ANXIETY PEER_PRESSURE CHRONIC DISEASE FATIGUE ALLERGY WHEEZING ALCOHOL CONSUMING COUGHING SHORTNESS OF BREATH SWALLOWING DIFFICULTY CHEST PAIN LUNG_CANCER
F 59 0 0 0 1 0 1 0 1 0 1 1 0 1 0
F 63 0 1 0 0 0 0 0 1 0 1 1 0 0 0
F 75 0 1 0 0 1 1 1 1 0 1 1 0 0 1
M 69 0 1 1 0 0 1 0 1 1 1 1 1 1 1
M 74 1 0 0 0 1 1 1 0 0 0 1 1 1 1
M 63 1 1 1 0 0 0 0 0 1 0 0 1 1 0
Run Code Online (Sandbox Code Playgroud)
脚本支持向量机
# Support Vector Machine (SVM)
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('C:/Users/Vishnu/Desktop/Lung Cancer/lung_cancer.csv')
X = dataset.iloc[:, [2,3,4,5,6,7,8,9,10,11,12,13,14]].values
y = dataset.iloc[:, 15].values
# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Fitting SVM to the Training set
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
# Visualising the Training set results
from matplotlib.colors import ListedColormap
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.contourf(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.xlabel('Age')
plt.ylabel('Lung Cancer Risk Factor')
plt.legend()
plt.show()
# Visualising the Test set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
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.contourf(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 (Test set)')
plt.xlabel('Age')
plt.ylabel('Lung Cancer Risk Factor')
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)
错误
ValueError: X.shape[1] = 2 should be equal to 13, the number of features at training time
Run Code Online (Sandbox Code Playgroud)
在这就像我收到错误
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
Run Code Online (Sandbox Code Playgroud)
为什么我收到错误,请给我建议。提前谢谢您。
编辑_1
SVM 测试集输出图

SVM 训练集输出图

有谁可以告诉我吗?这是正确的输出吗?
提前致谢
不管例外情况如何,我认为有几个方面需要解决。
异常本身是因为您只提供 2 个变量作为输入,classifier.predict而您的模型是在 13 个变量上进行训练的。如果您想在其中 2 个变量上绘制等高线,则必须将其他 11 个变量设置为某个默认值。
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(11)]).T
# Xpred now has a grid for x1 and x2 and average value (0) for x3 through x13
pred = classifier.predict(Xpred).reshape(X1.shape) # is a matrix of 0's and 1's !
plt.contourf(X1, X2, pred,
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
Run Code Online (Sandbox Code Playgroud)
这个片段可以工作,但是它可能不会给你你想要的。使用一些随机二项式数据,您会得到如下所示的数字红绿图。的输出SVC.predict是二进制矩阵,而不是概率。

您可以将其绘制decision_function为预测结果,该结果可视化到分离超平面的距离。这可以被解释为一个风险因素。然而这并不是一个概率
pred = classifier.decision_function(Xpred).reshape(X1.shape)
plt.contourf(X1, X2, pred,
alpha=1.0, cmap="RdYlGn", levels=np.linspace(pred.min(), pred.max(), 100))
Run Code Online (Sandbox Code Playgroud)

我发现您的数据集存在另一个问题。好像有15列。然后我预计该行y = dataset.iloc[:, 15].values会提出IndexError. 如果没有,请检查数据集的完整性。是否正确导入pd.read_csv?
另外,您还丢弃了前两列的信息:性别和年龄。对于性别,您可以转换F为0和Mto1例如,还可以将年龄包括在内X:
dataset = pd.read_csv('C:/Users/Vishnu/Desktop/Lung Cancer/lung_cancer.csv')
dataset.loc[dataset['GENDER'] == 'F', 'GENDER'] = 0
dataset.loc[dataset['GENDER'] == 'M', 'GENDER'] = 1
X = dataset.iloc[:, 0:14].values
y = dataset.iloc[:, 14].values
Run Code Online (Sandbox Code Playgroud)我希望这有帮助。如果在研究您想要的解决方案时出现另一个问题,并且您无法通过自己的研究找到答案,请随时询问:)
编辑
解决关于散点图正确性的第二个问题:我不知道你是如何制作这个图的,但是使用你的散点图代码,绘制在决策函数之上,我得到以下结果(带有肺癌数据)你提供的)

y是一个二元变量。这就是为什么np.unique(y_set)与 相同[0, 1]。我不知道如何使用此代码获得柱状数据点结构。很抱歉,我什至不知道您实际上想通过此图实现什么目的,所以我无法判断它是否显示您想要显示的内容。