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

This commit is contained in:
2024-11-02 15:07:32 +08:00
parent 8da0fd0717
commit 56ebe232e0
+15 -18
View File
@@ -29,7 +29,7 @@ name = "章程鸿"
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, KFold
from sklearn.model_selection import KFold
def sigmoid(z):
"""Sigmoid 激活函数。"""
@@ -69,14 +69,14 @@ def train_logreg(X, y, learning_rate=0.01, iterations=5000, lambda_reg=0.01, bat
return weights
def predict_logreg(inX, weights):
def predict_logreg(inX, weights, threshold=0.5):
"""逻辑回归预测函数。"""
z = np.dot(inX, weights)
prediction_prob = sigmoid(z)
return 1 if prediction_prob >= 0.5 else 0
return 1 if prediction_prob >= threshold else 0
def load_data(filepath):
"""加载数据并处理缺失值。"""
"""加载数据并处理缺失值与规划。"""
data = []
labels = []
@@ -103,12 +103,11 @@ def load_data(filepath):
data = np.array(data)
labels = np.array(labels)
return data, labels
def preprocess_data(X):
"""特征标准化。"""
# 数据标准化
scaler = StandardScaler()
return scaler.fit_transform(X)
data = scaler.fit_transform(data) # 在这里进行标准化处理
return data, labels
if __name__ == "__main__":
# 加载训练集
@@ -117,10 +116,6 @@ if __name__ == "__main__":
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test")
# 归一化数据
X_train = preprocess_data(X_train)
X_test = preprocess_data(X_test)
# K折交叉验证
k = 5
kf = KFold(n_splits=k, shuffle=True, random_state=42)
@@ -135,7 +130,7 @@ if __name__ == "__main__":
weights = train_logreg(X_train_kf, y_train_kf, learning_rate=0.01, iterations=5000, lambda_reg=0.01, batch_size=batch_size)
# 验证模型
val_predictions = [predict_logreg(X_val_kf[i], weights) for i in range(len(X_val_kf))]
val_predictions = [predict_logreg(X_val_kf[i], weights, threshold=0.5) for i in range(len(X_val_kf))]
accuracy = np.mean(val_predictions == y_val_kf)
accuracies.append(accuracy)
@@ -144,13 +139,15 @@ if __name__ == "__main__":
print(f"平均准确率: {np.mean(accuracies)}")
# 在测试集上测试模型
test_predictions = [predict_logreg(X_test[i], weights) for i in range(len(X_test))]
test_predictions = [predict_logreg(X_test[i], weights, threshold=0.5) for i in range(len(X_test))]
test_accuracy = np.mean(test_predictions == y_test)
print(f"测试集准确率: {test_accuracy}")
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
# 测试单个数据,输出错误分类
for i in range(len(X_test)):
y_pred = predict_logreg(X_test[i], weights, threshold=0.5)
if y_pred != y_test[i]:
print(f"第 {i} 次测试错误,数据 {X_test[i]} 被分类到 {y_pred} 实际是 {y_test[i]}")