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

This commit is contained in:
2024-11-07 16:53:55 +08:00
parent 3677b2ab86
commit 44b7b38c93
+7 -5
View File
@@ -58,6 +58,8 @@ def predict_logreg(inX, weights):
import numpy as np
import numpy as np
def load_data(filepath):
"""
从文件中加载数据,处理缺失数据,并将 '?' 替换为列的均值。
@@ -82,10 +84,9 @@ def load_data(filepath):
# 将标签提取出来
label = int(line[-1])
# 将特征转换为浮动数组
# 将特征转换为浮动数组,'?' 替换为 np.nan
features = line[:-1] # 去除标签部分
# 用特征中的均值替换 '?',并转换为 float
features = [float(f) if f != '?' else None for f in features]
features = [float(f) if f != '?' else np.nan for f in features]
# 将数据和标签分别加入到列表
data.append(features)
@@ -95,15 +96,16 @@ def load_data(filepath):
data = np.array(data)
labels = np.array(labels)
# 计算每列的均值,并用均值替换 None
# 计算每列的均值,并用均值替换 NaN
col_means = np.nanmean(data, axis=0) # 计算每列的均值,忽略 NaN
# 将 None 替换为对应的列均值
# 将 NaN 替换为对应的列均值
for i in range(data.shape[1]):
data[:, i] = np.where(np.isnan(data[:, i]), col_means[i], data[:, i])
return data, labels
if __name__ == "__main__":
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")