From 3b52f97a315888ab9ba2364ad5dd9d0a5e794953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=87=9A=E7=87=8A?= Date: Sun, 3 Nov 2024 17:05:01 +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 | 147 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 59 deletions(-) diff --git a/function.py b/function.py index 147ca5a..5289b8e 100644 --- a/function.py +++ b/function.py @@ -1,95 +1,124 @@ -# function.py import numpy as np -import pandas as pd +from sklearn.model_selection import KFold +from sklearn.impute import SimpleImputer -""" -请在这里填写学号和姓名,不填写即成绩无效。 -""" -id = "202210423107" +# 请在这里填写学号和姓名,不填写即成绩无效。 +id = "202210423106" name = "刘燚燊" - def load_data(filepath): """ - 加载数据集并进行预处理。 - 参数: - filepath: 数据文件路径 - 返回: - 特征和目标变量的数组 + 从文件中加载数据,处理缺失值和标签。 + + Args: + - filepath (str): 数据文件路径。 + + Returns: + - np.array: 特征数据。 + - np.array: 标签数据。 """ - # 读取数据 - data = pd.read_csv(filepath, header=None) + with open(filepath, 'r') as f: + data = [line.strip().split() for line in f.readlines()] - # 替换缺失值 - data.replace('?', np.nan, inplace=True) - data.fillna(0, inplace=True) + # 将数据中缺失值用0填充 + data = [[0 if value == '?' else float(value) for value in line] for line in data] + data = np.array(data) - # 处理目标变量 - target_col = data.shape[1] - 1 # 目标变量为最后一列 - data[target_col] = data[target_col].replace({2: 0, 3: 0}) + # 分离特征和标签 + X = data[:, :-1] + y = data[:, -1] - # 去掉不需要的特征 - data.drop(columns=[2, 24, 25, 26, 27], inplace=True, errors='ignore') + # 使用均值填充缺失值 + imputer = SimpleImputer(strategy='mean') + X = imputer.fit_transform(X) - # 转换数据为数值类型 - data = data.apply(pd.to_numeric, errors='coerce') - - # 分离特征与目标变量 - X = data.drop(columns=[target_col]).values # 特征 - y = data[target_col].values # 目标变量 + # 过滤掉没有类别标签的数据 + valid_indices = np.isfinite(y) + X = X[valid_indices] + y = y[valid_indices] return X, y -def train_logreg(X, y, learning_rate=0.01, iterations=1000): - """ - 训练逻辑回归模型。 - 参数: - X: 特征数据 - y: 目标变量 - learning_rate: 学习率 - iterations: 训练迭代次数 - 返回: - 权重向量 - """ - m, n = X.shape - weights = np.zeros(n) # 初始化权重 +def train_logreg(X, y, learning_rate=0.01, iterations=1000, reg_param=0.01): + # 初始化权重 + weights = np.zeros(X.shape[1]) + for i in range(iterations): - # 计算预测值 + # 计算线性组合和sigmoid函数 linear_model = np.dot(X, weights) y_predicted = 1 / (1 + np.exp(-linear_model)) - # 更新权重 - gradient = np.dot(X.T, (y_predicted - y)) / m - weights -= learning_rate * gradient + # 计算梯度 + gradient = np.dot(X.T, (y_predicted - y)) / y.size + weights -= learning_rate * (gradient + reg_param * weights) # L2 正则化 return weights def predict_logreg(inX, weights): """ - 使用训练好的模型进行预测。 - 参数: - inX: 单个输入数据 - weights: 权重向量 - 返回: - 预测结果 + 对率回归预测函数。 + + Args: + - inX (numpy array): 输入待分类的样本。 + - weights (numpy array): 训练得到的权重。 + + Returns: + - int: 预测的分类标签。 """ linear_model = np.dot(inX, weights) y_predicted = 1 / (1 + np.exp(-linear_model)) - return 1 if y_predicted >= 0.5 else 0 # 返回二分类结果 + return (y_predicted > 0.5).astype(int) + + +def evaluate_model(X, y, learning_rate, iterations, reg_param, folds=5): + """ + 使用交叉验证评估模型性能。 + + Args: + - X (numpy array): 特征数据。 + - y (numpy array): 标签数据。 + - learning_rate (float): 学习率。 + - iterations (int): 训练迭代次数。 + - reg_param (float): 正则化参数。 + - folds (int): 交叉验证折数。 + + Returns: + - float: 平均准确率。 + """ + kf = KFold(n_splits=folds) + accuracy_list = [] + + for train_index, test_index in kf.split(X): + X_train, X_test = X[train_index], X[test_index] + y_train, y_test = y[train_index], y[test_index] + + weights = train_logreg(X_train, y_train, learning_rate, iterations, reg_param) + predictions = predict_logreg(X_test, weights) + + accuracy = np.mean(predictions == y_test) + accuracy_list.append(accuracy) + + return np.mean(accuracy_list) if __name__ == "__main__": # 加载训练集 X_train, y_train = load_data("./data/horse-colic.data") - # 加载测试集 - X_test, y_test = load_data("./data/horse-colic.test") + # 调优超参数 + best_accuracy = 0 + best_params = {} + learning_rates = [0.001, 0.01, 0.1] + reg_params = [0.01, 0.1, 1.0] - # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) + for lr in learning_rates: + for reg in reg_params: + accuracy = evaluate_model(X_train, y_train, lr, 1000, reg) + print(f"尝试参数: {{'learning_rate': {lr}, 'reg_param': {reg}}}, 准确率: {accuracy:.2f}") + if accuracy > best_accuracy: + best_accuracy = accuracy + best_params = {'learning_rate': lr, 'reg_param': reg} - # 测试单个数据 - y_pred = predict_logreg(X_test[11], weights) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") + print(f"最佳参数: {best_params}, 最高准确率: {best_accuracy:.2f}")