Python ValueError:模型的特征数必须与输入匹配。在随机林中,模型n_特征为10,输入n_特征为7
我试图建立一个预测模型,但我得到了以下错误。我怎么把这个修好Python ValueError:模型的特征数必须与输入匹配。在随机林中,模型n_特征为10,输入n_特征为7,python,machine-learning,scikit-learn,data-science,Python,Machine Learning,Scikit Learn,Data Science,我试图建立一个预测模型,但我得到了以下错误。我怎么把这个修好 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-cd63269e2c86> in <module> 8
---------------------------------------------------------------------------
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
我试图用随机森林分类器为某一组数据建立一个预测模型。但是,我收到一条错误消息,指出模型的数字特征与输入不匹配 您的模型已使用具有10个特征的数据集进行训练,现在您的预测集(即
原始测试特征
仅具有7个特征)。这就是为什么会出现功能不匹配错误。请检查原始\u测试\u功能
,确保所有功能都与模型训练集匹配。我正在尝试调整参数,如果不调整,模型精度将保持不变。。。原始特征索引=[特征列表。如果特征不在['feature1'、'feature2'、'feature3']]中,则特征列表中特征的索引(特征)]我可以用其他方法进行参数调整吗??
# 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), '%.')