Python 熊猫:切片熊猫数据帧时出现关键帧错误
我正在尝试将Python 熊猫:切片熊猫数据帧时出现关键帧错误,python,pandas,hyperopt,Python,Pandas,Hyperopt,我正在尝试将熊猫数据帧分为训练和测试 例如: 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=
熊猫数据帧
分为训练
和测试
例如:
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)
有人能帮我解决这个错误吗
谢谢我通过将
X
和Y
转换为Numpy数组
解决了上述问题。
似乎
Hyperopt
只将numpy数组作为输入,而不是Pandas Dataframe
您的数据集没有错误中提到的索引。因此,当您尝试访问它们时,它会引发错误。您能否提供引发错误的行号?
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'