【问题标题】:ValueError: Number of features of the model must match the input. Model n_features is 10 and input n_features is 7 in Random ForestValueError:模型的特征数量必须与输入匹配。在随机森林中模型 n_features 为 10,输入 n_features 为 7
【发布时间】:2020-04-17 04:26:38
【问题描述】:

我正在尝试构建预测模型,但出现以下错误。我该如何解决这个问题?

---------------------------------------------------------------------------
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】:

    您的模型已使用具有 10 个特征的数据集进行训练,现在您的预测集 original_test_features 只有 7 个特征。这就是为什么会出现特征不匹配错误。检查original_test_features 并确保所有特征都与模型训练集匹配。

    【讨论】:

    • 我正在尝试调整参数,如果我不这样做,模型精度保持不变... original_feature_indices = [feature_list.index(feature) for feature_list 中的特征如果特征不在 ['feature1' , 'feature2', 'feature3']] 任何其他方法我可以进行参数调整??
    猜你喜欢
    • 2020-10-01
    • 2021-04-14
    • 2021-01-19
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2017-11-04
    相关资源
    最近更新 更多