generated from LufsX/Experiment-LogReg
105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
from sklearn.preprocessing import StandardScaler
|
|
from sklearn.model_selection import train_test_split, GridSearchCV
|
|
|
|
id = "202210423130"
|
|
name = "董秋"
|
|
|
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01):
|
|
"""
|
|
对率回归训练函数。
|
|
|
|
Args:
|
|
- X (numpy array): 特征数据。
|
|
- y (numpy array): 标签数据。
|
|
|
|
Returns:
|
|
- numpy array: 训练得到的权重。
|
|
"""
|
|
m, n = X.shape
|
|
weights = np.zeros(n)
|
|
|
|
for i in range(iterations):
|
|
z = np.dot(X, weights)
|
|
predictions = 1 / (1 + np.exp(-z))
|
|
gradient = np.dot(X.T, (predictions - y)) / m
|
|
|
|
# L2正则化:将正则项添加到梯度上
|
|
gradient += (lambda_reg / m) * weights
|
|
|
|
# 更新权重
|
|
weights -= learning_rate * gradient
|
|
|
|
# 动态调整学习率
|
|
learning_rate *= 0.95 # 每次迭代将学习率乘以0.99
|
|
|
|
return weights
|
|
|
|
def predict_logreg(inX, weights):
|
|
"""
|
|
对率回归预测函数。
|
|
|
|
Args:
|
|
- inX (numpy array): 输入待分类的样本。
|
|
- weights (numpy array): 训练得到的权重。
|
|
|
|
Returns:
|
|
- int: 预测的分类标签。
|
|
"""
|
|
z = np.dot(inX, weights)
|
|
prediction_prob = 1 / (1 + np.exp(-z))
|
|
return 1 if prediction_prob >= 0.5 else 0
|
|
|
|
def load_data(filepath):
|
|
data = []
|
|
labels = []
|
|
|
|
with open(filepath, 'r') as f:
|
|
for line in f:
|
|
values = line.strip().replace('?', '0').split()
|
|
try:
|
|
values = [float(v) for v in values]
|
|
if values[22] == 2 or values[22] == 3:
|
|
labels.append(0)
|
|
elif values[22] == 1:
|
|
labels.append(1)
|
|
else:
|
|
continue
|
|
values = [0 if np.isnan(v) else v for v in values]
|
|
data.append(values[:2] + values[3:23])
|
|
except ValueError:
|
|
continue
|
|
|
|
return np.array(data), np.array(labels)
|
|
|
|
def preprocess_data(X):
|
|
scaler = StandardScaler()
|
|
return scaler.fit_transform(X)
|
|
|
|
if __name__ == "__main__":
|
|
# 加载数据集
|
|
X, y = load_data("./data/horse-colic.data")
|
|
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 = {
|
|
'learning_rate': [0.01, 0.05, 0.1],
|
|
'iterations': [500, 1000, 2000],
|
|
'lambda_reg': [0.01, 0.1, 1]
|
|
}
|
|
|
|
# 网格搜索中定义的参数
|
|
best_params = {'learning_rate': 0.1, 'iterations': 1000, 'lambda_reg': 0.01}
|
|
|
|
# 使用最佳参数重新训练模型
|
|
weights = train_logreg(X_train, y_train,
|
|
learning_rate=best_params['learning_rate'],
|
|
iterations=best_params['iterations'],
|
|
lambda_reg=best_params['lambda_reg'])
|
|
|
|
# 测试单个数据
|
|
y_pred = predict_logreg(X_test[11], weights)
|
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|