diff --git a/function.py b/function.py index 962b8ef..05f90c3 100644 --- a/function.py +++ b/function.py @@ -1,4 +1,6 @@ +# function.py import numpy as np +import pandas as pd """ 请在这里填写学号和姓名,不填写即成绩无效。 @@ -6,73 +8,79 @@ import numpy as np id = "202210423107" name = "刘燚燊" -# 逻辑回归训练函数 -def train_logreg(X, y, learning_rate=0.01, iterations=1000): - """ - 对率回归训练函数。 - - Args: - - X (numpy array): 特征数据。 - - y (numpy array): 标签数据。 - - learning_rate (float): 学习率。 - - iterations (int): 迭代次数。 - - Returns: - - numpy array: 训练得到的权重。 - """ - m, n = X.shape - weights = np.zeros(n) # 初始化权重 - for _ in range(iterations): - # 计算预测值 - z = np.dot(X, weights) - predictions = 1 / (1 + np.exp(-z)) # Sigmoid函数 - # 更新权重 - gradient = np.dot(X.T, (predictions - y)) / m - weights -= learning_rate * gradient - return weights - -def predict_logreg(inX, weights): - """ - 对率回归预测函数。 - - Args: - - inX (numpy array): 输入待分类的样本。 - - weights (numpy array): 训练得到的权重。 - - Returns: - - int: 预测的分类标签。 - """ - z = np.dot(inX, weights) - probability = 1 / (1 + np.exp(-z)) # Sigmoid函数 - return 1 if probability >= 0.5 else 0 # 使用0.5作为阈值 def load_data(filepath): """ - 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 - - Args: - - filepath (str): 数据文件路径。 - - Returns: - - np.array: 特征数据。 - - np.array: 标签数据。 + 加载数据集并进行预处理。 + 参数: + filepath: 数据文件路径 + 返回: + 特征和目标变量的数组 """ - # 读取数据并处理缺失值 - data = [] - with open(filepath, 'r') as f: - for line in f: - # 将问号替换为0并分割成列表 - cleaned_line = line.strip().replace('?', '0').split() - # 将字符串转换为浮点数并添加到数据集中 - data.append([float(x) for x in cleaned_line]) - - data = np.array(data) # 转换为numpy数组 - X = data[:, :-1] # 所有特征 - y = data[:, -1] # 最后一列作为标签 + # 读取数据 + data = pd.read_csv(filepath, header=None) + + # 替换缺失值 + data.replace('?', np.nan, inplace=True) + data.fillna(0, inplace=True) + + # 处理目标变量 + target_col = data.shape[1] - 1 # 目标变量为最后一列 + data[target_col] = data[target_col].replace({2: 0, 3: 0}) + + # 去掉不需要的特征 + data.drop(columns=[2, 24, 25, 26, 27], inplace=True, errors='ignore') + + # 转换数据为数值类型 + data = data.apply(pd.to_numeric, errors='coerce') + + # 分离特征与目标变量 + X = data.drop(columns=[target_col]).values # 特征 + y = data[target_col].values # 目标变量 + 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) # 初始化权重 + for i in range(iterations): + # 计算预测值 + 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 + + return weights + + +def predict_logreg(inX, weights): + """ + 使用训练好的模型进行预测。 + 参数: + inX: 单个输入数据 + weights: 权重向量 + 返回: + 预测结果 + """ + linear_model = np.dot(inX, weights) + y_predicted = 1 / (1 + np.exp(-linear_model)) + return 1 if y_predicted >= 0.5 else 0 # 返回二分类结果 + + if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 # 加载训练集 X_train, y_train = load_data("./data/horse-colic.data")