From f6b8f9ec3c7dc1cca5b81dcdd58a6e78b1794b48 Mon Sep 17 00:00:00 2001 From: 43606070 <43606070@qq.com> Date: Tue, 29 Oct 2024 19:14:26 +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 | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/function.py b/function.py index 1433793..8864033 100644 --- a/function.py +++ b/function.py @@ -29,15 +29,15 @@ def sigmoid(z): # 对率回归训练函数 -def train_logreg(X, y, learning_rate=0.01, iterations=1000): +def train_logreg(X, y, learning_rate=0.05, iterations=500): """ 对率回归训练函数。 Args: - X (numpy array): 特征数据。 - y (numpy array): 标签数据。 - - learning_rate (float): 学习率, 默认值为0.01。 - - iterations (int): 迭代次数, 默认值为1000。 + - learning_rate (float): 学习率, 默认值为0.05。 + - iterations (int): 迭代次数, 默认值为500。 Returns: - numpy array: 训练得到的权重和偏置(合并为一个数组)。 @@ -49,7 +49,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000): X = np.c_[X, np.ones((n_samples, 1))] # 梯度下降法 - for i in range(iterations): + for _ in range(iterations): linear_model = np.dot(X, weights) y_pred = sigmoid(linear_model) @@ -84,7 +84,7 @@ def predict_logreg(inX, weights): # 数据加载函数 def load_data(filepath): """ - 从文件中加载数据,处理缺失值,将 ? 替换为 NaN 并填充缺失值。 + 从文件中加载数据,处理缺失值,将 ? 替换为 NaN 并填充缺失值为 0。 Args: - filepath (str): 数据文件路径。 @@ -94,12 +94,7 @@ def load_data(filepath): - np.array: 标签数据。 """ # 使用 genfromtxt 读取文件并将缺失值标记为 NaN - data = np.genfromtxt(filepath, delimiter=' ', dtype=float, missing_values='?', filling_values=np.nan) - - # 对每一列的 NaN 值用列的平均值进行填充 - nan_columns = np.isnan(data) - mean_values = np.nanmean(data, axis=0) - data[nan_columns] = np.take(mean_values, np.where(nan_columns)[1]) + data = np.genfromtxt(filepath, delimiter=' ', dtype=float, missing_values='?', filling_values=0.0) # 分离特征和标签 X = data[:, :-1] # 所有列(除了最后一列)是特征 @@ -115,7 +110,7 @@ if __name__ == "__main__": X_test, y_test = load_data("./data/horse-colic.test") # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) + weights = train_logreg(X_train, y_train, learning_rate=0.05, iterations=500) # 测试单个数据 y_pred = predict_logreg(X_test[11], weights) @@ -123,3 +118,4 @@ if __name__ == "__main__": +