我正在尝试在示例数据集的随机森林回归模型上使用 scipy fmin 函数。该模型运行良好,但是当我尝试使用初始猜测 np.zeros(8) 的 fmin 函数时,我收到此错误:
ValueError: Expected 2D array, got 1D array instead:
array=[0. 0. 0. 0. 0. 0. 0. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a
single feature or array.reshape(1, -1) if it contains a single sample.
所以我确实重塑了数组,它返回完全相同的错误消息。这是到目前为止的代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn as sk
import scipy as sp
data = pd.read_csv('Concrete_Data.csv')
data.describe(include='all')
Y = data.iloc[:,-1]
X = data.iloc[:,0:-1]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2,
random_state = 0)
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(random_state = 0)
regressor.fit(X_train,y_train)
def f(x):
p=regressor.predict(x)
return p
guess = np.zeros(8)
guess = guess.reshape(-1, 1)
minimum = sp.optimize.fmin(f,guess)
print('min = ', minimum)
我也尝试从训练数据中给它一行作为初始猜测,它返回与以前完全相同的错误消息。这可以做到吗?如果可能的话,它将对我的工作非常有用。谢谢詹姆斯