generated from LufsX/Experiment-LogReg
This commit is contained in:
+30
-26
@@ -1,6 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.impute import SimpleImputer
|
from sklearn.impute import SimpleImputer
|
||||||
from sklearn.feature_selection import SelectKBest, f_classif
|
from sklearn.feature_selection import SelectKBest, mutual_info_classif
|
||||||
|
from sklearn.preprocessing import RobustScaler
|
||||||
|
|
||||||
# 填写学号和姓名
|
# 填写学号和姓名
|
||||||
id = "202210423213"
|
id = "202210423213"
|
||||||
@@ -39,36 +40,26 @@ def load_data(filepath, k_features=20):
|
|||||||
labels = np.array(labels)
|
labels = np.array(labels)
|
||||||
|
|
||||||
# 使用SimpleImputer填充NaN
|
# 使用SimpleImputer填充NaN
|
||||||
imputer = SimpleImputer(strategy='mean')
|
imputer = SimpleImputer(strategy='median')
|
||||||
data = imputer.fit_transform(data)
|
data = imputer.fit_transform(data)
|
||||||
|
|
||||||
# 特征标准化,避免除零错误
|
# 使用RobustScaler进行数据标准化,减少异常值的影响
|
||||||
mean = data.mean(axis=0)
|
scaler = RobustScaler()
|
||||||
std_dev = data.std(axis=0)
|
data = scaler.fit_transform(data)
|
||||||
std_dev[std_dev == 0] = 1 # 如果标准差为0,将其设为1,避免除以零
|
|
||||||
data = (data - mean) / std_dev
|
|
||||||
|
|
||||||
# 特征选择
|
# 特征选择,使用互信息
|
||||||
selector = SelectKBest(f_classif, k=k_features)
|
selector = SelectKBest(mutual_info_classif, k=k_features)
|
||||||
data = selector.fit_transform(data, labels)
|
data = selector.fit_transform(data, labels)
|
||||||
|
|
||||||
return data, labels
|
return data, labels
|
||||||
|
|
||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
"""
|
"""Sigmoid激活函数"""
|
||||||
Sigmoid激活函数,用于对率回归模型。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
- z (numpy array): 输入数据。
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- numpy array: Sigmoid函数输出值。
|
|
||||||
"""
|
|
||||||
return 1 / (1 + np.exp(-z))
|
return 1 / (1 + np.exp(-z))
|
||||||
|
|
||||||
def train_logreg(X, y, initial_lr=0.005, iterations=5000, regularization=0.01, decay=0.0001):
|
def train_logreg(X, y, initial_lr=0.01, iterations=5000, regularization=0.02, decay=0.00005, patience=200):
|
||||||
"""
|
"""
|
||||||
对率回归训练函数,包含学习率衰减策略。
|
对率回归训练函数,包含学习率衰减和早停机制。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- X (numpy array): 特征数据。
|
- X (numpy array): 特征数据。
|
||||||
@@ -77,23 +68,36 @@ def train_logreg(X, y, initial_lr=0.005, iterations=5000, regularization=0.01, d
|
|||||||
- iterations (int): 迭代次数。
|
- iterations (int): 迭代次数。
|
||||||
- regularization (float): 正则化强度。
|
- regularization (float): 正则化强度。
|
||||||
- decay (float): 学习率衰减系数。
|
- decay (float): 学习率衰减系数。
|
||||||
|
- patience (int): 早停的等待次数。
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
- numpy array: 训练得到的权重。
|
- numpy array: 训练得到的权重。
|
||||||
"""
|
"""
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
weights = np.zeros(n)
|
||||||
learning_rate = initial_lr
|
best_loss = float('inf')
|
||||||
|
stop_counter = 0
|
||||||
|
|
||||||
for i in range(iterations):
|
for i in range(iterations):
|
||||||
|
# 前向传播和损失计算
|
||||||
z = np.dot(X, weights)
|
z = np.dot(X, weights)
|
||||||
predictions = sigmoid(z)
|
predictions = sigmoid(z)
|
||||||
|
loss = -np.mean(y * np.log(predictions + 1e-9) + (1 - y) * np.log(1 - predictions + 1e-9)) + regularization * np.sum(weights ** 2) / (2 * m)
|
||||||
|
|
||||||
|
# 早停机制
|
||||||
|
if loss < best_loss:
|
||||||
|
best_loss = loss
|
||||||
|
stop_counter = 0
|
||||||
|
else:
|
||||||
|
stop_counter += 1
|
||||||
|
if stop_counter >= patience:
|
||||||
|
print("早停:在第 {} 轮迭代停止".format(i))
|
||||||
|
break
|
||||||
|
|
||||||
|
# 反向传播更新权重
|
||||||
errors = y - predictions
|
errors = y - predictions
|
||||||
gradient = np.dot(X.T, errors) / m - regularization * weights / m
|
gradient = np.dot(X.T, errors) / m - regularization * weights / m
|
||||||
weights += learning_rate * gradient
|
weights += initial_lr * gradient / (1 + decay * i) # 学习率衰减
|
||||||
|
|
||||||
# 学习率衰减
|
|
||||||
learning_rate = initial_lr / (1 + decay * i)
|
|
||||||
|
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
@@ -119,7 +123,7 @@ if __name__ == "__main__":
|
|||||||
X_test, y_test = load_data("./data/horse-colic.test", k_features=20)
|
X_test, y_test = load_data("./data/horse-colic.test", k_features=20)
|
||||||
|
|
||||||
# 训练模型
|
# 训练模型
|
||||||
weights = train_logreg(X_train, y_train, initial_lr=0.005, iterations=5000, regularization=0.01, decay=0.0001)
|
weights = train_logreg(X_train, y_train, initial_lr=0.01, iterations=5000, regularization=0.02, decay=0.00005, patience=200)
|
||||||
|
|
||||||
# 模型评估
|
# 模型评估
|
||||||
correct_predictions = 0
|
correct_predictions = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user