diff --git a/function.py b/function.py index 01add2a..390a34c 100644 --- a/function.py +++ b/function.py @@ -31,35 +31,38 @@ name = "严锡好" # 这里写自己的代码实现 +# sigmoid函数,用于逻辑回归模型中将线性输出转换为概率 def sigmoid(z): return 1.0 / (1 + np.exp(-z)) +# compute_gradient函数,用于计算逻辑回归的梯度 def compute_gradient(X_batch, y_batch, weights, lambda_reg): - z = np.dot(X_batch, weights) - h = sigmoid(z) - gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch) - gradient += (lambda_reg / len(y_batch)) * weights # L2正则化 + z = np.dot(X_batch, weights) # 计算当前权重下的线性组合 + h = sigmoid(z) # 应用sigmoid函数将线性组合转换为概率 + gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch) # 计算梯度 + gradient += (lambda_reg / len(y_batch)) * weights # 添加L2正则化项 return gradient +# train_logreg函数,用于训练逻辑回归模型 def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1): - m, n = X.shape - weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化 - best_weights = weights.copy() - best_accuracy = 0 + m, n = X.shape # 获取样本数量和特征数量 + weights = np.random.randn(n) * np.sqrt(2. / n) # 使用He初始化权重 + best_weights = weights.copy() # 存储最佳权重 + best_accuracy = 0 # 存储最佳准确率 - for i in range(iterations): - indices = np.random.permutation(m) + for i in range(iterations): # 迭代指定次数 + indices = np.random.permutation(m) # 随机打乱数据 X_shuffled = X[indices] y_shuffled = y[indices] - for j in range(0, m, batch_size): + for j in range(0, m, batch_size): # 批量梯度下降 X_batch = X_shuffled[j:j + batch_size] y_batch = y_shuffled[j:j + batch_size] - gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg) - weights -= learning_rate * gradient + gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg) # 计算梯度 + weights -= learning_rate * gradient # 更新权重 - accuracy = accuracy_score(y, predict_logreg(X, weights)) - if accuracy > best_accuracy: + accuracy = accuracy_score(y, predict_logreg(X, weights)) # 计算当前准确率 + if accuracy > best_accuracy: # 更新最佳准确率和最佳权重 best_accuracy = accuracy best_weights = weights.copy() @@ -68,32 +71,35 @@ def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lam return best_weights +# predict_logreg函数,用于使用训练好的权重进行预测 def predict_logreg(X, weights): - z = np.dot(X, weights) - h = sigmoid(z) - return np.where(h >= 0.5, 1, 0) + z = np.dot(X, weights) # 计算线性组合 + h = sigmoid(z) # 应用sigmoid函数 + return np.where(h >= 0.5, 1, 0) # 返回预测结果 +# preprocess_data函数,用于预处理数据 def preprocess_data(X, y): - y[y == 2] = 0 - y[y == 3] = 0 - X = np.delete(X, [3], axis=1) - X = X[:, :23] + y[y == 2] = 0 # 将类别2转换为0 + y[y == 3] = 0 # 将类别3转换为0 + X = np.delete(X, [3], axis=1) # 删除特征3 + X = X[:, :23] # 删除特征24~28 return X, y +# load_data函数,用于加载数据 def load_data(filepath): try: - data = pd.read_csv(filepath, header=None, sep=r'\s+', on_bad_lines='skip') + data = pd.read_csv(filepath, header=None, sep=r'\s+', on_bad_lines='skip') # 读取数据 except pd.errors.ParserError as e: print(f"Error parsing file: {e}") return None, None - data.replace('?', np.nan, inplace=True) - imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0) + data.replace('?', np.nan, inplace=True) # 将'?'替换为np.nan + imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0) # 填充缺失值 data.iloc[:, :-1] = imputer.fit_transform(data.iloc[:, :-1]) for col in data.columns[:-1]: - data[col] = pd.to_numeric(data[col], errors='coerce') - X = data.iloc[:, :-1].values - y = data.iloc[:, -1].values.astype(int) - X, y = preprocess_data(X, y) + data[col] = pd.to_numeric(data[col], errors='coerce') # 转换为数值类型 + X = data.iloc[:, :-1].values # 特征数据 + y = data.iloc[:, -1].values.astype(int) # 目标变量 + X, y = preprocess_data(X, y) # 预处理数据 return X, y if __name__ == "__main__": @@ -115,7 +121,6 @@ if __name__ == "__main__": y_pred = predict_logreg(X_val, final_weights) val_accuracy = accuracy_score(y_val, y_pred) print(f"验证集的准确率: {val_accuracy:.4f}") - print(classification_report(y_val, y_pred)) # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") @@ -124,5 +129,4 @@ if __name__ == "__main__": # 测试模型 y_test_pred = predict_logreg(X_test, final_weights) test_accuracy = accuracy_score(y_test, y_test_pred) - print(f"最终测试集的准确率: {test_accuracy:.4f}") - print(classification_report(y_test, y_test_pred)) \ No newline at end of file + print(f"最终测试集的准确率: {test_accuracy:.4f}") \ No newline at end of file