Python 3.x 如何保存我必须编码的真实标签?
为了使用sickit learn获得决策树,我需要标记一个数据帧Python 3.x 如何保存我必须编码的真实标签?,python-3.x,label,label-encoding,Python 3.x,Label,Label Encoding,为了使用sickit learn获得决策树,我需要标记一个数据帧 S02Q01_Gender S02Q02_Age_rec S02Q03A_Region S02Q03B_Settlement_type S02Q03C_Province S02Q10A_Employment S02Q11_Professional_field Segment Cluster 0 Female 12-19 Marrakesh U
S02Q01_Gender S02Q02_Age_rec S02Q03A_Region S02Q03B_Settlement_type S02Q03C_Province S02Q10A_Employment S02Q11_Professional_field Segment Cluster
0 Female 12-19 Marrakesh Urban Casablanca-Settat Student None Class1
1 Male 65 or above Marakesh Rural El Jadida My Employed, part-time Property Class2
...
但是,为了在混淆矩阵上正确地绘制它,我需要保存target
列的标签
我试过:
y_test_dencoded = label_encoder.inverse_transform(y_test)
y_pred_dencoded = label_encoder.inverse_transform(y_pred)
cnf_matrix = metrics.confusion_matrix(y_test_dencoded, y_pred_dencoded, labels=None, sample_weight=None)
并绘制它:
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
df_cm = pd.DataFrame(cnf_matrix, index = [i for i in set(y_test_dencoded)],
columns = [i for i in set(y_pred_dencoded)])
plt.figure(figsize = (10,7))
ax = sn.heatmap(df_cm, annot=True)
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)
它返回一个混淆矩阵,但我不知道我是否很好地标记了它…如果您的分类器是
clf
,您可以使用clf.classes\uu
来识别模型分配给标签的编号
例如,如果clf.classes
是[“class1”、“class3”、“class2”]
(假设您只有三个类),这意味着以下映射在预定标签和实际标签之间有效:{0:“class1”、1:“class3”、2:“class2”}
在这种情况下,在sklearn混淆矩阵输出中,X
轴刻度标签为2,1,0,Y轴刻度标签为0,1,2(以轴值递增顺序)。您可以使用上述字典将这些映射回标签