generated from LufsX/Experiment-LogReg
This commit is contained in:
+13
-10
@@ -58,7 +58,7 @@ def predict_logreg(inX, weights):
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,处理缺失数据。
|
||||
从文件中加载数据,处理缺失数据和确保样本数匹配。
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
@@ -76,22 +76,25 @@ def load_data(filepath):
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# 检查并替换缺失值 '?' 为 np.nan
|
||||
line = [float(x) if x != '?' else np.nan for x in line[:-1]]
|
||||
|
||||
# 如果数据中包含 np.nan,则跳过该行
|
||||
if np.any(np.isnan(line)):
|
||||
# 过滤掉包含 '?' 的行
|
||||
if '?' in line:
|
||||
continue
|
||||
|
||||
# 获取标签并添加到标签列表
|
||||
# 确保每行都有正确数量的特征和标签
|
||||
if len(line) != 9: # 假设每行有8个特征和1个标签
|
||||
continue
|
||||
|
||||
# 将特征转换为浮点数
|
||||
features = list(map(float, line[:-1]))
|
||||
label = int(line[-1])
|
||||
|
||||
# 组合特征和标签
|
||||
data.append(line)
|
||||
|
||||
# 添加到数据集
|
||||
data.append(features)
|
||||
labels.append(label)
|
||||
|
||||
return np.array(data), np.array(labels)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
|
||||
Reference in New Issue
Block a user