我是决策的新手,并试图从具有到目前为止得分的Review数据框中创建决策树,而我尝试了这个bu却给了我
X = ndf.drop('Score', axis=1)
y = ndf['Score']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.30)
model = tree.DecisionTreeClassifier()
model.fit(X_train, y_train)
ndf ['Score']中的所有值都是1或0。当我尝试运行最后一行时,出现了Unknown标签类型错误:'unknown'。我该怎么解决?
答案 0 :(得分:0)
确保所有标签(y_train
和y_test
)仅是单一类型(int
或string
)。
对于您来说,int
可能是标签的适当类型,如果在执行{时看到int
的类型为Score
,则将其转换为object
{1}}
ndf.info()
通常,我们应该始终对列应用适当的类型,而不仅仅是# Add below line to convert the typt to `int`
xdf['Score'] = xdf['Score'].astype('int')
# Your code should be working fine now
X = ndf.drop('Score', axis=1)
y = ndf['Score']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.30)
model = tree.DecisionTreeClassifier()
model.fit(X_train, y_train)
。