I'm trying to use the scipy fmin function on a random forest regression model of an example dataset. The model works well, but when I try the fmin function with an initial guess np.zeros(8), I get this error:
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.
So I do reshape the array and it returns the exact same error message. Here's the code so far:
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)
I've tried to give it a row from the training data as an initial guess too and it returns the exact same error message as before. Can this be done? If it's possible it would be very useful for my work. Thanks James