日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python-ML】SKlearn库性能指标-混淆矩阵和F1

發布時間:2025/4/16 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python-ML】SKlearn库性能指标-混淆矩阵和F1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# -*- coding: utf-8 -*- ''' Created on 2018年1月19日 @author: Jason.F @summary: 混淆矩陣:TP、TN、FP、FP、FN F1:召回率和爭取率 ''' import pandas as pd 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 import numpy as np from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt from sklearn.metrics import precision_score, recall_score, f1_score #導入數據 df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data',header=None) X=df.loc[:,2:].values y=df.loc[:,1].values le=LabelEncoder() y=le.fit_transform(y)#類標整數化 print (le.transform(['M','B'])) #劃分訓練集合測試集 X_train,X_test,y_train,y_test = train_test_split (X,y,test_size=0.20,random_state=1) #建立pipeline pipe_svc=Pipeline([('scl',StandardScaler()),('clf',SVC(random_state=1))]) pipe_svc.fit(X_train,y_train) y_pred=pipe_svc.predict(X_test) #混淆矩陣并可視化 confmat= confusion_matrix(y_true=y_test,y_pred=y_pred)#輸出混淆矩陣 print (confmat) 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') plt.show() #召回率、準確率、F1 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))#參考:http://scikit-learn.org/stable/modules/model_evaluation.html

結果:

[1 0] [[71 1][ 2 40]] precision:0.976 recall:0.952 F1:0.964

總結

以上是生活随笔為你收集整理的【Python-ML】SKlearn库性能指标-混淆矩阵和F1的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。