我需要对下面列出的 Logistic 回归分类器的参数执行网格搜索,使用召回进行评分和交叉验证 3 次。
数据位于 csv 文件 (11,1 MB) 中,此下载链接为:https ://drive.google.com/file/d/1cQFp7HteaaL37CefsbMNuHqPzkINCVzs/view?usp=sharing
我有grid_values = {'gamma':[0.01, 0.1, 1, 10, 100]}
我需要在逻辑回归中应用惩罚 L1 e L2
我无法验证分数是否会运行,因为我有以下错误:估计器 LogisticRegression 的参数 gamma 无效。使用 来检查可用参数列表estimator.get_params().keys()
。
这是我的代码:
from sklearn.model_selection import train_test_split
df = pd.read_csv('fraud_data.csv')
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
def LogisticR_penalty():
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
grid_values = {'gamma':[0.01, 0.1, 1, 10, 100]}
#train de model with many parameters for "C" and penalty='l1'
lr_l1 = LogisticRegression(penalty='l1')
grid_lr_l1 = GridSearchCV(lr_l1, param_grid = grid_values, cv=3, scoring = 'recall')
grid_lr_l1.fit(X_train, y_train)
y_decision_fn_scores_recall = grid_lr_l1.decision_function(X_test)
lr_l2 = LogisticRegression(penalty='l2')
grid_lr_l2 = GridSearchCV(lr_l2, param_grid = grid_values, cv=3 , scoring = 'recall')
grid_lr_l2.fit(X_train, y_train)
y_decision_fn_scores_recall = grid_lr_l2.decision_function(X_test)
#The precision, recall, and accuracy scores for every combination
#of the parameters in param_grid are stored in cv_results_
results = pd.DataFrame()
results['l1_results'] = pd.DataFrame(grid_lr_l1.cv_results_)
results['l1_results'] = results['l2_results'].sort_values(by='mean_test_precision_score', ascending=False)
results['l2_results'] = pd.DataFrame(grid_lr_l2.cv_results_)
results['l2_results'] = results['l2_results'].sort_values(by='mean_test_precision_score', ascending=False)
return results
LogisticR_penalty()
我从 .cv_results_ 中期望,我应该在这里可用的每个参数组合的平均测试分数:mean_test_precision_score 但不确定
输出为:ValueError:估计器 LogisticRegression 的参数 gamma 无效。使用 来检查可用参数列表estimator.get_params().keys()
。