import pandas as pd
from sklearn.ensemble import RandomForestClassifier
df = pd.read_excel(r'C:\Users\Administrator\Desktop\DBL.xlsx')
X = df.iloc[:, 0:]
Y = df["a1"] # 最后要预测的对象
df.head()
clf = RandomForestClassifier()
# 模型拟合数据
clf.fit(X, Y)
feat_importances = pd.Series(clf.feature_importances_, index=X.columns)
# 筛选出特征的重要性程度最大的10个特征
feat_importances.nlargest(10)
feat_importances.nlargest(10).plot(kind='barh', figsize = (8, 6))
报错信息:
Traceback (most recent call last):
File "D:\PythoProject\BISHE\CAO.py", line 13, in <module>
clf.fit(X, Y)
File "D:\envir\lib\site-packages\sklearn\ensemble\_forest.py", line 385, in fit
y, expanded_class_weight = self._validate_y_class_weight(y)
File "D:\envir\lib\site-packages\sklearn\ensemble\_forest.py", line 746, in _validate_y_class_weight
check_classification_targets(y)
File "D:\envir\lib\site-packages\sklearn\utils\multiclass.py", line 218, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
以下答案由GPT-4.0Plus大模型与博主A努力中.......共同编写:
这是一个比较常见的报错问题,主要有以下几个可能原因:
所以,解决这个报错问题,首先需要检查:
然后,可以按以下步骤进行 checking:
py
print(type(X))
print(type(Y))
py
print(X.shape)
print(Y.shape)
进行上述检查后,应能找到报错的具体原因,并采取相应措施进行修复。