generated from LufsX/Experiment-LogReg
This commit is contained in:
+6
-22
@@ -58,7 +58,7 @@ def predict_logreg(inX, weights):
|
|||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
"""
|
"""
|
||||||
从文件中加载数据,处理缺失数据和确保样本数匹配。
|
从文件中加载数据。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- filepath (str): 数据文件路径。
|
- filepath (str): 数据文件路径。
|
||||||
@@ -69,36 +69,20 @@ def load_data(filepath):
|
|||||||
"""
|
"""
|
||||||
data = []
|
data = []
|
||||||
labels = []
|
labels = []
|
||||||
|
|
||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
for line in file:
|
for line in file:
|
||||||
line = line.strip().split()
|
line = line.strip().split()
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
# 将特征转换为浮点型
|
||||||
# 过滤掉包含 '?' 的行
|
features = list(map(float, line[:-1]))
|
||||||
if '?' in line:
|
label = int(line[-1])
|
||||||
continue
|
|
||||||
|
|
||||||
# 确保每行都有正确数量的特征和标签
|
|
||||||
if len(line) != 9: # 假设每行有8个特征和1个标签
|
|
||||||
continue
|
|
||||||
|
|
||||||
# 将特征转换为浮点数
|
|
||||||
try:
|
|
||||||
features = list(map(float, line[:-1])) # 所有特征转换为浮点数
|
|
||||||
label = int(line[-1]) # 标签为整数
|
|
||||||
except ValueError:
|
|
||||||
# 如果遇到无法转换的值,则跳过这一行
|
|
||||||
continue
|
|
||||||
|
|
||||||
# 添加到数据集
|
|
||||||
data.append(features)
|
data.append(features)
|
||||||
labels.append(label)
|
labels.append(label)
|
||||||
|
|
||||||
return np.array(data), np.array(labels)
|
return np.array(data), np.array(labels)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载训练集
|
# 加载训练集
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
X_train, y_train = load_data("./data/horse-colic.data")
|
||||||
|
|||||||
Reference in New Issue
Block a user