3

我正在尝试将其拆分Pandas DataframeTrainand Test

例子:

Y = final_dataset['Species']
X = final_dataset[X_best_features]

X_train = X[0:120]
X_test = X[120:150]

y_train = Y[0:120]
y_test =  Y[120:150]

现在我Hyperopt用来获得最好的分类器。

estimator = hpsklearn.HyperoptEstimator(
    preprocessing=hpsklearn.components.any_preprocessing('pp'),
    classifier=hpsklearn.components.any_classifier('clf'),
    algo=hyperopt.tpe.suggest,
    trial_timeout=15.0, # seconds
    max_evals=15,
    )

fit_iterator = estimator.fit_iter(X_train,y_train)
fit_iterator.__next__()
plot_helper = hpsklearn.demo_support.PlotHelper(estimator,
                                                mintodate_ylim=(-.01, .05))
while len(estimator.trials.trials) < estimator.max_evals:
    fit_iterator.send(1) # -- try one more model
    plot_helper.post_iter()
plot_helper.post_loop()

# -- Model selection was done on a subset of the training data.
# -- Now that we've picked a model, train on all training data.
estimator.retrain_best_model_on_full_data(X_train, y_train)

print('Best preprocessing pipeline:')
print('')
for pp in estimator._best_preprocs:
    print(pp)
print('')
print('Best classifier:\n', estimator._best_learner)
test_predictions = estimator.predict(X_test)
acc_in_percent = 100 * np.mean(test_predictions == y_test)
print('')
print('Prediction accuracy in generalisation is %.1f%%' % acc_in_percent)

运行上述代码后,我收到此错误:

KeyError: '[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24\n 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\n 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74\n 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95] not in index'

谁能帮我解决这个错误??

谢谢

4

1 回答 1

1

我已经通过将XY转换为Numpy array. 似乎Hyperopt只需要 numpy 数组作为输入而不是Pandas Dataframe.

于 2018-11-16T05:37:05.623 回答