这个答案似乎正是我需要为回归器而不是分类器提供的。
https://stackoverflow.com/a/46913459/9726897
我做了非常小的修改,以修改sascha通过链接提供的代码,如下所示。我认为将其用于MLPRegressior会非常简单...但是我收到一条错误消息,我不知道如何解决。任何帮助将不胜感激:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
estimator_reg = MLPRegressor(
solver='adam',
activation='relu',
learning_rate='adaptive',
learning_rate_init=.01,
hidden_layer_sizes=[100],
alpha=0.01,
max_iter=1000,
random_state=42,
tol=0.0001,
early_stopping=False,
warm_start=True,
beta_1=0.7,
beta_2=0.98,
epsilon=0.0000000001,
verbose=10,
)
""" Home-made mini-batch learning
-> not to be used in out-of-core setting!
"""
N_TRAIN_SAMPLES = train_data.shape[0]
N_EPOCHS = 25
N_BATCH = 128
scores_train = []
scores_test = []
# EPOCH
epoch = 0
while epoch < N_EPOCHS:
print('epoch: ', epoch)
# SHUFFLING
random_perm = np.random.permutation(train_data.shape[0])
mini_batch_index = 0
while True:
# MINI-BATCH
indices = random_perm[mini_batch_index:mini_batch_index + N_BATCH]
estimator_reg.partial_fit(train_data[indices], train_labels[indices])
mini_batch_index += N_BATCH
if mini_batch_index >= N_TRAIN_SAMPLES:
break
# SCORE TRAIN
scores_train.append(estimator_reg.score(train_data, train_labels))
# SCORE TEST
scores_test.append(estimator_reg.score(test_data, test_labels))
epoch += 1
""" Plot """
fig, ax = plt.subplots(2, sharex=True, sharey=True)
ax[0].plot(scores_train)
ax[0].set_title('Train')
ax[1].plot(scores_test)
ax[1].set_title('Test')
fig.suptitle("Accuracy over epochs", fontsize=14)
plt.show()
,我收到此错误:
KeyError跟踪(最近一次通话)
在()
---> 46 estimator_reg.partial_fit(train_data [indices],train_labels [indices])
.......
.......
KeyError:'[789 1493 353 33 1011 2029 1696 1649 653 1648 22 2477 2120 1000 \ n 2481 2448 1704 1962 2291 1995 2085 710 967 1839 461 504 1650 2166 \ n 584513676676 1196 1621 2109 766 2012 1017 1636 1286 448 2049 1791 \ n 141 1168 1249 159 2061 2456 431 1799 2249 2379 1169 1044 1010 120 \ n 2503 316 1070 671 1005 2164 975 2371 811 1555 1193 1316 487 1867 \ n 1262 1395 135 2224 32 1509 2132 997 263 233 1614 2317 1432 49 \ n 1251 2227 2536 1955 359 650 2287 792 1900 606 763 1837 742 965 \ n 1190 53 910 2486 738 103 1965 99 1084 123 1061 806 384 2261 \ n 2284 2114 360 1075 1479 1446 455 2294 221 1221 856 10979 2780 189 \ n 2153 1183 ]不在索引中>
答案 0 :(得分:0)
我猜您的索引不在(0,N_TRAIN_SAMPLES
)范围内。
如果删除或过滤了一些行,或者从开头开始包含的索引不在该范围内,则可能会发生这种情况。
尝试更改此行:
random_perm = np.random.permutation(train_data.shape[0])
对此:
random_perm = np.random.permutation(train_data.index.values)