sklearn分類算法的評價指標調用
#二分類問題的算法評價指標
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets
d=datasets.load_digits()
x=d.data
y=d.target.copy()
print(len(y))
y[d.target==9]=1
y[d.target!=9]=0
print(y)
print(pd.value_counts(y))
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=666)
from sklearn.linear_model import LogisticRegression
log_reg=LogisticRegression(solver="newton-cg")
log_reg.fit(x_train,y_train)
print(log_reg.score(x_test,y_test))
y_pre=log_reg.predict(x_test)
def TN(y_true,y_pre):
return np.sum((y_true==0) & (y_pre==0))
def FP(y_true,y_pre):
return np.sum((y_true==0) & (y_pre==1))
def FN(y_true,y_pre):
return np.sum((y_true==1) & (y_pre==0))
def TP(y_true,y_pre):
return np.sum((y_true==1) & (y_pre==1))
print(TN(y_test,y_pre))
print(FP(y_test,y_pre))
print(FN(y_test,y_pre))
print(TP(y_test,y_pre))
def confusion_matrix(y_true,y_pre):
return np.array([
[TN(y_true,y_pre),FP(y_true,y_pre)],
[FN(y_true,y_pre),TP(y_true,y_pre)]
])
print(confusion_matrix(y_test,y_pre))
def precision(y_true,y_pre):
try:
return TP(y_true,y_pre)/(FP(y_true,y_pre)+TP(y_true,y_pre))
except:
return 0.0
def recall(y_true,y_pre):
try:
return TP(y_true,y_pre)/(FN(y_true,y_pre)+TP(y_true,y_pre))
except:
return 0.0
print(precision(y_test,y_pre))
print(recall(y_test,y_pre))
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
print((confusion_matrix(y_test,y_pre)))
print(precision_score(y_test,y_pre))
print(recall_score(y_test,y_pre))
print(log_reg.score(x_test,y_test))
def F1(pre,rec):
try:
return (2*pre*rec)/(pre+rec)
except:
return 0.0
print(F1(precision(y_test,y_pre),recall(y_test,y_pre)))
print(F1(0.1,0.9))
print(F1(0,1))
from sklearn.metrics import f1_score
print(f1_score(y_test,y_pre))
print(log_reg.decision_function(x_test))
#改變閾值,可以改變機器學習的召回率和精准率
decision_scores=log_reg.decision_function(x_test)
y_pre2=np.array(decision_scores>=5,dtype="int")
print(precision(y_test,y_pre2))
print(recall(y_test,y_pre2))
print(confusion_matrix(y_test,y_pre2))
y_pre3=np.array(decision_scores>=-5,dtype="int")
print(precision(y_test,y_pre3))
print(recall(y_test,y_pre3))
print(confusion_matrix(y_test,y_pre3))
print(y_pre3)
#繪制出決策邊界閾值與精准率和召回率的變化曲線
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
thresholds=np.arange(np.min(decision_scores),np.max(decision_scores),0.1)
pre=[]
rec=[]
for threshold in thresholds:
y_pre11=np.array(decision_scores>threshold,dtype="int")
pre.append(precision_score(y_test,y_pre11))
rec.append(recall_score(y_test,y_pre11))
plt.figure()
plt.plot(thresholds,pre,"r",thresholds,rec,"g")
plt.show()
#輸出精確率和召回率曲線
plt.plot(pre,rec,"g",linewidth=5)
plt.show()
#直接在sklearn中調用精准率召回率曲線直接輸出相應的精准率變化和召回率變化以及決策閾值
from sklearn.metrics import precision_recall_curve
decision_scores=log_reg.decision_function(x_test)
pre1,rec1,thre1=precision_recall_curve(y_test,decision_scores)
print(rec1.shape)
print(pre1.shape)
print(thre1.shape)
plt.figure()
plt.plot(thre1,pre1[:-1],"r")
plt.plot(thre1,rec1[:-1],"g")
plt.show()
plt.plot(pre1,rec1)
plt.show()
#sklearn中調用ROC(TPR與FPR曲線)
from sklearn.metrics import roc_curve
decision_scores=log_reg.decision_function(x_test)
fpr,tpr,thre2=roc_curve(y_test,decision_scores)
plt.plot(fpr,tpr,"r")
plt.show() #曲線和x軸所圍成的面積越大則性能越好一點
from sklearn.metrics import roc_auc_score
print(roc_auc_score(y_test,decision_scores)) #輸出ROC與x軸圍成的面積大小roc_auc
#多分類問題下的各個評判指標應用
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets
d=datasets.load_digits()
x=d.data
y=d.target
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=666)
from sklearn.linear_model import LogisticRegression
log1=LogisticRegression()
log1.fit(x_train,y_train)
print(log1.score(x_test,y_test))
y_p=log1.predict(x_test)
from sklearn.metrics import precision_score
print(precision_score(y_test,y_p,average="micro")) #輸出精准率的大小(需要設定average參數)
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test,y_p)) #輸出混淆矩陣
#繪制混淆矩陣通過灰度圖的方法可以看出各個行列元素的相對大小
c=confusion_matrix(y_test,y_p)
plt.matshow(c,cmap=plt.cm.gray)
plt.show()
row_sum=np.sum(c,axis=1)
erro_matrix=c/row_sum
np.fill_diagonal(erro_matrix,0) #將對角線的值填充為0
print(erro_matrix)
plt.matshow(erro_matrix,cmap=plt.cm.gray) #輸出多元分類結果時所輸出的錯誤結果
plt.show()
