MLP regression, error: TypeError: '<=' not supported between instances of 'numpy.ndarray' and 'int'
MLP regression, error: TypeError: '<=' not supported between instances of 'numpy.ndarray' and 'int'
我正在尝试拟合 MLP 回归模型。
我的数据看起来像
>>> 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.]])
我想先做 5 次交叉验证来找到最好的参数,这是我写的只适合 x_train 和 y_train 的 50 个,只是为了检查:
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])
但是,它显示此错误:
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'
x_train[1:50] 和 y_train[1:50] 都是 .. 我该如何解决这个问题?
谢谢
parameter_space
字典可以传递给估计器进行解包。为此,在传递时在参数前添加 2 颗星,例如 **parameter_space
。 parameter_space
变量必须具有受支持数据类型的参数。例如,
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)
以上模型给出了预期的输出,没有任何错误。有关 MLPRegressor 的更多信息,请参阅 here.
我正在尝试拟合 MLP 回归模型。
我的数据看起来像
>>> 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.]])
我想先做 5 次交叉验证来找到最好的参数,这是我写的只适合 x_train 和 y_train 的 50 个,只是为了检查:
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])
但是,它显示此错误:
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'
x_train[1:50] 和 y_train[1:50] 都是
谢谢
parameter_space
字典可以传递给估计器进行解包。为此,在传递时在参数前添加 2 颗星,例如 **parameter_space
。 parameter_space
变量必须具有受支持数据类型的参数。例如,
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)
以上模型给出了预期的输出,没有任何错误。有关 MLPRegressor 的更多信息,请参阅 here.