ValueError: X has 1 features, but DecisionTreeRegressor is expecting 2 features as input. 错误分析与解决方案

这段代码使用自定义的随机森林模型进行回归分析,并在图表中展示结果。 错误的根源在于模型预测时输入的特征数量不一致,模型期望有两个特征,但实际只提供了一个特征。

代码片段:

ValueError                                Traceback (most recent call last)
Cell In[14], line 13
     11 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
     12 xx = np.arange(x_min, x_max, .05)
---> 13 yy = rf.predict(np.c_[xx.ravel()])
     15 plt.scatter(X, y, c='#e63946', marker='o', s=20)
     16 plt.plot(xx, yy)

Cell In[12], line 41, in rfr.predict(self, X)
     39     dt = self.trees[i]
     40     # 依次预测结果
---> 41     ys += dt.predict(X)
     42 # 预测结果取平均
     43 ys /= self.n_estimators

File ~\anaconda3\lib\site-packages\sklearn\tree\_classes.py:467, in BaseDecisionTree.predict(self, X, check_input)
    444 """Predict class or regression value for X.
    445 
    446 For a classification model, the predicted class for each sample in X is
   (...)
    464     The predicted classes, or the predict values.
    465 """
    466 check_is_fitted(self)
--> 467 X = self._validate_X_predict(X, check_input)
    468 proba = self.tree_.predict(X)
    469 n_samples = X.shape[0]

File ~\anaconda3\lib\site-packages\sklearn\tree\_classes.py:433, in BaseDecisionTree._validate_X_predict(self, X, check_input)
    431 """Validate the training data on predict (probabilities)."""
    432 if check_input:
--> 433     X = self._validate_data(X, dtype=DTYPE, accept_sparse="csr", reset=False)
    434     if issparse(X) and (
    435         X.indices.dtype != np.intc or X.indptr.dtype != np.intc
    436     ):
    437         raise ValueError("No support for np.int64 index based sparse matrices")

File ~\anaconda3\lib\site-packages\sklearn\base.py:585, in BaseEstimator._validate_data(self, X, y, reset, validate_separately, **check_params)
    582     out = X, y
    584 if not no_val_X and check_params.get("ensure_2d", True):
--> 585     self._check_n_features(X, reset=reset)
    587 return out

File ~\anaconda3\lib\site-packages\sklearn\base.py:400, in BaseEstimator._check_n_features(self, X, reset)
    397     return
    399 if n_features != self.n_features_in_:
--> 400     raise ValueError(
    401         f"X has {n_features} features, but {self.__class__.__name__} "
    402         f"is expecting {self.n_features_in_} features as input."
    403     )

ValueError: X has 1 features, but DecisionTreeRegressor is expecting 2 features as input.

解决方法:

  1. 检查数据集的维度: 确认数据集的特征数量是否与模型期望一致。2. 添加虚拟特征: 如果数据集只有一个特征,但模型需要两个特征,可以通过添加一个虚拟特征来解决问题。例如,可以使用 np.zeros(len(xx)) 创建一个长度为len(xx) 的虚拟特征,然后将其与 xx.ravel() 合并:
yy = rf.predict(np.c_[xx.ravel(), np.zeros(len(xx))])
  1. 选择其他模型: 如果数据集确实只有一个特征,可以选择其他适合处理单特征数据的模型,例如线性回归模型。

总结:

此错误的根本原因在于模型期望的特征数量与实际输入的特征数量不匹配。通过检查数据集维度、添加虚拟特征或选择其他模型,可以有效解决这个问题。

解决ValueError: X has 1 features, but DecisionTreeRegressor is expecting 2 features as input. - 随机森林回归模型错误分析

原文地址: https://www.cveoy.top/t/topic/oQb2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录