From 99d3bde587adc6125c021d473d0e2e326172b6aa Mon Sep 17 00:00:00 2001 From: cy <939601402@qq.com> Date: Sun, 10 Nov 2024 23:50:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/function.py b/function.py index 0412122..bdc9723 100644 --- a/function.py +++ b/function.py @@ -88,7 +88,7 @@ def predict_logreg(inX, weights): def load_data(filepath): """ - 从文件中加载数据,填充缺失值,并进行特征处理。 + 从文件中加载数据,填充缺失值为均值,并进行特征处理。 Args: - filepath (str): 数据文件路径。 @@ -104,14 +104,26 @@ def load_data(filepath): for line in lines: line = line.strip().split() 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) labels.append(int(line[-1])) - data = np.array(data) + # 转换为 numpy 数组 + data = np.array(data, dtype=np.object) 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[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为0 @@ -125,6 +137,7 @@ def load_data(filepath): return data, target_variable + if __name__ == "__main__": X_train, y_train = load_data("./data/horse-colic.data") X_test, y_test = load_data("./data/horse-colic.test")