簡體   English   中英

如何保存我必須編碼的真實標簽?

[英]How to save the true labels I have to encode?

為了獲得帶有病態學習的決策樹,我需要對數據框進行標簽編碼。

    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_來識別模型分配給標簽的數字。

例如,如果clf.classes_["class1", "class3", "class2"] (假設您只有三個類),則意味着預測標簽和實際標簽之間存在以下映射: {0: "class1", 1:"class3", 2: "class2"}

在這種情況下,在 sklearn 混淆矩陣輸出中, X軸刻度標簽為 2, 1, 0,Y 軸刻度標簽為 0, 1, 2(軸值遞增順序)。 您可以使用上述字典將這些映射回標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM