import warnings
warnings.filterwarnings(
'ignore'
)import matplotlib.pyplot as plt
plt.rcparams[
'font.sans-serif']=
['simhei'
]# 顯示中文
plt.rcparams[
'axes.unicode_minus']=
false
# 畫圖中顯示負號
from sklearn.linear_model import logisticregression
from sklearn import metrics
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
# 讀取資料
breast_cancer = load_breast_cancer(
)x = breast_cancer.data
y = breast_cancer.target
# 建立分類模型
model = logisticregression(c=
10, max_iter=
100)
# c 正則化λ的倒數
trainx, testx, trainy, testy = train_test_split(x, y, test_size=
0.4, random_state=
12345
)# 8.使用訓練集資料進行模型訓練,對測試集資料進行**,列印混淆矩陣。(5分)
model.fit(trainx, trainy)
# 對訓練集進行訓練
# 模型**
prey = model.predict(testx)
# **的類標籤----0或者1
preproba = model.predict_proba(testx)
# preproba包含樣本為0的概率和為1的概率
print
("preproba=\n"
, preproba)
# 列印混淆矩陣
print
(metrics.confusion_matrix(testy, prey)
)# 9.列印精確率、召回率、f1分數和auc值。(5分)
print
('---------精確率---------------'
)print
(metrics.precision_score(testy, prey)
)print
('---------召回率---------------'
)print
(metrics.recall_score(testy, prey)
)print
('---------f1分值---------------'
)print
(metrics.f1_score(testy, prey)
)print
('---------auc值---------------'
)print
(metrics.roc_auc_score(testy, preproba[:,
1]))
# 提取preproba的第二列:正樣本的概率,roc_auc:roc曲線下的面積
# 10.畫出roc曲線。(5分)
plt.subplot(
121)
fpr, tpr, threshold = metrics.roc_curve(testy, preproba[:,
1])# 同上,roc_curve:roc曲線
plt.xlabel(
'fpr'
)plt.ylabel(
'tpr'
)plt.title(
'roc'
)plt.plot(fpr, tpr)
# 11.畫出pr曲線
plt.subplot(
122)
p, r, th = metrics.precision_recall_curve(testy, preproba[:,
1])plt.xlabel(
'recall'
)plt.ylabel(
'precision'
)plt.title(
'pr'
)plt.plot(r, p)
plt.show(
)'''
fpr, tpr, thresholds = roc_curve(y_test, scores)
其中y_test為測試集的結果,scores為模型**的測試集得分(注意:通過decision_function(x_test)計算scores的值);
fpr,tpr,thresholds 分別為假正率、真正率和閾值。(應該是不同閾值下的真正率和假正率)。
roc_auc為計算的acu的值。
邏輯回歸模型 乳腺癌資料集
匯入資料集 from sklearn import datasets import warnings warnings.filterwarnings ignore df datasets.load breast cancer x df.data y df.target x.shape 檢視屬性維度 ...
sklearn之Knn實戰乳腺癌資料案例
from sklearn.datasets import load breast cancer from sklearn.neighbors import kneighborsclassifier from sklearn.model selection import train test spli...
重做五種乳腺癌亞型的json檔案
1 用乙個list來儲存五種亞型 import pandas as pd df pd.read csv c users administrator desktop 重做json first1.csv header none,low memory false data df.values data l...