generated from LufsX/Experiment-LogReg
This commit is contained in:
+3
-6
@@ -35,16 +35,13 @@ def load_data(filepath):
|
|||||||
y = data[:, -1]
|
y = data[:, -1]
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
def train_logreg(X, y, initial_learning_rate=0.1, iterations=1000, decay_rate=0.01):
|
def train_logreg(X, y, learning_rate=0.1, iterations=1000):
|
||||||
"""训练逻辑回归模型,使用随机梯度下降和学习率衰减."""
|
"""训练逻辑回归模型,使用随机梯度下降."""
|
||||||
n_samples, n_features = X.shape
|
n_samples, n_features = X.shape
|
||||||
weights = np.zeros(n_features + 1) # 增加一位用于偏置
|
weights = np.zeros(n_features + 1) # 增加一位用于偏置
|
||||||
X = np.c_[X, np.ones((n_samples, 1))] # 增加偏置项
|
X = np.c_[X, np.ones((n_samples, 1))] # 增加偏置项
|
||||||
|
|
||||||
for i in range(iterations):
|
for _ in range(iterations):
|
||||||
# 更新学习率
|
|
||||||
learning_rate = initial_learning_rate / (1 + decay_rate * i)
|
|
||||||
|
|
||||||
random_index = np.random.randint(n_samples)
|
random_index = np.random.randint(n_samples)
|
||||||
X_i = X[random_index]
|
X_i = X[random_index]
|
||||||
y_i = y[random_index]
|
y_i = y[random_index]
|
||||||
|
|||||||
Reference in New Issue
Block a user