ValueError:模型的特征数量必须与输入匹配。在随机森林中,模型n_features为10,输入n_features为7

问题描述 投票:0回答:1

我正在尝试建立预测模型并收到以下错误。我该如何解决?

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-cd63269e2c86> in <module>
      8 
      9 # Make predictions on test data using the model trained on original data
---> 10 baseline_predictions = rf.predict(original_test_features)
     11 
     12 # Performance metrics

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\ensemble\forest.py in predict(self, X)
    691         check_is_fitted(self, 'estimators_')
    692         # Check data
--> 693         X = self._validate_X_predict(X)
    694 
    695         # Assign chunk of trees to jobs

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\ensemble\forest.py in _validate_X_predict(self, X)
    357                                  "call `fit` before exploiting the model.")
    358 
--> 359         return self.estimators_[0]._validate_X_predict(X, check_input=True)
    360 
    361     @property

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\tree\tree.py in _validate_X_predict(self, X, check_input)
    400                              "match the input. Model n_features is %s and "
    401                              "input n_features is %s "
--> 402                              % (self.n_features_, n_features))
    403 
    404         return X

ValueError: Number of features of the model must match the input. Model n_features is 10 and input n_features is 7 

这里是我使用的代码.....

# Find the original feature indices 
original_feature_indices = [feature_list.index(feature) for feature in
                                      feature_list if feature not in
                                      ['feature1', 'feature2', 'feature3']]

# Create a test set of the original features
original_test_features = test_features[:, original_feature_indices]

# Make predictions on test data using the model trained on original data
baseline_predictions = rf.predict(original_test_features)

# Performance metrics
baseline_errors = abs(baseline_predictions - test_labels)

print('Metrics for Random Forest Trained on Original Data')
print('Average absolute error:', round(np.mean(baseline_errors), 2), 'degrees.')

# Calculate mean absolute percentage error (MAPE)
baseline_mape = 100 * np.mean((baseline_errors / test_labels))

# Calculate and display accuracy
baseline_accuracy = 100 - baseline_mape
print('Accuracy:', round(baseline_accuracy, 2), '%.')

我正在尝试使用随机森林分类器为特定数据集构建预测模型。但是我收到此错误消息,指出模型的数字特征与输入不匹配。

python machine-learning scikit-learn data-science
1个回答
0
投票

您的模型训练了10个功能,现在您的预测集original_test_features只有7个功能。这就是为什么出现功能不匹配错误的原因。请检查original_test_features并确保所有功能都与模型训练集匹配。

© www.soinside.com 2019 - 2024. All rights reserved.