我正在使用 scikit-learn 運(yùn)行一堆模型來解決分類問題。這是應(yīng)該完成所有運(yùn)行的代碼:for model_name, classifier, param_grid, cv, cv_name in tqdm(zip(model_names, classifiers, param_grids, cvs, cv_names)): pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', classifier)]) train_and_score_model(model_name, pipeline, param_grid, cv=cv)我的問題是,如何保留train_and_score_model函數(shù)的輸出?它返回一個(gè) cv 對(duì)象,即一個(gè)模型。我試圖做但我認(rèn)為不正確的是創(chuàng)建一個(gè)列表cv_names = ['dm_cv', 'lr_cv', 'knn_cv', 'svm_cv', 'dt_cv', 'rf_cv', 'nb_cv']并將每個(gè)列表設(shè)置為 for 循環(huán)運(yùn)行。那是cv_namefor 循環(huán)頭中的迭代器。我不認(rèn)為那是對(duì)的,因?yàn)槲也粫?huì)設(shè)置字符串而不是變量嗎?就像,我真正應(yīng)該擁有的是cv_names = [dm_cv, lr_cv, knn_cv, svm_cv, dt_cv, rf_cv, nb_cv],但我不認(rèn)為我可以擁有這樣的列表。我想到的另一種方法是將每個(gè)模型保存在字典中,其中的鍵是我上面概述的列表中的元素。我不知道我是否可以將模型作為字典值。這是我目前在 for 循環(huán)中運(yùn)行的笨重、重復(fù)的代碼:pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', classifier_dm)])dm_cv = train_and_score_model('Dummy Model', pipeline, param_grid_dm)pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', classifier_lr)])lr_cv = train_and_score_model('Logistic Regression', pipeline, param_grid_lr)pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', classifier_knn)])knn_cv = train_and_score_model('K Nearest Neighbors', pipeline, param_grid_knn)pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', classifier_svm)])svm_cv = train_and_score_model('Support Vector Machine', pipeline, param_grid_svm)pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('classifier', classifier_dt)])dt_cv = train_and_score_model('Decision Tree', pipeline, param_grid_dt)
查看完整描述