lambda_LR
是用于使用Logistic回归使用“ L2”来计算lambda值的函数。
为什么LogisticRegression()
在下面的最佳Lambda定义的GridSearchCV
中?
def lambda_LR1(X_train,y_train,X_test, y_test,vectorization):
# regularization penalty space
penalty = ['l2']
# regularization hyperparameter distribution using uniform distribution
C1 = uniform(loc=0, scale=4)
C = np.logspace(0, 4, 10)
# hyperparameter options
hp1 =dict(C=C, penalty=penalty)
hp = dict(C=C1, penalty=penalty)
# Scoring options
d = ['accuracy','precision','recall','f1']
for i in range(len(d)):
models_performence['Model'].append('Logistic Regression')
models_performence['Sampling'].append(Sampling)
models_performence['SearchCV'].append('GridSearchCV')
#print('for GridSearchCV')
p = d[i]
models_performence['Scoring Metrics'].append(p)
model1 = GridSearchCV(LogisticRegression(), hp1, scoring = p , n_jobs= -1)
best_model1=model1.fit(X_train, y_train)
Test_model_score=model1.score(X_test, y_test)
Train_model_score=model1.score(X_train, y_train)
models_performence['Train_model_score'].append(Train_model_score.mean())
models_performence['Test_model_score'].append(Test_model_score.mean())
models_performence['best panalty'].append('l2')
optimal_l1=best_model1.best_estimator_.get_params()['C']
models_performence['Best lambda'].append(optimal_l1)