Reputation: 99
I am trying to fit a MLP regression model.
My data looks like
>>> x
array([[-0.10423869, -0.26620437, -0.15806682, ..., 0.14673972,
-0.08155304, -0.18497986],
[-0.09611467, -0.25407929, -0.14541038, ..., 0.12948089,
-0.08478664, -0.18818328],
[-0.11439996, -0.27334441, -0.19392899, ..., -0.05813874,
-0.0083624 , -0.1901444 ],
...,
[-0.05907788, -0.19151134, -0.00739118, ..., -0.00207883,
-0.20569605, -0.09713173],
[ 0.2577889 , -0.19076356, -0.16640778, ..., 0.19883847,
-0.19295281, 0.28263902],
[-0.04733956, -0.33666808, -0.24709939, ..., -0.2130735 ,
-0.22681055, 0.15976231]])
>>> y
array([[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]])
I want to do 5 cross-validation first to find the best parameters,here is what I wrote for just fit 50 of x_train and y_train, just to check:
mlp = MLPRegressor()
parameter_space = {
'max_iter': [1000,2000,5000],
'activation': ['relu'],
'alpha': [0.0001,0.001,0.01],
'hidden_layer_sizes': [(8,8,),(50,50,),(100,100,)],
'solver': ['sgd', 'adam'],
'learning_rate': ['constant','adaptive']
}
k = 5
kf = KFold(n_splits=k, random_state=None)
model = MLPRegressor(parameter_space)
acc_score = []
for train_index , test_index in kf.split(x):
x_train , x_valid = x[train_index,:],x[test_index,:]
y_train , y_valid = y[train_index] , y[test_index]
model.fit(x_train[1:50], y_train[1:50])
However, it shows this error:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "/local/xx/anaconda3/lib/python3.9/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 673, in fit
return self._fit(X, y, incremental=False)
File "/local/xx/anaconda3/lib/python3.9/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 358, in _fit
if np.any(np.array(hidden_layer_sizes) <= 0):
TypeError: '<=' not supported between instances of 'numpy.ndarray' and 'int'
Both x_train[1:50] and y_train[1:50] are <class 'numpy.ndarray'>.. How should I fix this?
Thank you
Upvotes: 1
Views: 312
Reputation: 1916
The parameter_space
dictionary can be passed to the estimator to be unpacked. To do so, add 2 stars before the parameter like so **parameter_space
when passing. The parameter_space
variable has to have the parameters in the supported data types. For example,
parameter_space = {
'max_iter': 1000,
'activation': 'relu',
'alpha': 0.0001,
'hidden_layer_sizes': (8,50,100),
'solver': 'sgd',
'learning_rate': 'constant'
}
model = MLPRegressor(**parameter_space)
The above model gave the desired output without any errors. For more information on MLPRegressor refer here.
Upvotes: 1