创建混淆矩阵时出错,尽管对目标变量和特征变量进行了标签编码

时间:2019-02-12 08:57:06

标签: python-3.x machine-learning scikit-learn confusion-matrix

在创建混淆矩阵时,我反复遇到此错误。我的特征变量以及目标变量都被labelEncoded编码,但是仍然不知道为什么它会产生此错误。

错误: C:\ Users \ Strat Com \ PycharmProjects \ IGN Review \ venv \ lib \ site-packages \ sklearn \ metrics \ classification.py:261:FutureWarning:逐元素比较失败;而是返回标量,但将来会执行逐元素比较

ValueError::必须至少在y_true中指定一个标签

注意:随附说明和数据集的代码。 使用Windows 10并在Jupyter Notebook上运行所有这些代码

Link of Data Set

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

DataFrame=pd.read_csv("DataSet.txt",sep='\t',low_memory=False,skip_blank_lines=True)        # Loading the data into the Data Frame
DataFrame=DataFrame.dropna(how='all')
half_count=len(DataFrame)/2
DataFrame=DataFrame.dropna(thresh=half_count,axis=1)                                        # Dropping any column with more than 50% missing values

FrameExplorer = pd.DataFrame(DataFrame.dtypes,columns=['dtypes'])
FrameExplorer=FrameExplorer.reset_index()
FrameExplorer=FrameExplorer.rename(columns={'index':'ColumnName'})

drop_list=['IDShop','PaymentDay','ShopRank','OtherCards','QuantBankAccounts','ApplicationBooth','InsuranceOption']
DataFrame=DataFrame.drop(drop_list,axis=1)


DataFrame = DataFrame.loc[:,DataFrame.apply(pd.Series.nunique) != 1]                        # Getting all the columns which dont have 1 unique value

for cols in DataFrame.columns:  
    if (len(DataFrame[cols].unique())<4):
        print (DataFrame[cols].value_counts())

null_counts = DataFrame.isnull().sum()
print("Number of Null count in each column \n{}".format(null_counts))

# Here we would remove the column containing more than 1% of the rows contains null values So from above column names so
# "Sex" and "Reference 2" would be dropped as they contain approx 10% of rows of missing values

DataFrame=DataFrame.drop(['Sex','Reference2'],axis=1)

DataFrame=DataFrame.dropna()                      # Dropping rows containing missing values to make data more cleaner

DataFrame=DataFrame.drop('Reference1',axis=1)

# Now we would be Label Encoding the columns of object dataType as shown above as they contain only "Y" and "N" Value 

FeatureEncoder=preprocessing.LabelEncoder()

DataFrame['MaritalStatus']=FeatureEncoder.fit_transform(DataFrame['MaritalStatus'])
DataFrame['ResidencialPhone']=FeatureEncoder.fit_transform(DataFrame['ResidencialPhone'])
DataFrame['ResidenceType']=FeatureEncoder.fit_transform(DataFrame['ResidenceType'])
DataFrame['MothersName']=FeatureEncoder.fit_transform(DataFrame['MothersName'])
DataFrame['FathersName']=FeatureEncoder.fit_transform(DataFrame['FathersName'])
DataFrame['WorkingTown']=FeatureEncoder.fit_transform(DataFrame['WorkingTown'])
DataFrame['WorkingState']=FeatureEncoder.fit_transform(DataFrame['WorkingState'])
DataFrame['PostalAddress']=FeatureEncoder.fit_transform(DataFrame['PostalAddress'])

# Now we will start to split the data into training set and testing set to train the model and then test it 

cols = [col for col in DataFrame.columns if col not in ['Label']]           # Label is the Target Feature
FeatureData=DataFrame[cols]                                                 # Feature Variables
TargetData=DataFrame['Label']                                               # Target Variables

#split data set into train and test sets
FeatureData_Train, FeatureData_Test, TargetData_Train, TargetData_Test = train_test_split(FeatureData,TargetData, test_size = 0.30, random_state = 10)


type(FeatureData_Train)
type(TargetData_Train)

# Next we will be feeding all of the split done above to the model 

neighbor=KNeighborsClassifier(n_neighbors=3)                     # Creating an Object of KNN Classifier

neighbor.fit(FeatureData_Train,TargetData_Train)                 # Training the model to classify
PredictionData=neighbor.predict(FeatureData_Test)                # Predicting the Response 

# evaluate accuracy
print ("KNeighbors accuracy score : ",accuracy_score(TargetData_Test, PredictionData))


from yellowbrick.classifier import ClassificationReport
from yellowbrick.classifier import ConfusionMatrix

# Instantiate the classification model and visualizer
visualizer = ClassificationReport(neighbor, classes=['0','1'])

visualizer.fit(FeatureData_Train,TargetData_Train)             # Fit the training data to the visualizer
visualizer.score(FeatureData_Test,TargetData_Test)             # Evaluate the model on the test data
g = visualizer.poof()                                          # Draw/show/poof the data

cm = ConfusionMatrix(neighbor, classes=['0','1'])

cm.fit(FeatureData_Train,TargetData_Train)
cm.score(FeatureData_Test,TargetData_Test)

1 个答案:

答案 0 :(得分:4)

问题是您提供的类的数据类型与数据集中的类不同。在数据中,您将类型定义为字符串(在文件末尾的第三行),而类型为float。

只需将该行更改为:

cm = ConfusionMatrix(neighbor, classes=[0,1])

它将正常工作。