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

This commit is contained in:
2024-11-07 16:58:14 +08:00
parent 44b7b38c93
commit f5032392e9
+10 -17
View File
@@ -57,24 +57,15 @@ def predict_logreg(inX, weights):
return 1 if prob >= 0.5 else 0
import numpy as np
import numpy as np
from sklearn.preprocessing import StandardScaler
def load_data(filepath):
"""
从文件中加载数据,处理缺失数据,并将 '?' 替换为列的均值。
Args:
- filepath (str): 数据文件路径。
Returns:
- np.array: 特征数据。
- np.array: 标签数据。
从文件中加载数据,处理缺失数据,并将 '?' 替换为列的均值或中位数。
"""
data = []
labels = []
# 读取数据
with open(filepath, 'r') as file:
for line in file:
line = line.strip().split()
@@ -92,20 +83,22 @@ def load_data(filepath):
data.append(features)
labels.append(label)
# 将列表转换为numpy数组
# 将列表转换为 numpy 数组
data = np.array(data)
labels = np.array(labels)
# 计算每列的均值,并用均值替换 NaN
col_means = np.nanmean(data, axis=0) # 计算每列的均值,忽略 NaN
# 将 NaN 替换为对应的列均值
# 计算每列的中位数,并用中位数替换 NaN
col_medians = np.nanmedian(data, axis=0) # 计算每列的中位数
for i in range(data.shape[1]):
data[:, i] = np.where(np.isnan(data[:, i]), col_means[i], data[:, i])
data[:, i] = np.where(np.isnan(data[:, i]), col_medians[i], data[:, i])
# 对数据进行标准化
scaler = StandardScaler()
data = scaler.fit_transform(data)
return data, labels
if __name__ == "__main__":
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")