import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.tree import DecisionTreeClassifier
path = r"C:\Users\thund\Downloads\Boat.csv"
data = pd.read_csv(path) # pip install xlrd
print(data.shape)
print(data.columns)
print(data.isnull().sum())
print (data.dropna(axis=0)) #dropping rows that have missing values
print (data['Class'].value_counts())
print(data['Class'].value_counts().plot(kind = 'bar'))
#plt.show()
data['safety'].value_counts().plot(kind = 'bar')
#plt.show()
import seaborn as sns
sns.countplot(data['demand'], hue = data['Class'])
#plt.show()
X = data.drop(['Class'], axis = 1)
y = data['Class']
from sklearn.preprocessing import OrdinalEncoder
demand_category = ['low', 'med', 'high', 'vhigh']
maint_category = ['low', 'med', 'high', 'vhigh']
seats_category = ['2', '3', '4', '5more']
passenger_category = ['2', '4', 'more']
storage_category = ['Nostorage', 'small', 'med']
safety_category = ['poor', 'good', 'vgood']
all_categories = [demand_category, maint_category,seats_category,passenger_category,storage_category,safety_category]
oe = OrdinalEncoder(categories= all_categories)
X = oe.fit_transform( data[['demand','maint', 'seats', 'passenger', 'storage', 'safety']])
数据集:
对于上述代码,我一直在拟合期间在第2列中获得此“ ValueError:valueerror:发现的未知类别[NAN]”。我尝试删除所有缺失的值。我尝试搜索修复程序,发现某人关于使用handing_unknown =“ imploore”的建议,但我认为它不适用于序数编码。
我对Python是个新手,因此,如果有人可以对为什么会发生这种情况以及如何努力解决它的深入分析,这将非常感谢它。
PS:这是用于预处理数据。
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.tree import DecisionTreeClassifier
path = r"C:\Users\thund\Downloads\Boat.csv"
data = pd.read_csv(path) # pip install xlrd
print(data.shape)
print(data.columns)
print(data.isnull().sum())
print (data.dropna(axis=0)) #dropping rows that have missing values
print (data['Class'].value_counts())
print(data['Class'].value_counts().plot(kind = 'bar'))
#plt.show()
data['safety'].value_counts().plot(kind = 'bar')
#plt.show()
import seaborn as sns
sns.countplot(data['demand'], hue = data['Class'])
#plt.show()
X = data.drop(['Class'], axis = 1)
y = data['Class']
from sklearn.preprocessing import OrdinalEncoder
demand_category = ['low', 'med', 'high', 'vhigh']
maint_category = ['low', 'med', 'high', 'vhigh']
seats_category = ['2', '3', '4', '5more']
passenger_category = ['2', '4', 'more']
storage_category = ['Nostorage', 'small', 'med']
safety_category = ['poor', 'good', 'vgood']
all_categories = [demand_category, maint_category,seats_category,passenger_category,storage_category,safety_category]
oe = OrdinalEncoder(categories= all_categories)
X = oe.fit_transform( data[['demand','maint', 'seats', 'passenger', 'storage', 'safety']])
Dataset: https://drive.google.com/file/d/1O0sYZGJep4JkrSgGeJc5e_Nlao2bmegV/view?usp=sharing
For the mentioned code I keep getting this 'ValueError: Found unknown categories [nan] in column 2 during fit'. I have tried dropping all missing values. I tried searching for a fix and I found someone's suggestion on using handle_unknown="ignore", but I don't think it works for ordinal encoding.
I am fairly new to python so would deeply appreciate it if someone could give me an in-depth analysis of why this is happening and how can I work to fix it.
Ps: This is for pre-processing the data.
发布评论
评论(1)
为了解释错误,您已经删除了“ NAN”,只是用丢弃的数据打印了数据帧。
根据您的数据集或错误,您在“座位”中具有“ NAN”值。
打印出
data ['seats']。
您
这是什么是,它将原始数据框更新为其更新的值
手动分配:
这确实做了“ Intplate”的作用,但不是那么有效,而是更易于理解的。
希望这回答您的问题。
To explain the error, You have dropped the "NaN" and just printed the DataFrame with dropped data.
According to your dataset or the ERROR you have a value "NaN" in column "seats".
When you print out the
data['seats'].unique()
, You get something like this:There are two solutions:
Using inplace :
What this does is , it updates the original DataFrame to its updated value
Manually assigning:
This exactly does what 'inplace' does but its not that effecient but more understandable.
Hope this answers your question.