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

This commit is contained in:
2024-11-03 13:46:37 +08:00
parent 2377c3ad35
commit 8555213ddf
+26 -2
View File
@@ -21,11 +21,15 @@ import numpy as np
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423xxx"
name = "test_name"
id = "202210423124"
name = "刘子豪"
# 这里写自己的代码实现
def sigmoid(z):
return 1 / (1 + np.exp(-z)) #用于将数据转化成一个0 - 1之间的概率值
def train_logreg(X, y, other_params="你自己设置一些其它参数"):
"""
对率回归训练函数。
@@ -38,6 +42,14 @@ def train_logreg(X, y, other_params="你自己设置一些其它参数"):
Returns:
- numpy array: 训练得到的权重。
"""
m, n = X.shape
weights = np.zeros(n)
for _ in range(iterations):
predictions = sigmoid(np.dot(X, weights))
errors = y - predictions
gradient = np.dot(X.T, errors) / m
weights += learning_rate * gradient
return weights
def predict_logreg(inX, weights):
@@ -51,6 +63,8 @@ def predict_logreg(inX, weights):
Returns:
- int: 预测的分类标签。
"""
probability = sigmoid(np.dot(inX, weights))
return 1 if probability >= 0.5 else 0
# 这里也写自己的代码实现
@@ -65,6 +79,16 @@ def load_data(filepath):
- np.array: 特征数据。
- np.array: 标签数据。
"""
data = []
labels = []
with open(filepath, 'r') as file:
for line in file:
fields = line.strip().split()
if '?' in fields:
continue # 跳过包含缺失值的行
data.append([float(x) for x in fields[:-1]])
labels.append(int(fields[-1])) # 假设标签在最后一列
return np.array(data), np.array(labels)
if __name__ == "__main__":