MLP中的K折叠交叉验证

时间:2019-10-08 09:56:51

标签: python cross-validation mlp

我想在我的数据集中的MLP Regressor中实现交叉验证。我想知道我的代码是否正确?这是交叉验证的方法吗?这是我的示例数据:

Wavelength    Phase_velocity     Shear_wave_velocity
1.50              202.69          240.73
1.68              192.72          240.73
1.79              205.54          240.73
17.08             218               229
16.73             243               269
17.72             245               269
16.72             212               253
17.26             214               253
........

这是我的代码:

import pandas as pd
import numpy as np
from sklearn.model_selection import KFold,train_test_split,cross_val_score
from sklearn.neural_network import MLPRegressor

df = pd.read_csv("0.5-1.csv")
df.head()

X = df[['wavelength', 'phase velocity']]
y = df['shear wave velocity']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)

print (len(X_train),len(X_test),len(y_train),len(y_test))

from sklearn.model_selection import KFold
kfold = KFold(n_splits=207, shuffle=True)
mlp = MLPRegressor(hidden_layer_sizes=(500,500,500), max_iter=2000, activation='relu')
mlp.fit(X_train,y_train)
results = cross_val_score(mlp, X_train, y_train, cv=kfold) #Cross validation on training set

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

print (mlp.predict(X_test)) #array_output
print (y_test)
print (mlp.score(X_test, y_test))
y_pred = mlp.predict(X_test)
from sklearn.metrics import mean_absolute_error
print (mean_absolute_error(y_test,y_pred))

from sklearn.metrics import mean_squared_error

from math import sqrt

rmse = sqrt(mean_squared_error(y_test,y_pred))

print(rmse)

0 个答案:

没有答案