generated from LufsX/Experiment-LogReg
This commit is contained in:
+22
-9
@@ -16,33 +16,46 @@ def sigmoid(z):
|
||||
"""
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
def sigmoid(z):
|
||||
"""
|
||||
Sigmoid 激活函数。
|
||||
"""
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
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)
|
||||
weights = np.random.randn(n) * 0.01 # 随机初始化权重,取较小值
|
||||
|
||||
for i in range(iterations):
|
||||
# 每次迭代打乱数据
|
||||
indices = np.random.permutation(m)
|
||||
X = X[indices]
|
||||
y = y[indices]
|
||||
|
||||
z = np.dot(X, weights)
|
||||
predictions = sigmoid(z)
|
||||
gradient = np.dot(X.T, (predictions - y)) / m
|
||||
|
||||
|
||||
# L2正则化
|
||||
gradient += (lambda_reg / m) * weights
|
||||
|
||||
# 更新权重
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
# 动态调整学习率,逐渐减小
|
||||
learning_rate *= 0.99 # 每次迭代将学习率乘以0.99
|
||||
learning_rate *= 0.99
|
||||
|
||||
return weights
|
||||
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""
|
||||
逻辑回归预测函数。
|
||||
|
||||
Reference in New Issue
Block a user