首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >极限学习机(ELM)的交叉验证(K-fold)

极限学习机(ELM)的交叉验证(K-fold)
EN

Stack Overflow用户
提问于 2021-08-06 14:55:55
回答 1查看 136关注 0票数 0

我使用了极限学习机(ELM)模型进行预测。我使用了训练数据集和测试数据集,并希望使用交叉验证(K-fold)来验证模型。如何添加代码以进行交叉验证(K-fold)?

代码语言:javascript
复制
#------------------------------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)  
EN

回答 1

Stack Overflow用户

发布于 2021-08-10 18:07:49

您可以使用sklearn的KFold, GroupKFoldRepeatedKFold

代码语言:javascript
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68683666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档