Codrin Mironiuc
Codrin Mironiuc

Reputation: 121

Is is possible to use np.array() with lists as inputs?

I was building my MLP algorithm and I was trying to use k-fold cross validation in combination with grid search to find the best combination of hidden layers/nodes.

I initially tried simply varying alpha levels, and it worked, and I used:

from sklearn.model_selection import cross_val_score
import numpy as np
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt 
import mglearn 

mlp = MLPClassifier()
param_grid = {'alpha': np.arange(0,1,0.5)}
knn_gscv = GridSearchCV(mlp, param_grid, cv=5)

#fit model to data
knn_gscv.fit(X, y)

#check top performing n_neighbors value
print("best alpha value is",knn_gscv.best_params_)
#check mean score for the top performing value of n_neighbors
print("best score best alpha",knn_gscv.best_score_)

This worked. But now I was trying to vary the number of hidden layers and nodes, and tried this:

from sklearn.model_selection import cross_val_score
import numpy as np
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt 
import mglearn 

mlp = MLPClassifier()
param_grid = {'hidden_layer_sizes': np.arange([10,10],[20,20],[30,30])}
knn_gscv = GridSearchCV(mlp, param_grid, cv=5)

#fit model to data
knn_gscv.fit(X, y)

#check top performing n_neighbors value
print("best alpha value is",knn_gscv.best_params_)
#check mean score for the top performing value of n_neighbors
print("best score best alpha",knn_gscv.best_score_)

But I get an error message. I think it is because np.array() does not work well with lists as inputs. But I still believe I should use np.array since this is the easiest way to implement it with grid search. Is there a way to go around it?

Upvotes: 1

Views: 326

Answers (1)

seralouk
seralouk

Reputation: 33147

First of all,

np.arange([10,10],[20,20],[30,30]) will never work.

or even np.arange([[10,10],[20,20],[30,30]]) as suggested in the comments.

Both will raise:

TypeError: unsupported operand type(s) for -: 'list' and 'list'


Short answer

For 'hidden_layer_sizes', you need a list of tuples.

E.g. param_grid = {'hidden_layer_sizes': [(10,10), (20,20)]}


Long answer

To make a range of tuples use something like this:

start=10
stop=20
step = 5

param_grid = {'hidden_layer_sizes': 
               [(n, min(n+step, stop)) for n in range(start, stop, step)]}

param_grid
Out[29]: {'hidden_layer_sizes': [(10, 15), (15, 20)]}

Upvotes: 2

Related Questions