KeyError: "None of [Int64Index([112, 113,..121,\n .\n 58, 559],\n dtype='int64', length=448)] are in the [columns]"
KeyError: "None of [Int64Index([112, 113,..121,\n .\n 58, 559],\n dtype='int64', length=448)] are in the [columns]"
我使用极限学习机 (ELM) 模型进行预测。我使用 K-fold 来验证模型预测。但是执行以下代码后,我收到此消息错误:
KeyError: "None of [Int64Index([112, 113, 114, 115, 116, 117, 118, 119, 120, 121,\n ...\n 550, 551, 552, 553, 554, 555, 556, 557, 558, 559],\n dtype='int64', length=448)] are in the [columns]"
我该如何解决这个问题?怎么了?代码:
dataset = pd.read_excel("un.xls")
X=dataset.iloc[:,:-1]
y=dataset.iloc[:,-1:]
#----------Scaler----------
scaler = MinMaxScaler()
scaler_X = MinMaxScaler()
X=scaler.fit_transform(X)
#---------------------- Divided the datset----------------------
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
# 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
# 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
#----------------------------(input size)-------------
input_size = X_train.shape[1]
hidden_size = 23
#---------------------------(To fix the RESULT)-------
seed =22 # 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_folds)
消息错误:
raise KeyError(f[{key}] 的None 在[{axis_name}]")
KeyError: "None of [Int64Index([112, 113, 114, 115, 116, 117, 118, 119, 120, 121,\n ...\n 550, 551, 552 , 553, 554, 555, 556, 557, 558, 559],\n dtype='int64', length=448)] 在[列]
您应该使用 train_test_split()
或 KFold()
之一来拆分数据。 不是两者
正如 KFold()
的 documentation 所说:
您应该只在 KFold.split()
中使用 X
。所以使用这个:
kfolds = KFold(n_splits=5, random_state=16, shuffle=False)
for train_index, test_index in kfolds.split(X):
X_train_folds, X_test_folds = X[train_index], X[test_index]
y_train_folds, y_test_folds = y[train_index], y[test_index]
此外,删除所有 X_train
和 y_train
,因为不需要。
input_size = X.shape[1]
def relu(x):
return np.maximum(x, 0)
output_weights = np.dot(pinv2(hidden_nodes(X_train_folds)), y_train_folds)
如果代码仍然由于KFold()
而导致错误,您应该考虑使用train_test_split()
并将KFold()
的训练、测试变量替换为train_test_split()
的变量
对于train_test_split()
:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
input_size = X_train.shape[1]
def relu(x):
return np.maximum(x, 0)
output_weights = np.dot(pinv2(hidden_nodes(X_train)), y_train)
prediction = predict(X_test)
我使用极限学习机 (ELM) 模型进行预测。我使用 K-fold 来验证模型预测。但是执行以下代码后,我收到此消息错误:
KeyError: "None of [Int64Index([112, 113, 114, 115, 116, 117, 118, 119, 120, 121,\n ...\n 550, 551, 552, 553, 554, 555, 556, 557, 558, 559],\n dtype='int64', length=448)] are in the [columns]"
我该如何解决这个问题?怎么了?代码:
dataset = pd.read_excel("un.xls")
X=dataset.iloc[:,:-1]
y=dataset.iloc[:,-1:]
#----------Scaler----------
scaler = MinMaxScaler()
scaler_X = MinMaxScaler()
X=scaler.fit_transform(X)
#---------------------- Divided the datset----------------------
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
# 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
# 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
#----------------------------(input size)-------------
input_size = X_train.shape[1]
hidden_size = 23
#---------------------------(To fix the RESULT)-------
seed =22 # 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_folds)
消息错误:
raise KeyError(f[{key}] 的None 在[{axis_name}]")
KeyError: "None of [Int64Index([112, 113, 114, 115, 116, 117, 118, 119, 120, 121,\n ...\n 550, 551, 552 , 553, 554, 555, 556, 557, 558, 559],\n dtype='int64', length=448)] 在[列]
您应该使用 train_test_split()
或 KFold()
之一来拆分数据。 不是两者
正如 KFold()
的 documentation 所说:
您应该只在 KFold.split()
中使用 X
。所以使用这个:
kfolds = KFold(n_splits=5, random_state=16, shuffle=False)
for train_index, test_index in kfolds.split(X):
X_train_folds, X_test_folds = X[train_index], X[test_index]
y_train_folds, y_test_folds = y[train_index], y[test_index]
此外,删除所有 X_train
和 y_train
,因为不需要。
input_size = X.shape[1]
def relu(x):
return np.maximum(x, 0)
output_weights = np.dot(pinv2(hidden_nodes(X_train_folds)), y_train_folds)
如果代码仍然由于KFold()
而导致错误,您应该考虑使用train_test_split()
并将KFold()
的训练、测试变量替换为train_test_split()
的变量
对于train_test_split()
:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
input_size = X_train.shape[1]
def relu(x):
return np.maximum(x, 0)
output_weights = np.dot(pinv2(hidden_nodes(X_train)), y_train)
prediction = predict(X_test)