import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score , recall_score, f1_score
file = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data",
header = None)
df = file
X = df.loc[:,2:].values
y = df.loc[:,1].values
le = LabelEncoder()
y = le.fit_transform(y)
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.20,random_state = 1)
pipe_svc = Pipeline([('scl',StandardScaler()),('clf',SVC(random_state=1))])
pipe_svc.fit(X_train,y_train)
Pipeline(memory=None,
steps=[('scl', StandardScaler(copy=True, with_mean=True, with_std=True)), ('clf', SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=1, shrinking=True,
tol=0.001, verbose=False))])
y_pred = pipe_svc.predict(X_test)
confmat = confusion_matrix(y_true = y_test,y_pred = y_pred)
confmat
array([[71, 1],
[ 2, 40]], dtype=int64)
fig,ax = plt.subplots(figsize = (2.5,2.5))
ax.matshow(confmat,cmap=plt.cm.Blues,alpha = 0.3)
for i in range(confmat.shape[0]):
for j in range(confmat.shape[1]):
ax.text(x = j,y = i,s = confmat[i,j],va = "center",ha = "center")
plt.xlabel('predicted label')
plt.ylabel('true label')
Text(0,0.5,'true label')
![png](https://i-blog.csdnimg.cn/blog_migrate/fca4ed75119fe131081e556c50039016.png)
print('precision:%.3f'%precision_score(y_true = y_test,y_pred = y_pred))
print('recall:%.3f'%recall_score(y_true = y_test,y_pred = y_pred))
print('F1:%.3f'%f1_score(y_true = y_test,y_pred = y_pred))
precision:0.976
recall:0.952
F1:0.964