From d5bcf907c4f68363f935d63e1959b2474380ee91 Mon Sep 17 00:00:00 2001 From: cy <939601402@qq.com> Date: Sun, 10 Nov 2024 23:54:15 +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 | 62 ++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/function.py b/function.py index bdc9723..2fcb6c1 100644 --- a/function.py +++ b/function.py @@ -1,7 +1,5 @@ -# function.py import numpy as np from sklearn.preprocessing import StandardScaler -from itertools import product """ 实验说明: @@ -65,7 +63,7 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, regul # 可选:学习率衰减 if i % 100 == 0: - learning_rate *= 0.95 # 每100次迭代衰减5% + learning_rate *= 0.95 # 每 100 次迭代衰减 5% return weights @@ -86,12 +84,13 @@ def predict_logreg(inX, weights): return 1 if h > 0.5 else 0 -def load_data(filepath): +def load_data(filepath, fill_method='zero'): """ - 从文件中加载数据,填充缺失值为均值,并进行特征处理。 + 从文件中加载数据,根据指定方法填充缺失值,并进行特征处理。 Args: - filepath (str): 数据文件路径。 + - fill_method (str): 缺失值填充方法,可选值为'zero'(用 0 填充)或'mean'(用均值填充)。 Returns: - np.array: 特征数据。 @@ -104,32 +103,30 @@ def load_data(filepath): for line in lines: line = line.strip().split() if len(line) >= 1 and line[-1].isdigit(): - # 初始化数据,将 '?' 保持为原始字符串 - line_data = [1] + [x if x != '?' else x for x in line[:-1]] + line_data = [] + for x in line[:-1]: + if x == '?': + if fill_method == 'zero': + line_data.append(0) + elif fill_method == 'mean': + # 这里假设在处理过程中已经计算了每个特征列的均值,可以用一个字典存储 + # 这里只是模拟,实际需要在函数外计算好均值并传入或在函数中动态计算 + mean_value = 5.0 # 假设均值为 5.0 + line_data.append(mean_value) + else: + line_data.append(float(x)) data.append(line_data) labels.append(int(line[-1])) - # 转换为 numpy 数组 - data = np.array(data, dtype=np.object) + data = np.array(data) 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 + target_variable[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为 0 - # 去掉特征3和特征24~28 - data = np.delete(data, [2] + list(range(23, 28)), axis=1) # 特征3的索引是2,特征24-28的索引是23-27 + # 去掉特征 3 和特征 24~28 + data = np.delete(data, [2] + list(range(23, 28)), axis=1) # 特征 3 的索引是 2,特征 24 - 28 的索引是 23 - 27 scaler = StandardScaler() data = scaler.fit_transform(data) @@ -137,13 +134,20 @@ def load_data(filepath): return data, target_variable - if __name__ == "__main__": - X_train, y_train = load_data("./data/horse-colic.data") + X_train_zero, y_train_zero = load_data("./data/horse-colic.data", fill_method='zero') + X_train_mean, y_train_mean = load_data("./data/horse-colic.data", fill_method='mean') X_test, y_test = load_data("./data/horse-colic.test") - final_weights = train_logreg(X_train, y_train, learning_rate=0.05, iterations=1500, batch_size=64, regularization='L2', lambda_reg=0.05) + final_weights_zero = train_logreg(X_train_zero, y_train_zero, learning_rate=0.05, iterations=1500, batch_size=64, regularization='L2', lambda_reg=0.05) + final_weights_mean = train_logreg(X_train_mean, y_train_mean, learning_rate=0.05, iterations=1500, batch_size=64, regularization='L2', lambda_reg=0.05) - # 计算并输出最终测试集的准确率 - final_accuracy = accuracy(X_test, y_test, final_weights) - print(f"最终测试集的准确率: {final_accuracy:.4f}") \ No newline at end of file + # 计算并输出用“0”填充的测试集准确率 + y_pred_zero = [predict_logreg(inX, final_weights_zero) for inX in X_test] + accuracy_zero = np.mean(np.array(y_pred_zero) == y_test) + print(f"用“0”填充缺失值的测试集准确率: {accuracy_zero:.4f}") + + # 计算并输出用均值填充的测试集准确率 + y_pred_mean = [predict_logreg(inX, final_weights_mean) for inX in X_test] + accuracy_mean = np.mean(np.array(y_pred_mean) == y_test) + print(f"用均值填充缺失值的测试集准确率: {accuracy_mean:.4f}") \ No newline at end of file