如何使用sklearn获得K-Fold交叉验证的平均分

问题描述 投票:0回答:2

我使用 sklearn 应用 K 折决策树,有人可以帮助我显示它的平均分数。下面是我的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix,classification_report

dta=pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/blood-transfusion/transfusion.data")

X=dta.drop("whether he/she donated blood in March 2007",axis=1)

X=X.values # convert dataframe to numpy array

y=dta["whether he/she donated blood in March 2007"]

y=y.values # convert dataframe to numpy array

kf = KFold(n_splits=10)

KFold(n_splits=10, random_state=None, shuffle=False)

clf_tree=DecisionTreeClassifier()

for train_index, test_index in kf.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    clf=clf_tree.fit(X_train,y_train)
    print("classification_report_tree", 
           classification_report(y_test,clf_tree.predict(X_test)))
scikit-learn cross-validation
2个回答
6
投票

如果您只想要准确性,那么您可以简单地使用

cross_val_score()

kf = KFold(n_splits=10)
clf_tree=DecisionTreeClassifier()
scores = cross_val_score(clf_tree, X, y, cv=kf)

avg_score = np.mean(score_array)
print(avg_score)

这里

cross_val_score
将把你的原始 X 和 y 作为输入(不分为训练和测试)。
cross_val_score
会自动将它们分为训练和测试,在训练数据上拟合模型并在测试数据上评分。这些分数将在
scores
变量中返回。

因此,当你有 10 次折叠时,10 个分数将在

scores
变量中返回。然后你可以取平均值。


4
投票

您可以尝试 sklearn 中的 Precision_recall_fscore_support 指标,然后获取每个类别每次折叠的平均结果。我在这里假设您需要每班的平均分数。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import precision_recall_fscore_support
from sklearn.model_selection import GridSearchCV,cross_val_score

dta=pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/blood-transfusion/transfusion.data")

X=dta.drop("whether he/she donated blood in March 2007",axis=1)

X=X.values # convert dataframe to numpy array

y=dta["whether he/she donated blood in March 2007"]

y=y.values # convert dataframe to numpy array

kf = KFold(n_splits=10)

KFold(n_splits=10, random_state=None, shuffle=False)

clf_tree=DecisionTreeClassifier()

score_array =[]
for train_index, test_index in kf.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    clf=clf_tree.fit(X_train,y_train)
    y_pred = clf.predict(X_test)
    score_array.append(precision_recall_fscore_support(y_test, y_pred, average=None))

avg_score = np.mean(score_array,axis=0)
print(avg_score)

#Output:
#[[  0.77302466   0.30042282]
# [  0.81755068   0.22192344]
# [  0.79063779   0.24414489]
# [ 57.          17.8       ]]

现在要获得 0 级精度,您可以使用

avg_score[0][0]
。召回率可以通过第二行访问(即对于 0 类,它是
avg_score[1][0]
),而 fscore 和支持度可以分别从第三行和第四行访问。

© www.soinside.com 2019 - 2024. All rights reserved.