ValueError: The number of folds must be of Integral type. [array([[0.25 , 0.

ValueError: The number of folds must be of Integral type. [array([[0.25 , 0.

我使用极限学习机 (ELM) 模型进行回归预测。我使用 K-fold 来验证模型预测。但是执行以下代码后,我收到此消息错误:

ValueError: The number of folds must be of Integral type. [array([[0.25      , 0. ........

而且当我打印预测时,它没有打印出来。

我的代码:

dataset = pd.read_excel("ar.xls")

X=dataset.iloc[:,:-1]
y=dataset.iloc[:,-1:]

#----------Scaler----------
scaler = MinMaxScaler(feature_range=(0, 1))
X=scaler.fit_transform(X)


#---------------------- Divided the datset----------------------

kfolds = KFold(train_test_split(X, y) ,n_splits=5, random_state=16, shuffle=False)   
for train_index, test_index in kfolds.split(X):
    
    X_train_split, X_test_split = X[train_index], X[test_index]
    y_train_split, y_test_split = y[train_index], y[test_index]
      
#------------------------INPUT------------------

input_size = X.shape[1]

#---------------------------(Number of neurons)-------
hidden_size = 26

#---------------------------(To fix the RESULT)-------
seed =26   # 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)), y)
output_weights = np.dot(pinv2(hidden_nodes(X_train_split)), y_train_split)


#------------------------(Def prediction)---------
def predict(X):
    out = hidden_nodes(X)
    out = np.dot(out, output_weights)
    return out

#------------------------------------(Make_PREDICTION)--------------
prediction = predict(X_test_split)
print(prediction)

KFold 将第一个参数视为 n_splits ,可以在此处看到 class sklearn.model_selection.KFold(n_splits=5, *, shuffle=False, random_state=None) 并且您正在传递 train_test_split(X, y) 代替它,因此您得到了这个错误。另外,在下面的循环中

for train_index, test_index in kfolds.split(X):
    
    X_train_split, X_test_split = X[train_index], X[test_index]
    y_train_split, y_test_split = y[train_index], y[test_index]

您正在覆盖您的变量,因此最后您将只考虑最后一次折叠值。正确的方法如下

kfolds = KFold(n_splits=5, random_state=16, shuffle=False)  

train_folds_idx = []
valid_folds_idx = []

for train_index, valid_index in kfolds.split(dataset.index):
    train_folds_idx.append(train_index)
    valid_folds_idx.append(valid_index)