generated from LufsX/Experiment-LogReg
This commit is contained in:
+4
-20
@@ -58,7 +58,7 @@ def predict_logreg(inX, weights):
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,处理缺失数据和确保样本数匹配。
|
||||
从文件中加载数据。
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
@@ -75,30 +75,14 @@ def load_data(filepath):
|
||||
line = line.strip().split()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# 过滤掉包含 '?' 的行
|
||||
if '?' in line:
|
||||
continue
|
||||
|
||||
# 确保每行都有正确数量的特征和标签
|
||||
if len(line) != 9: # 假设每行有8个特征和1个标签
|
||||
continue
|
||||
|
||||
# 将特征转换为浮点数
|
||||
try:
|
||||
features = list(map(float, line[:-1])) # 所有特征转换为浮点数
|
||||
label = int(line[-1]) # 标签为整数
|
||||
except ValueError:
|
||||
# 如果遇到无法转换的值,则跳过这一行
|
||||
continue
|
||||
|
||||
# 添加到数据集
|
||||
# 将特征转换为浮点型
|
||||
features = list(map(float, line[:-1]))
|
||||
label = int(line[-1])
|
||||
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