【问题标题】:Unknown label type: %r" % y_type) ValueError: Unknown label type: 'unknown'未知标签类型:%r" % y_type) ValueError:未知标签类型:'unknown'
【发布时间】:2021-08-17 11:17:37
【问题描述】:

我正在将 DT 应用于我的信用卡流失数据集,但出现错误。

我使用的代码附在下面。 我遇到了未知标签类型错误。我该如何解决?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.tree import plot_tree


df=pd.read_csv(r'G:\\Edu\\My academics\\MSc in CS\\3rd sem\\Research\\Python files\\BankChurners.csv')
##df = sns.load_dataset(r'G:\\Edu\\My academics\\MSc in CS\\3rd sem\\Research\\Python files\\BankChurners.csv')
df.head()
df.drop(['CLIENTNUM',
'Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_1',
'Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_2'],axis = 1, inplace = True)
print(df.columns)
##sns.pairplot(data=df, hue = 'Attrition_Flag')
##plt.show()
sns.heatmap(df.corr())
plt.show()
df = pd.get_dummies(df, columns = ['Gender','Education_Level','Marital_Status','Income_Category','Card_Category'])
target = df['Attrition_Flag']
df1 = df.copy()
df1 = df1.drop('Attrition_Flag', axis =1)
X = df1
df.loc[df['Attrition_Flag'] == 'Existing Customer', 'Attrition_Flag'] = 0
df.loc[df['Attrition_Flag'] == 'Attrited Customer', 'Attrition_Flag'] = 1
df.Attrition_Flag = df.Attrition_Flag.astype(int)
y = target

X_train, X_test, y_train, y_test = train_test_split(X , y, test_size = 0.2, random_state = 42)
print("Training split input- ", X_train.shape)
print("Testing split input- ", X_test.shape)

### Defining the decision tree algorithm
dtree=DecisionTreeClassifier()
dtree.fit(X_train,y_train)
print('Decision Tree Classifier Created')

我得到的错误:

【问题讨论】:

  • y_train的形状和类型是什么,它的独特价值是什么?
  • 请勿发布代码、数据、错误消息等的图片 - 将文本复制或输入到问题中。 How to Ask

标签: python scikit-learn decision-tree


【解决方案1】:

对目标进行了标签编码,因为上面的目标将其显示为对象类型,导致此错误

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 2018-12-16
    • 2021-05-11
    • 2018-03-16
    • 2020-05-14
    • 2019-07-03
    • 2018-01-01
    • 2017-08-29
    • 2018-12-10
    相关资源
    最近更新 更多