generated from LufsX/Experiment-LogReg
This commit is contained in:
+14
-8
@@ -2,6 +2,7 @@ import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.model_selection import train_test_split, GridSearchCV
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
id = "202210423130"
|
||||
name = "董秋"
|
||||
@@ -85,19 +86,24 @@ if __name__ == "__main__":
|
||||
|
||||
# 定义参数网格
|
||||
param_grid = {
|
||||
'learning_rate': [0.01, 0.05, 0.1],
|
||||
'iterations': [500, 1000, 2000],
|
||||
'lambda_reg': [0.01, 0.1, 1]
|
||||
'C': [0.01, 0.1, 1.0, 10.0], # L2正则化强度的倒数
|
||||
'max_iter': [500, 1000, 2000]
|
||||
}
|
||||
|
||||
# 网格搜索中定义的参数
|
||||
best_params = {'learning_rate': 0.1, 'iterations': 1000, 'lambda_reg': 0.01}
|
||||
# 使用sklearn的LogisticRegression进行网格搜索
|
||||
model = LogisticRegression(penalty='l2', solver='lbfgs')
|
||||
grid_search = GridSearchCV(model, param_grid, cv=5)
|
||||
grid_search.fit(X_train, y_train)
|
||||
|
||||
# 获取最佳参数
|
||||
best_params = grid_search.best_params_
|
||||
print(f"最佳参数: {best_params}")
|
||||
|
||||
# 使用最佳参数重新训练模型
|
||||
weights = train_logreg(X_train, y_train,
|
||||
learning_rate=best_params['learning_rate'],
|
||||
iterations=best_params['iterations'],
|
||||
lambda_reg=best_params['lambda_reg'])
|
||||
learning_rate=0.1,
|
||||
iterations=best_params['max_iter'],
|
||||
lambda_reg=1 / best_params['C']) # 将C转换为lambda_reg
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
|
||||
Reference in New Issue
Block a user