【发布时间】:2019-04-09 19:57:56
【问题描述】:
我有一个从 scikit learn 导入随机森林分类器的函数,我用数据拟合它,最后我想显示准确度、kappa 和混淆矩阵。除了打印混淆矩阵之外的所有工作。我没有收到任何错误,但没有打印混淆矩阵。
我尝试调用 print(cm) 并且它可以工作,但它不能以通常的 pandas 数据框样式打印,这正是我正在寻找的。p>
这是代码
def rf_clf(X, y, test_size = 0.3, random_state = 42):
"""This function splits the data into train and test and fits it in a random forest classifier
to the data provided, analysing its errors (Accuracy and Kappa). Also as this is classification,
the function will output a confusion matrix"""
#Split data in train and test, as well as predictors (X) and targets, (y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state, stratify=y)
#import random forest classifier
base_model = RandomForestClassifier(random_state=random_state)
#Train the model
base_model.fit(X_train,y_train)
#make predictions on test set
y_pred=base_model.predict(X_test)
#Print Accuracy and Kappa
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Kappa:",metrics.cohen_kappa_score(y_test, y_pred))
#create confusion matrix
labs = [y_test[i][0] for i in range(len(y_test))]
cm = pd.DataFrame(confusion_matrix(labs, y_pred))
cm #here is the issue. Kinda works with print(cm)
【问题讨论】:
-
你想要列名和索引吗?也许在创建数据框本身时尝试喂食?前任。 pd.DataFrame( ... , columns = [ ... ], index = [ ... ])
-
在最后一行尝试
return cm -
谢谢@ChrisA,你的回答对我有用。谢谢!
标签: python pandas function machine-learning confusion-matrix