From 0d6b02d2a0e41b02a22e40ec90bd9665d2822b9d Mon Sep 17 00:00:00 2001 From: wujunjie <297081150@qq.com> Date: Thu, 21 Nov 2024 17:14:23 +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 | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/function.py b/function.py index 521ef93..8cb28e1 100644 --- a/function.py +++ b/function.py @@ -78,20 +78,24 @@ def predict_svm(inX, weights, bias): # 这里也写自己的代码实现 -import numpy as np -import pandas as pd +def load_data(filepath): + """ + 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 -def load_data(data): - # 将数据转换为 DataFrame - df = pd.DataFrame(data) - - # 清洗数据,替换 '?' 为 NaN - df.replace('?', np.nan, inplace=True) - - # 尝试将数据转换为浮点数 - df = df.apply(pd.to_numeric, errors='coerce') - - return df + Args: + - filepath (str): 数据文件路径。 + + Returns: + - np.array: 特征数据。 + - np.array: 标签数据。 + """ + data = np.loadtxt(filepath, delimiter=',', dtype=str) + X = data[:, :-1].astype(float) # 特征数据 + y = data[:, -1] # 标签数据 + + # 将标签转换为 -1 和 1 + y = np.where(y == '1', 1, -1) # 假设标签为 '1' 表示死亡,其他为存活 + return X, y if __name__ == "__main__":