generated from LufsX/Experiment-LogReg
This commit is contained in:
+13
-4
@@ -58,7 +58,7 @@ def predict_logreg(inX, weights):
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据。
|
||||
从文件中加载数据,处理缺失数据。
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
@@ -75,10 +75,19 @@ def load_data(filepath):
|
||||
line = line.strip().split()
|
||||
if not line:
|
||||
continue
|
||||
# 将特征转换为浮点型
|
||||
features = list(map(float, line[:-1]))
|
||||
|
||||
# 检查并替换缺失值 '?' 为 np.nan
|
||||
line = [float(x) if x != '?' else np.nan for x in line[:-1]]
|
||||
|
||||
# 如果数据中包含 np.nan,则跳过该行
|
||||
if np.any(np.isnan(line)):
|
||||
continue
|
||||
|
||||
# 获取标签并添加到标签列表
|
||||
label = int(line[-1])
|
||||
data.append(features)
|
||||
|
||||
# 组合特征和标签
|
||||
data.append(line)
|
||||
labels.append(label)
|
||||
|
||||
return np.array(data), np.array(labels)
|
||||
|
||||
Reference in New Issue
Block a user