generated from LufsX/Experiment-LogReg
This commit is contained in:
+4
-4
@@ -1,4 +1,3 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
|
||||
"""
|
||||
@@ -28,6 +27,7 @@ name = "雷武"
|
||||
# 这里写自己的代码实现
|
||||
def sigmoid(z):
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
@@ -43,7 +43,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n)
|
||||
bias = 0
|
||||
for i in range(iterations):
|
||||
for i in range(iterations):
|
||||
linear_model = np.dot(X, weights) + bias
|
||||
y_pred = sigmoid(linear_model)
|
||||
|
||||
@@ -96,8 +96,8 @@ if __name__ == "__main__":
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
weights, bias = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
y_pred = predict_logreg(X_test[11], weights, bias)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
|
||||
Reference in New Issue
Block a user