Python 类型错误:'<';在';dict';和';int';使用gridsearch保存参数时
我正在使用gridsearch查找最佳参数,我希望保存这些参数并将其用于测试。保存best^参数并将其提供给algo时,我遇到了一个错误:Python 类型错误:'<';在';dict';和';int';使用gridsearch保存参数时,python,dictionary,knn,grid-search,gridsearchcv,Python,Dictionary,Knn,Grid Search,Gridsearchcv,我正在使用gridsearch查找最佳参数,我希望保存这些参数并将其用于测试。保存best^参数并将其提供给algo时,我遇到了一个错误: BEST PARAMETERS: {'metric': 'euclidean', 'n_neighbors': 5, 'weights': 'uniform'} BEST SCORE: 0.631578947368421 ---Filename in processed corpus_ix_test_FMC_ small Traceback (most
BEST PARAMETERS:
{'metric': 'euclidean', 'n_neighbors': 5, 'weights': 'uniform'}
BEST SCORE:
0.631578947368421
---Filename in processed corpus_ix_test_FMC_
small
Traceback (most recent call last):
File "training_and_cross_validate_tuning.py", line 161, in <module>
testing(testdir, f, root, dict_sm)
File "training_and_cross_validate_tuning.py", line 138, in testing
model_final.fit(Xtrain, Ytrain)
File "/home/g/kee/anaconda3/lib/python3.7/site-packages/sklearn/neighbors/base.py", line 917, in fit
return self._fit(X)
File "/home/g/ke/anaconda3/lib/python3.7/site-packages/sklearn/neighbors/base.py", line 238, in _fit
self.n_neighbors < self._fit_X.shape[0] // 2) and
TypeError: '<' not supported between instances of 'dict' and 'int'
Xtrain_emb, mdlname = get_flaubert_layer(data)
for mdl in dict_model:
print('Training model: ', mdl)
clf = GridSearchCV(estimator=dict_model[mdl], param_grid=parameters_dict[mdl], verbose = 1, n_jobs = -1, return_train_score=True)
best_model = clf.fit(Xtrain_emb, label)
print("Best parameters set found on development set:")
best_model_param = best_model.best_params_
print(type(best_model_param))
print('BEST PARAMETERS:\n', best_model_param)
best_model_score = best_model.best_score_
print('BEST SCORE:\n',best_model_score)
# create, train and test
return best_model_param, Xtrain_emb, label
def testing(path2, file, path, dict_model):
param, Xtrain, Ytrain = training_data(file, path, dict_model)
# test
filename_t = path2[21:-5] # corpus_or_AB_FMC
print("---Filename in processed", filename_t)
f = pd.read_excel(os.path.join(path2), sheet_name= 0)
data_idd = f.identifiant
Xtest = f.verbatim
Ytest = f.etiquette
Xtest_emb, mdlname = get_flaubert_layer(Xtest)
model_final = KNeighborsClassifier(param)
model_final.fit(Xtrain, Ytrain)
preds = model_final.predict(Xtest_emb)
print('The best model from grid search scores {:.5f} ROC AUC on the test set.'.format(roc_auc_score(Ytest, preds)))