尽管对目标变量和特征变量进行了标签编码,但创建混淆矩阵时出错
Error creating confusion matrix although label encoding done on target variable and feature variable
我在创建混淆矩阵时反复遇到此错误。我的特征变量和目标变量都是 labelEncoded 但仍然不知道为什么会产生这个错误。
错误:
C:\Users\Strat Com\PycharmProjects\IGN Review\venv\lib\site-packages\sklearn\metrics\classification.py:261: FutureWarning: elementwise 比较失败;返回标量,但将来会执行逐元素比较
ValueError:至少指定一个标签必须在y_true
注:附解释代码和数据集。
在 Jupyter Notebook
上使用 Windows 10 和 运行 所有这些代码
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)
问题是你提供的类数据类型和数据集中的不一样。在数据中,您的类型为 float,而您将 类(在文件末尾的第 3 行)定义为字符串。
只需将该行更改为:
cm = ConfusionMatrix(neighbor, classes=[0,1])
它会正常工作。
我在创建混淆矩阵时反复遇到此错误。我的特征变量和目标变量都是 labelEncoded 但仍然不知道为什么会产生这个错误。
错误: C:\Users\Strat Com\PycharmProjects\IGN Review\venv\lib\site-packages\sklearn\metrics\classification.py:261: FutureWarning: elementwise 比较失败;返回标量,但将来会执行逐元素比较
ValueError:至少指定一个标签必须在y_true
注:附解释代码和数据集。 在 Jupyter Notebook
上使用 Windows 10 和 运行 所有这些代码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)
问题是你提供的类数据类型和数据集中的不一样。在数据中,您的类型为 float,而您将 类(在文件末尾的第 3 行)定义为字符串。
只需将该行更改为:
cm = ConfusionMatrix(neighbor, classes=[0,1])
它会正常工作。