gpt4 book ai didi

python - ValueError : Number of features of the model must match the input. 随机森林中模型 n_features 为 10,输入 n_features 为 7

转载 作者:行者123 更新时间:2023-11-30 09:59:45 26 4
gpt4 key购买 nike

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

---------------------------------------------------------------------------
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), '%.')

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

最佳答案

您的模型已经使用具有 10 个特征的数据集进行了训练,现在您的预测集(original_test_features)只有 7 个特征。这就是您收到特征不匹配错误的原因。检查 original_test_features 并确保所有特征都与模型训练集匹配。

关于python - ValueError : Number of features of the model must match the input. 随机森林中模型 n_features 为 10,输入 n_features 为 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com