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

This commit is contained in:
2024-10-31 15:33:49 +08:00
parent 2b522e52d0
commit b0c53f2d32
+16 -5
View File
@@ -93,7 +93,7 @@ def predict_logreg(inX, weights, degree=2):
def load_data(filepath): def load_data(filepath):
""" """
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 从文件中加载数据,并使用均值填充缺失值。
Args: Args:
- filepath (str): 数据文件路径。 - filepath (str): 数据文件路径。
@@ -107,10 +107,21 @@ def load_data(filepath):
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
for line in file: for line in file:
line_data = line.strip().split() line_data = line.strip().split()
if '?' not in line_data: # 忽略缺失值的行 # 将缺失值替换为 np.nan
data.append([float(x) for x in line_data[:-1]]) line_data = [float(x) if x != '?' else np.nan for x in line_data]
labels.append(int(float(line_data[-1]))) data.append(line_data[:-1])
return np.array(data), np.array(labels) labels.append(int(line_data[-1]))
data = np.array(data)
labels = np.array(labels)
# 使用列均值填充缺失值
col_mean = np.nanmean(data, axis=0)
# 找到所有缺失值,并用列均值替换
indices = np.where(np.isnan(data))
data[indices] = np.take(col_mean, indices[1])
return data, labels
if __name__ == "__main__": if __name__ == "__main__":
# 测试数据加载和模型训练预测 # 测试数据加载和模型训练预测