weixin_45654810
2021-12-27 22:48
采纳率: 0%
浏览 40

决策树在实现机器学习时报错

问题遇到的现象和发生背景

在决策树案例实现时发生报错

问题相关代码,请勿粘贴截图
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import train_test_split

df=pd.read_csv("CAR.csv")
df.loc[df["class"]=="unacc","class"]=0
df.loc[df["class"]=="acc","class"]=1
df.loc[df["class"]=="good","class"]=1
df.loc[df["class"]=="v_good","class"]=1
print(df.head())
print(df.info())
df.describe()
#去除字符串数值前面的空格
str_cols=[0,1,2,3,4,5]
for col in str_cols:
    df.iloc[:,col]=df.iloc[:,col].map(lambda  x: x.strip())
#划分训练集和测试集
x=df[["buying","maint","doors","persons","lug_boot","safety"]]
y=df["class"]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)
x_train=x_train.to_dict(orient="records")
x_test=x_test.to_dict(orient="records")
transfer=DictVectorizer()
x_train=transfer.fit_transform(x_train)
x_test=transfer.fit_transform(x_test)
#机器学习
estimator=DecisionTreeClassifier()
estimator.fit(x_train,y_train)
y_pre=estimator.predict(x_test)
print(y_pre)

运行结果及报错内容

Traceback (most recent call last):
File "F:\python\dashuju\CAR.py", line 31, in
estimator.fit(x_train,y_train)
File "F:\python\venv\lib\site-packages\sklearn\tree_classes.py", line 937, in fit
super().fit(
File "F:\python\venv\lib\site-packages\sklearn\tree_classes.py", line 203, in fit
check_classification_targets(y)
File "F:\python\venv\lib\site-packages\sklearn\utils\multiclass.py", line 197, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'unknown'

我的解答思路和尝试过的方法
我想要达到的结果


1条回答 默认 最新

相关推荐 更多相似问题