# 多层感知器, 神经网络
from sklearn.neural_network import MLPRegressor
# 设置交叉验证集的折数
# 时间序列分割
from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(max_train_size=None, n_splits=13)
def cv_mae_(model, train_X, train_y):
val_loss = -cross_val_score(model, train_X, train_y, scoring="neg_mean_absolute_error", cv=tscv)
print('val loss is: {0}'.format(val_loss))
import matplotlib.pyplot as plt
plt.plot(val_loss, marker = 'o')
plt.show()
cv_mae = np.mean(-cross_val_score(model, train_X, train_y, scoring="neg_mean_absolute_error", cv = tscv))
return cv_mae
def build_model(train_X, train_y):
init_params = {
# 三层,神经元个数依次是 26 、6、10
'hidden_layer_sizes': (26, 6, 10),
'solver': 'lbfgs',
'max_iter': 20000,
'early_stopping': False,
'shuffle': False,
'verbose': True,
'random_state': 1,
}
mult_mlp = MLPRegressor(**init_params)
cv_mae_mlp = cv_mae_(mult_mlp, train_X, train_y)
mult_mlp = mult_mlp.fit(train_X, train_y)
python 创建多层感知机,神经网络
最新推荐文章于 2022-01-16 23:08:43 发布