From e2435a33132405e14c496a64ad6efff7f3a99be7 Mon Sep 17 00:00:00 2001 From: hao <19382120447@163.com> Date: Sat, 2 Nov 2024 17:10:06 +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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/function.py b/function.py index 3dca7e5..0a6eb28 100644 --- a/function.py +++ b/function.py @@ -26,7 +26,7 @@ name = "黎世浩" # 这里写自己的代码实现 -def train_logreg(X, y, random_state="114514"): +def train_logreg(X, y, random_state=114514): """ 对率回归训练函数。 @@ -38,6 +38,9 @@ def train_logreg(X, y, random_state="114514"): Returns: - numpy array: 训练得到的权重。 """ + weights = LogisticRegression(solver="liblinear", random_state=random_state) + weights.fit(X, y) + return weights def predict_logreg(inX, weights): @@ -51,6 +54,7 @@ def predict_logreg(inX, weights): Returns: - int: 预测的分类标签。 """ + return int(weights.predict([inX])[0]) # 这里也写自己的代码实现 @@ -59,13 +63,13 @@ def load_data(filepath): 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 Args: - - filepath (str): 数据文件路径。 + - directory (str): 数据文件路径。 Returns: - np.array: 特征数据。 - np.array: 标签数据。 """ - # 使用 np.genfromtxt 加载数据并处理缺失值 '?' 为 0 + # 使用 np.genfromtxt 加载数据,处理缺失值 '?' 为 0 data = np.genfromtxt(filepath, delimiter=" ", missing_values="?", filling_values=-1) # 分离特征标签