更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 43s

This commit is contained in:
2024-11-07 17:12:26 +08:00
parent f5aa7b86c7
commit 49fe7874d4
+38 -21
View File
@@ -27,11 +27,14 @@ name = "章程鸿"
# 这里写自己的代码实现 # 这里写自己的代码实现
import numpy as np import numpy as np
from sklearn.metrics import accuracy_score
# 定义sigmoid函数
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, iterations): # 训练逻辑回归模型
def train_logreg(X, y, learning_rate, iterations=1000):
m, n = X.shape m, n = X.shape
weights = np.zeros(n) weights = np.zeros(n)
bias = 0 bias = 0
@@ -42,13 +45,15 @@ def train_logreg(X, y, learning_rate, iterations):
db = (1/m) * np.sum(h - y) db = (1/m) * np.sum(h - y)
weights -= learning_rate * dw weights -= learning_rate * dw
bias -= learning_rate * db bias -= learning_rate * db
return weights return weights, bias
def predict_logreg(inX, weights): # 使用逻辑回归模型进行预测
z = np.dot(inX, weights) def predict_logreg(inX, weights, bias):
z = np.dot(inX, weights) + bias
h = sigmoid(z) h = sigmoid(z)
return 1 if h >= 0.5 else 0 return 1 if h >= 0.5 else 0
# 加载数据
def load_data(filepath): def load_data(filepath):
data = [] data = []
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
@@ -64,24 +69,36 @@ def load_data(filepath):
y = data[:, -1] y = data[:, -1]
return X, y return X, y
# 寻找最佳学习率
def find_best_learning_rate(X, y, learning_rates, iterations=1000):
best_accuracy = 0
best_learning_rate = None
best_weights = None
best_bias = None
for rate in learning_rates:
weights, bias = train_logreg(X, y, rate, iterations)
y_pred = np.array([predict_logreg(x, weights, bias) for x in X])
accuracy = accuracy_score(y, y_pred)
if accuracy > best_accuracy:
best_accuracy = accuracy
best_learning_rate = rate
best_weights = weights
best_bias = bias
return best_learning_rate, best_accuracy, best_weights, best_bias
if __name__ == "__main__": if __name__ == "__main__":
X_train, y_train = load_data("./data/horse-colic.data") X_train, y_train = load_data("./data/horse-colic.data")
X_test, y_test = load_data("./data/horse-colic.test") X_test, y_test = load_data("./data/horse-colic.test")
best_learning_rate = None
best_accuracy = 0 # 定义要尝试的学习率列表
learning_rates = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01] learning_rates = np.linspace(0.001, 0.1, 50)
iterations = 1000
for lr in learning_rates: # 寻找最佳学习率
weights = train_logreg(X_train, y_train, learning_rate=lr, iterations=iterations) best_learning_rate, best_accuracy, best_weights, best_bias = find_best_learning_rate(X_train, y_train, learning_rates)
correct_count = 0
for i in range(len(X_test)): print(f"最佳学习率: {best_learning_rate}")
y_pred = predict_logreg(X_test[i], weights) print(f"最佳准确率: {best_accuracy}")
if y_pred == y_test[i]:
correct_count += 1 # 使用最佳权重和偏置进行预测
accuracy = correct_count / len(X_test) y_pred = predict_logreg(X_test[11], best_weights, best_bias)
if accuracy > best_accuracy:
best_accuracy = accuracy
best_learning_rate = lr
best_weights = train_logreg(X_train, y_train, learning_rate=best_learning_rate, iterations=iterations)
y_pred = predict_logreg(X_test[11], best_weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")