ValueError: Found array with dim 3. Estimator expected <= 2. when using numpy arrays

ValueError: Found array with dim 3. Estimator expected <= 2. when using numpy arrays

我有两个 numpy 数组

import numpy as np

temp_1 = np.array([['19.78018766'],
 ['19.72487359'],
 ['19.70280336'],
 ['19.69589641'],
 ['19.69746018']])

temp 2 = np.array([['43.8'],
 ['43.9'],
 ['44'],
 ['44.1'],
 ['44.2']])

我正在准备X = np.stack((temp_1,temp_2), axis=-1) 看起来像这样

 X = [[['19.78018766' '43.8']]
 [['19.72487359' '43.9']]
 [['19.70280336' '44']]
 [['19.69589641' '44.1']]
 [['19.69746018' '44.2']]]

我有另一个变量 Y 也是一个 numpy 数组

Y = np.array([['28.78'],
     ['32.72'],
     ['15.70'],
     ['32.69'],
     ['55.69']])

我正在尝试 运行 RandomforestRegressor 模型

哪里

from sklearn.ensemble import RandomForestRegressor
clf = RandomForestRegressor()
clf.fit(X,Y)

但是,它给我这个错误

ValueError: Found array with dim 3. Estimator expected <= 2.

发生这种情况是因为 XY 形状不同 (5, 1, 2) != (5,1)

只需将您的 X 数据重塑为您拥有的样本数量

# In this example 5 samples
X = X.reshape(5, 2)