generated from LufsX/Experiment-LogReg
This commit is contained in:
+11
-6
@@ -27,8 +27,6 @@ id = "202210423629"
|
|||||||
name = "Tanjunfeng"
|
name = "Tanjunfeng"
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
# 这里写自己的代码实现
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1 / (1 + np.exp(-z))
|
return 1 / (1 + np.exp(-z))
|
||||||
|
|
||||||
@@ -72,7 +70,8 @@ def predict_logreg(inX, weights):
|
|||||||
"""
|
"""
|
||||||
z = np.dot(inX, weights)
|
z = np.dot(inX, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
return 1 if h > 0.5 else 0
|
# 调整分类阈值
|
||||||
|
return 1 if h > 0.6 else 0
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
"""
|
"""
|
||||||
@@ -110,8 +109,14 @@ 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)
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000)
|
||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
correct_count = 0
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
total_count = len(y_test)
|
||||||
|
for i in range(total_count):
|
||||||
|
y_pred = predict_logreg(X_test[i], weights)
|
||||||
|
if y_pred == y_test[i]:
|
||||||
|
correct_count += 1
|
||||||
|
accuracy = correct_count / total_count
|
||||||
|
print(f"准确率: {accuracy}")
|
||||||
Reference in New Issue
Block a user