generated from LufsX/Experiment-LogReg
This commit is contained in:
+8
-4
@@ -31,7 +31,7 @@ import numpy as np
|
|||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1 / (1 + np.exp(-z))
|
return 1 / (1 + np.exp(-z))
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, init_weights=None):
|
||||||
"""
|
"""
|
||||||
对率回归训练函数。
|
对率回归训练函数。
|
||||||
|
|
||||||
@@ -41,12 +41,16 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32):
|
|||||||
- learning_rate (float): 学习率,默认为 0.01。
|
- learning_rate (float): 学习率,默认为 0.01。
|
||||||
- iterations (int): 迭代次数,默认为 1000。
|
- iterations (int): 迭代次数,默认为 1000。
|
||||||
- batch_size (int): 小批量大小,默认为 32。
|
- batch_size (int): 小批量大小,默认为 32。
|
||||||
|
- init_weights (numpy array): 权重初始值,默认为 None(即全零初始化)。
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
- numpy array: 训练得到的权重。
|
- numpy array: 训练得到的权重。
|
||||||
"""
|
"""
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
if init_weights is None:
|
||||||
|
weights = np.zeros(n)
|
||||||
|
else:
|
||||||
|
weights = init_weights
|
||||||
bias = 0
|
bias = 0
|
||||||
for _ in range(iterations):
|
for _ in range(iterations):
|
||||||
indices = np.random.permutation(m)
|
indices = np.random.permutation(m)
|
||||||
@@ -113,9 +117,9 @@ if __name__ == "__main__":
|
|||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
# 训练对率回归模型
|
# 训练对率回归模型
|
||||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32) # 修改批量大小
|
init_weights = np.random.randn(X_train.shape[1]) # 随机初始化权重
|
||||||
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32, init_weights=init_weights)
|
||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
y_pred = predict_logreg(X_test[11], weights)
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user