From f702a594857899b51d9d3c71ebf02e14be01415e Mon Sep 17 00:00:00 2001 From: meixiaodong <3081674342@qq.com> Date: Sun, 3 Nov 2024 22:31:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 80 +++-------------------------------------------------- 1 file changed, 4 insertions(+), 76 deletions(-) diff --git a/function.py b/function.py index fc327e9..7da91dd 100644 --- a/function.py +++ b/function.py @@ -1,47 +1,5 @@ # function.py import numpy as np - -""" -实验说明: -- 自行编写实现数据加载与预测病马死亡的对率回归算法。 - -注意事项: -- 在 function.py 中实现对率回归算法。 -- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。 -- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。 - -测评环境: -- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz -- OS: Ubuntu 22.04.4 LTS -- Python: 3.10.12 -- numpy: 2.1.1 -""" - - -""" -请在这里填写学号和姓名,不填写即成绩无效。 -姓名请直接**填写中文** -例如: name = "张三" -""" -id = "202210423211" -name = "梅晓东" - - -# 这里写自己的代码实现 -def train_logreg(X, y, other_params="你自己设置一些其它参数"): - """ - 对率回归训练函数。 - - Args: - - X (numpy array): 特征数据。 - - y (numpy array): 标签数据。 - - other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。 - - Returns: - - numpy array: 训练得到的权重。 - """ -# function.py -import numpy as np import os from sklearn.preprocessing import StandardScaler @@ -67,8 +25,8 @@ from sklearn.preprocessing import StandardScaler 姓名请直接**填写中文** 例如: name = "张三" """ -id = "202210423110" -name = "罗浩" +id = "202210423211" +name = "梅晓东" import numpy as np @@ -121,7 +79,8 @@ def load_data(filepath): data, labels = [], [] print(f"加载数据中: {filepath}") - with open(filepath, 'r') as f: + with open(filepath, 'r') + as f: for line in f: line = line.strip().split() if len(line) >= 1 and line[-1].isdigit(): @@ -183,37 +142,6 @@ def load_data(filepath): y_pred = predict_logreg(X_test[11], weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") -def predict_logreg(inX, weights): - """ - 对率回归预测函数。 - - Args: - - inX (numpy array): 输入待分类的样本。 - - weights (numpy array): 训练得到的权重。 - - Returns: - - int: 预测的分类标签。 - """ - - -# 这里也写自己的代码实现 -def load_data(filepath): - """ - 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 - - Args: - - directory (str): 数据文件路径。 - - Returns: - - np.array: 特征数据。 - - np.array: 标签数据。 - """ - - -if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 - # 加载训练集 - X_train, y_train = load_data("./data/horse-colic.data") # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test")