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

This commit is contained in:
Wyy
2024-11-02 20:51:46 +08:00
parent 5c62123573
commit 78d1c7ba3d
+15 -5
View File
@@ -64,12 +64,22 @@ def load_data(filepath):
- X (np.array): 特征数据。
- y (np.array): 标签数据。
"""
data = np.genfromtxt(filepath, delimiter=',', dtype=float, filling_values=0)
# 将问号 '?' 替换为 NaN,并将 NaN 替换为 0
data = np.where(data == '?', np.nan, data)
data = np.where(np.isnan(data), 0, data)
# 使用np.genfromtxt加载数据,并且忽略文件中不能转换为float的行
with open(filepath, 'r') as file:
lines = file.readlines()
data = []
for line in lines:
split_line = line.strip().split(',')
# 将非数值数据转换为0
split_line = [float(x) if x != '?' else 0 for x in split_line]
data.append(split_line)
data = np.array(data)
if data.ndim == 1:
data = data.reshape(1, -1)
X = data[:, :-1]
y = data[:, -1]
y = data[:, -1].reshape(-1, 1) # 确保y是二维的
return X, y