我使用了极限学习机(ELM)模型进行预测。我使用了训练数据集和测试数据集,并希望使用交叉验证(K-fold)来验证模型。如何添加代码以进行交叉验证(K-fold)?
#------------------------------import data--------------
train = pd.read_excel('nametrain.xlsx')
test = pd.read_excel('nametest.xlsx')
#--------------------------------(scaler data)------------
scaler = MinMaxScaler()
scaler_X = MinMaxScaler()
scaler_Y = MinMaxScaler()
# fit_transform for training data:
X_train = scaler_X.fit_transform(train.values[:,:-1])
y_train = scaler_Y.fit_transform(train.values[:,-1:])
X_test = scaler_X.transform(test.values[:,:-1])
y_test = scaler_Y.transform(test.values[:,-1:])
#----------------------------(input size)-------------
input_size = X_train.shape[1]
#---------------------------(Number of neurons)-------
hidden_size = 17
#---------------------------(To fix the RESULT)-------
seed =16 # can be any number, and the exact value does not matter
np. random.seed(seed)
#---------------------------(weights & biases)------------
input_weights = np.random.normal(size=[input_size,hidden_size])
biases = np.random.normal(size=[hidden_size])
#----------------------(Activation Function)----------
def relu(x):
return np.maximum(x, 0, x)
#--------------------------(Calculations)----------
def hidden_nodes(X):
G = np.dot(X, input_weights)
G = G + biases
H = relu(G)
return H
#Output weights
output_weights = np.dot(pinv2(hidden_nodes(X_train)), y_train)
#------------------------(Def prediction)---------
def predict(X):
out = hidden_nodes(X)
out = np.dot(out, output_weights)
return out
#------------------------------------(Make_PREDICTION)--------------
prediction = predict(X_test)
unscaler_prediction=prediction*(4.5862069-1.23333333)+1.23333333
unscaler_y_test=y_test*(4.5862069-1.23333333)+1.23333333
#--------------------------(Calculate metrics)---------------
mse = metrics.mean_squared_error(y_test, prediction)
rmse = np.sqrt(mse) # or mse**(0.5) 发布于 2021-08-10 18:07:49
您可以使用sklearn的KFold, GroupKFold或RepeatedKFold
# Splits dataset into k consecutive folds (without shuffling by default).
kfolds = KFold(n_splits=5, random_state=16, shuffle=False)
for train_index, test_index in kfolds.split(X_train, y_train):
X_train_folds, X_test_folds = X_train[train_index], X_train[test_index]
y_train_folds, y_test_folds = y_train[train_index], y_train[test_index]
# put all code in the for loop so that for every set of (X_train_folds, y_train_folds), the model is fitted.
# call predict() for corresponding set of X_test_folds
prediction = predict(X_test_folds)https://stackoverflow.com/questions/68683666
复制相似问题