我是靠谱客的博主 任性路人,最近开发中收集的这篇文章主要介绍第11周sklearn学习笔记,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

从两分类评价扩展到多分类评价
In [36]:

1
from sklearn import datasets
2
from sklearn import tree
3
iris = datasets.load_iris()
4
clf = tree.DecisionTreeClassifier()
5
clf = clf.fit(iris.data, iris.target)
In [37]:

1
#二分法拆分数据
2
from sklearn.model_selection import train_test_split
3
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size = 0.3,random_state = 666)
In [38]:

1
len(x_train),len(x_test),len(y_train),len(y_test)
Out[38]:
(105, 45, 105, 45)
In [39]:

1
from sklearn.tree import DecisionTreeClassifier
2
dt = DecisionTreeClassifier()
3
dt.fit(x_train,y_train)

In [40]:

1
dt.predict(x_test)
Out[40]:
array([1, 2, 1, 2, 0, 1, 1, 2, 1, 1, 1, 0, 0, 0, 2, 1, 0, 2, 2, 2, 1, 0,
2, 0, 1, 1, 0, 1, 2, 2, 0, 0, 1, 2, 1, 1, 2, 2, 0, 1, 2, 2, 1, 1,
0])
In [41]:

1
from sklearn.metrics import classification_report
2
print(classification_report(y_test,dt.predict(x_test)))
precision recall f1-score support


0
1.00
1.00
1.00
12
1
1.00
1.00
1.00
18
2
1.00
1.00
1.00
15

micro avg 1.00 1.00 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45

In [42]:

1
print(classification_report(y_train,dt.predict(x_train)))
precision recall f1-score support


0
1.00
1.00
1.00
38
1
1.00
1.00
1.00
32
2
1.00
1.00
1.00
35

micro avg 1.00 1.00 1.00 105
macro avg 1.00 1.00 1.00 105
weighted avg 1.00 1.00 1.00 105

In [43]:

1
#混淆矩阵
2
from sklearn.metrics import confusion_matrix
3
cm = confusion_matrix(y_train,dt.predict(x_train))
4
cm
Out[43]:
array([[38, 0, 0],
[ 0, 32, 0],
[ 0, 0, 35]], dtype=int64)
In [44]:

1
#自定义类别顺序输出混淆矩阵
2
confusion_matrix(y_train,dt.predict(x_train),labels=[2,1,0])
Out[44]:
array([[35, 0, 0],
[ 0, 32, 0],
[ 0, 0, 38]], dtype=int64)
In [45]:

1
#用热力图形式展示
2
%matplotlib inline
3
import matplotlib.pyplot as plt
4
import seaborn as sns
5
sns.heatmap(cm,cmap = sns.color_palette(“Blues”),annot = True)
Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x1cd4dd56278>

In [46]:

1
import numpy as np
2
from sklearn.metrics import precision_recall_curve
3
y_true = np.array([0,0,0,0,0,1,1,1,1,1])
4
y_scores = np.array([0.1,0.2,0.25,0.4,0.6,0.2,0.45,0.6,0.75,0.8])
5

6
precision, recall, thresholds = precision_recall_curve(y_true,y_scores)
7

8
len(precision),len(recall),len(thresholds)
Out[46]:
(8, 8, 7)
In [47]:

1
thresholds
Out[47]:
array([0.2 , 0.25, 0.4 , 0.45, 0.6 , 0.75, 0.8 ])
In [50]:

1
precision
Out[50]:
array([0.55555556, 0.57142857, 0.66666667, 0.8 , 0.75 ,
1. , 1. , 1. ])
In [49]:

1
recall
Out[49]:
array([1. , 0.8, 0.8, 0.8, 0.6, 0.4, 0.2, 0. ])
In [ ]:

1
#计算ROC曲线下面积
2
y_true, y_score, average = ‘macro’,sample_weight = None)
In [ ]:

1
from sklearn.metrics import roc_auc_s0
2

In [ ]:

1
class sklearn model

最后

以上就是任性路人为你收集整理的第11周sklearn学习笔记的全部内容,希望文章能够帮你解决第11周sklearn学习笔记所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部