ValueError: 折数必须是整数类型。 [array([[0.25 , 0.

我使用了极限学习机(ELM)模型进行回归预测,并使用K折交叉验证来验证模型的预测效果。但是在执行以下代码后,我得到了这个错误信息:

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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注