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

This commit is contained in:
2024-11-05 11:10:39 +08:00
parent f699d7ea09
commit 4f017337f2
+13 -5
View File
@@ -1,5 +1,6 @@
# function.py
import numpy as np
import pandas as pd
"""
实验说明:
@@ -73,19 +74,26 @@ def predict_logreg(inX, weights):
# 这里也写自己的代码实现
def load_data(filepath):
"""
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
从文件中加载数据,处理缺失值并确保格式正确。
Args:
- directory (str): 数据文件路径。
- filepath (str): 数据文件路径。
Returns:
- np.array: 特征数据。
- np.array: 标签数据。
"""
data = np.loadtxt(filepath, delimiter=',') # 使用numpy加载数据
X = data[:, :-1] # 假设最后一列为标签
y = data[:, -1] # 最后一列为标签
# 使用 pandas 加载数据
data = pd.read_csv(filepath, header=None, na_values='?')
# 将数据中的缺失值用均值填充(或可以选择其他策略)
data.fillna(data.mean(), inplace=True)
# 提取特征和标签
X = data.iloc[:, :-1].values # 假设最后一列为标签
y = data.iloc[:, -1].values # 最后一列为标签
# 确保 y 是二进制的,1表示死亡,0表示存活
y = np.where(y == 1, 1, 0) # 处理标签,确保其为0或1
return X, y