python - ValueError:折叠数必须是 Integral 类型。[数组([[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)
推荐阅读
- excel - 非直观的比较行为
- visual-studio-code - 在 vs 代码中调试时如何停止弹出活动栏?
- python - 将列表格式的字符串转换为整数列表格式
- julia - 元素明智的操作数组朱莉娅
- javascript - React useState钩子变量的破坏性排序?
- performance - x86 锁定与 SFENCE/MFENCE
- r - 将两个嵌套的 tibble 变量相乘
- python - 实施最小成本流算法或有条件的组分配
- vue.js - TailwindCSS 自定义主题不适用于 Nuxt.js
- javascript - 在发送带有文本的 HTML 电子邮件时出现图像中断