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

This commit is contained in:
2024-11-05 14:44:40 +08:00
parent 6441f223d9
commit 1f4253e0c6
+19 -10
View File
@@ -29,6 +29,9 @@ name = "Tanjunfeng"
# 这里写自己的代码实现
# 这里写自己的代码实现
import numpy as np
# 这里写自己的代码实现
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
"""
@@ -85,17 +88,23 @@ def load_data(filepath):
- np.array: 特征数据。
- np.array: 标签数据。
"""
data = []
with open(filepath, 'r') as file:
for line in file:
line = line.strip().split()
if '?' in line:
continue # 跳过包含缺失值的行
data.append([float(x) for x in line])
data = np.array(data)
# 使用 numpy.genfromtxt 读取数据
data = np.genfromtxt(
filepath,
delimiter=None, # 自动检测分隔符(包括空白符、制表符等)
dtype=float,
missing_values='?',
filling_values=np.nan,
autostrip=True
)
# 删除包含缺失值的行
data = data[~np.isnan(data).any(axis=1)]
# 将数据分为特征和标签
X = data[:, :-1]
y = data[:, -1]
# 将标签转换为二分类1: 存活,0: 死亡或安乐死
# 将标签转换为二分1: 存活,0: 死亡或安乐死
y = np.where(y == 1, 0, 1)
return X, y
@@ -113,7 +122,7 @@ if __name__ == "__main__":
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
print(f"预测结果: {y_pred}, 实际结果: {int(y_test[11])}")
# 可选:计算测试集上的准确率
correct = sum(predict_logreg(X_test[i], weights) == y_test[i] for i in range(len(X_test)))