我正在使用sklearn进行随机森林分类。现在,我想比较不同的描述符集(一个具有125个功能,一个具有154个功能)。因此,我正在创建两个不同的随机森林,但是它们似乎相互覆盖,从而导致错误:'模型的特征数量必须与输入匹配。模型n_features为125,输入n_features为154'
rf_std = RandomForestClassifier(n_estimators = 150, max_depth = 200, max_features = 'sqrt')
rf_nostd = RandomForestClassifier(n_estimators = 150, max_depth = 200, max_features = 'sqrt')
rf_std=rf_std.fit(X_train_std,y_train_std)
print('Testing score std:',rf_std.score(X_test_std,y_test_std))
rf_nostd=rf_nostd.fit(X_train_nostd,y_train_nostd)
print('Testing score nostd:',rf_nostd.score(X_test_nostd,y_test_nostd))
# until here it works
fig, (ax1, ax2) = plt.subplots(1, 2)
disp = plot_confusion_matrix(rf_std, X_test_std, y_test_std,
cmap=plt.cm.Blues,
normalize='true',ax=ax1)
disp = plot_confusion_matrix(rf_nostd, X_test_nostd, y_test_nostd,
cmap=plt.cm.Blues,
normalize='true',ax=ax2)
plt.show()
#here i get the error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-eee9fea5dbfb> in <module>
3 disp = plot_confusion_matrix(rf_std, X_test_std, y_test_std,
4 cmap=plt.cm.Blues,
----> 5 normalize='true',ax=ax1)
6 disp = plot_confusion_matrix(rf_nostd, X_test_nostd, y_test_nostd,
7 cmap=plt.cm.Blues,
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\_plot\confusion_matrix.py in plot_confusion_matrix(estimator, X, y_true, labels, sample_weight, normalize, display_labels, include_values, xticks_rotation, values_format, cmap, ax)
183 raise ValueError("plot_confusion_matrix only supports classifiers")
184
--> 185 y_pred = estimator.predict(X)
186 cm = confusion_matrix(y_true, y_pred, sample_weight=sample_weight,
187 labels=labels, normalize=normalize)
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\ensemble\_forest.py in predict(self, X)
610 The predicted classes.
611 """
--> 612 proba = self.predict_proba(X)
613
614 if self.n_outputs_ == 1:
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\ensemble\_forest.py in predict_proba(self, X)
654 check_is_fitted(self)
655 # Check data
--> 656 X = self._validate_X_predict(X)
657
658 # Assign chunk of trees to jobs
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\ensemble\_forest.py in _validate_X_predict(self, X)
410 check_is_fitted(self)
411
--> 412 return self.estimators_[0]._validate_X_predict(X, check_input=True)
413
414 @property
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\tree\_classes.py in _validate_X_predict(self, X, check_input)
389 "match the input. Model n_features is %s and "
390 "input n_features is %s "
--> 391 % (self.n_features_, n_features))
392
393 return X
ValueError: Number of features of the model must match the input. Model n_features is 125 and input n_features is 154
EDIT:拟合第二个randomforest会以某种方式覆盖第一个randomforest,如下所示:
rf_std=rf_std.fit(X_train_std,y_train_std)
print(rf_std.n_features_)
rf_nostd=rf_nostd.fit(X_train_nostd,y_train_nostd)
print(rf_std.n_features_)
Output:
154
125
为什么两个模型不分开,有人可以帮忙吗?
我能够在train
和test
输入形状不一致的情况下重现此错误。
尝试一下:
assert X_train_std.shape[-1] == X_test_std.shape[-1], "Input shapes don't match."
assert X_train_nostd.shape[-1] == X_test_nostd.shape[-1], "Input shapes don't match."
这是我重现您的错误的方式:
import numpy as np
from sklearn.ensemble import RandomForestClassifier
X_train_std = np.random.rand(400, 154)
X_test_std = np.random.rand(100, 125)
y_train_std = np.random.randint(0, 2, 400).tolist()
y_test_std = np.random.randint(0, 2, 100).tolist()
rf_std = RandomForestClassifier(n_estimators = 150,
max_depth = 200, max_features = 'sqrt')
rf_std=rf_std.fit(X_train_std,y_train_std)
print('Testing score std:',rf_std.score(X_test_std,y_test_std))
ValueError:模型的特征数量必须与输入匹配。模型n_features为154,输入n_features为125
通常在您的训练/测试集与形状不匹配时发生。您能否检查以下各项的形状信息匹配项?
X_train_std.shape[1] == X_test_std.shape[1]
X_train_nostd.shape[1] == X_test_nostd.shape[1]
如果它符合您的要求,否则您必须前往发现差异的地方。
关于,MJ