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

This commit is contained in:
cy
2024-11-10 23:50:31 +08:00
parent f3636cd5ae
commit 99d3bde587
+17 -4
View File
@@ -88,7 +88,7 @@ def predict_logreg(inX, weights):
def load_data(filepath): def load_data(filepath):
""" """
从文件中加载数据,填充缺失值,并进行特征处理。 从文件中加载数据,填充缺失值为均值,并进行特征处理。
Args: Args:
- filepath (str): 数据文件路径。 - filepath (str): 数据文件路径。
@@ -104,14 +104,26 @@ def load_data(filepath):
for line in lines: for line in lines:
line = line.strip().split() line = line.strip().split()
if len(line) >= 1 and line[-1].isdigit(): if len(line) >= 1 and line[-1].isdigit():
# 将缺失值 '?' 填充为 0,并转换为浮点数 # 初始化数据,将 '?' 保持为原始字符串
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]] line_data = [1] + [x if x != '?' else x for x in line[:-1]]
data.append(line_data) data.append(line_data)
labels.append(int(line[-1])) labels.append(int(line[-1]))
data = np.array(data) # 转换为 numpy 数组
data = np.array(data, dtype=np.object)
labels = np.array(labels) labels = np.array(labels)
# 将 '?' 替换为 np.nan,便于计算均值
data[data == '?'] = np.nan
data = data.astype(float)
# 计算每个特征的均值
col_means = np.nanmean(data[:, 1:], axis=0) # 排除常数项(偏置项)
# 用每个特征的均值填充缺失值
inds = np.where(np.isnan(data))
data[inds] = np.take(col_means, inds[1]-1)
# 选择特征,合并目标变量 # 选择特征,合并目标变量
target_variable = labels target_variable = labels
target_variable[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为0 target_variable[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为0
@@ -125,6 +137,7 @@ def load_data(filepath):
return data, target_variable return data, target_variable
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")
X_test, y_test = load_data("./data/horse-colic.test") X_test, y_test = load_data("./data/horse-colic.test")