generated from LufsX/Experiment-LogReg
This commit is contained in:
+9
-35
@@ -1,13 +1,13 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
|
||||
from sklearn.model_selection import train_test_split, GridSearchCV
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
id = "202210423130"
|
||||
name = "董秋"
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.05, iterations=1000, lambda_reg=0.01):
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1500, lambda_reg=0.01):
|
||||
"""
|
||||
逻辑回归训练函数。
|
||||
|
||||
@@ -86,19 +86,7 @@ def preprocess_data(X):
|
||||
# 标准化
|
||||
scaler = StandardScaler()
|
||||
X_scaled = scaler.fit_transform(X)
|
||||
|
||||
# 创建多项式特征
|
||||
poly = PolynomialFeatures(degree=2, interaction_only=False, include_bias=False)
|
||||
X_poly = poly.fit_transform(X_scaled)
|
||||
|
||||
# 例如,可以对某个特征进行分箱处理
|
||||
age_bins = pd.cut(X_scaled[:, 0], bins=[0, 30, 60, 100], labels=["年轻", "中年", "老年"])
|
||||
age_dummies = pd.get_dummies(age_bins, drop_first=True)
|
||||
|
||||
# 将独热编码的结果与原特征组合
|
||||
X_combined = np.hstack((X_poly, age_dummies.values))
|
||||
|
||||
return X_combined
|
||||
return X_scaled
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载数据集
|
||||
@@ -106,26 +94,12 @@ if __name__ == "__main__":
|
||||
X = preprocess_data(X)
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
# 定义参数网格
|
||||
param_grid = {
|
||||
'C': [0.01, 0.1, 1.0, 10.0], # L2正则化强度的倒数
|
||||
'max_iter': [500, 1000, 2000]
|
||||
}
|
||||
# 使用sklearn的LogisticRegression进行训练
|
||||
model = LogisticRegression(penalty='l2', solver='lbfgs', max_iter=2000)
|
||||
model.fit(X_train, y_train)
|
||||
|
||||
# 使用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=0.1,
|
||||
iterations=best_params['max_iter'],
|
||||
lambda_reg=1 / best_params['C']) # 将C转换为lambda_reg
|
||||
# 获取权重
|
||||
weights = model.coef_[0] # 只获取第一类的权重
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
|
||||
Reference in New Issue
Block a user