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

This commit is contained in:
cy
2024-10-26 12:36:12 +08:00
parent f399ea6269
commit b0969e36df
+19 -13
View File
@@ -27,15 +27,15 @@ name = "程悦"
def sigmoid(z):
return 1.0 / (1 + np.exp(-z))
# 这里写自己的代码实现
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=256):
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
"""
对率回归训练函数。
Args:
- X (numpy array): 特征数据。
- y (numpy array): 标签数据。
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。
- learning_rate (float): 学习率。
- iterations (int): 迭代次数。
Returns:
- numpy array: 训练得到的权重。
@@ -67,10 +67,10 @@ def predict_logreg(inX, weights):
# 这里也写自己的代码实现
def load_data(filepath):
"""
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
从文件中加载数据,处理缺失值,并修改指定的特征。
Args:
- directory (str): 数据文件路径。
- filepath (str): 数据文件路径。
Returns:
- np.array: 特征数据。
@@ -82,19 +82,25 @@ def load_data(filepath):
lines = f.readlines()
for line in lines:
line = line.strip().split()
if '?' not in line:
line_data = [1] + [0 if x == '?' else float(x) for x in [line[i] for i in range(len(line) - 1) if i not in [2, 23, 24, 25, 26, 27]]]
target = int(line[22])
if target in [2, 3]:
target = 0
if len(line) >= 1 and line[-1].isdigit():
# 处理缺失值,用0填充
line_data = [1 if i == 0 else (0 if x == '?' else float(x)) for i, x in enumerate(line[:-1])]
# 修改特征23(索引为22,因为索引从0开始)
if int(line[22]) in [2, 3]:
line_data[22] = 0 # 将2和3合并为0(表示死亡)
else:
target = 1
line_data[22] = 1 # 1表示存活
# 去掉特征3(索引为2)和特征24到28(索引为23到28)
line_data = line_data[:2] + line_data[3:23] + line_data[29:]
data.append(line_data)
labels.append(target)
labels.append(int(line[-1]))
return np.array(data), np.array(labels)
if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")