1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
from sklearn import datasets import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn import svm import numpy as np #Load dataset cancer = datasets.load_breast_cancer() # Split dataset into training set and test set X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, test_size=0.3,random_state=109) # 70% training and 30% test h = .02 # mesh step C = 1.0 # Regularisation clf = svm.SVC(kernel='linear', C=C).fit(X_train[:,:2], y_train) # Linear Kernel x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1 y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8) scat=plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train) legend1 = plt.legend(*scat.legend_elements(), loc="upper right", title="diagnostic") plt.xlabel('mean_radius') plt.ylabel('mean_texture') plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show()
|