diff --git a/function.py b/function.py index 9dc71f9..ee9d822 100644 --- a/function.py +++ b/function.py @@ -15,41 +15,61 @@ import numpy as np - numpy: 2.1.1 """ -id = "202210423213" -name = "熊浩燃" +id = "202210423xxx" +name = "test_name" # 对率回归训练函数 -def train_logreg(X, y, learning_rate=0.001, iterations=1000): - # 标准化特征数据 - X = (X - np.mean(X, axis=0)) / np.std(X, axis=0) - +def train_logreg(X, y, learning_rate=0.01, iterations=1000): + """ + 对率回归训练函数。 + Args: + - X (numpy array): 特征数据。 + - y (numpy array): 标签数据。 + - learning_rate (float): 学习率,默认为0.01。 + - iterations (int): 迭代次数,默认为1000。 + Returns: + - numpy array: 训练得到的权重。 + """ + # 初始化权重 weights = np.zeros(X.shape[1]) - + for i in range(iterations): + # 预测值:通过对率回归模型得到 z = np.dot(X, weights) predictions = 1 / (1 + np.exp(-z)) - # 防止 log(0) 引发 NaN - predictions = np.clip(predictions, 1e-10, 1 - 1e-10) - + # 计算梯度并更新权重 gradient = np.dot(X.T, (predictions - y)) / y.size weights -= learning_rate * gradient - - # 输出损失帮助调试 + + # 可选:输出损失,帮助调试 if i % 100 == 0: loss = -np.mean(y * np.log(predictions) + (1 - y) * np.log(1 - predictions)) print(f"迭代 {i},损失: {loss}") return weights -# 数据加载函数 +# 对率回归预测函数 +def predict_logreg(inX, weights): + """ + 对率回归预测函数。 + Args: + - inX (numpy array): 输入待分类的样本。 + - weights (numpy array): 训练得到的权重。 + Returns: + - int: 预测的分类标签。 + """ + probability = 1 / (1 + np.exp(-np.dot(inX, weights))) + return 1 if probability >= 0.5 else 0 + +# 数据加载函数,包含缺失值填充 def load_data(filepath): """ - 从文件中加载数据。 + 从文件中加载数据,并填充缺失值。 Args: - filepath (str): 数据文件路径。 Returns: - - numpy array: 特征数据。 + - numpy array: 填充后的特征数据。 - numpy array: 标签数据。 """ data = [] @@ -58,14 +78,24 @@ def load_data(filepath): for line in file: # 去除无效行,并将数据转换为float格式 fields = line.strip().split() - if '?' not in fields: - # 以最后一列作为标签,其余列作为特征 - features = [float(x) for x in fields[:-1]] - label = float(fields[-1]) - data.append(features) - labels.append(label) + + # 将 '?' 替换为 None + features = [float(x) if x != '?' else None for x in fields[:-1]] + label = float(fields[-1]) + data.append(features) + labels.append(label) + + # 转换为numpy数组并填充缺失值 + data = np.array(data, dtype=float) + labels = np.array(labels, dtype=float) + + # 处理缺失值,使用平均值填充 + col_means = np.nanmean(data, axis=0) + indices = np.where(np.isnan(data)) + data[indices] = np.take(col_means, indices[1]) + + return data, labels - return np.array(data), np.array(labels) if __name__ == "__main__": # 加载训练集