diff --git a/function.py b/function.py index 4e2794c..89a0f51 100644 --- a/function.py +++ b/function.py @@ -26,77 +26,64 @@ name = "吴俊煯" # 这里写自己的代码实现 -def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.0): +def train_logreg(X, y, learning_rate=0.01, iterations=1000): m, n = X.shape weights = np.zeros(n) bias = 0 - m = len(X) # 重新赋值m,以确保它表示样本数量(在添加正则化时可能需要) - + for _ in range(iterations): - linear_output = np.dot(X, weights) + bias # 向量化操作 - y_predicted = 1 / (1 + np.exp(-linear_output)) - error = y - y_predicted - + for idx, x_i in enumerate(X): + linear_output = np.dot(x_i, weights) + bias + y_predicted = 1 / (1 + np.exp(-linear_output)) + + # 计算误差 + error = y[idx] - y_predicted + + # 更新权重和偏置 + weights += learning_rate * error * x_i + bias += learning_rate * error + + return weights - # 计算梯度(不包括正则化项) - gradient = np.dot(X.T, error) / m - - # 加上正则化项的梯度(注意这里是对weights的每个元素都加上-regularization*weight[i]) - gradient += -regularization * weights - - # 更新权重 - weights += learning_rate * gradient - bias += learning_rate * np.sum(error) / m - - return weights, bias - -def predict_logreg(inX, weights, bias): - linear_output = np.dot(inX, weights) + bias - # 如果需要sigmoid输出,可以添加下面这行 - # sigmoid_output = 1 / (1 + np.exp(-linear_output)) - # return sigmoid_output - return linear_output +def predict_logreg(inX, weights): + linear_output = np.dot(inX, weights) + y_predicted = 1 / (1 + np.exp(-linear_output)) + + # 将输出转换为分类标签 + return 1 if y_predicted >= 0.5 else 0 # 这里也写自己的代码实现 def load_data(filepath): data = [] with open(filepath, 'r') as file: for line in file: + # 分割行,并去除任何可能的空白字符 parts = [part.strip() for part in line.split()] + # 将缺失值'?'替换为0(或者你可以选择其他适当的处理方式) features = [float(part) if part != '?' else 0 for part in parts[:-1]] - label = int(parts[-1]) - 1 # 假设标签是{1, 2},转换为{0, 1} + # 将标签从2转换为1(如果原始标签是1和2的话) + label = int(parts[-1]) - 1 # 假设原始标签是1和2,转换为0和1 data.append(features + [label]) + + # 将数据转换为numpy数组 data_array = np.array(data) - X = data_array[:, :-1] - y = data_array[:, -1] - return X, y - -def evaluate_model(test_data, weights, bias, user_test_func=predict_logreg): - results = [] - for i in range(len(test_data)): - # 确保传递了正确的参数:test_data[i], weights, bias - result = user_test_func(test_data[i], weights, bias) - results.append(result) - # 根据需要对结果进行进一步处理,例如计算准确率等 - return results + + # 分离特征和标签 + X = data_array[:, :-1] # 所有行,除了最后一列 + y = data_array[:, -1] # 所有行的最后一列 + + return X, y if __name__ == "__main__": + # 加载训练集 X_train, y_train = load_data("./data/horse-colic.data") + + # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") - # 数据标准化(可选,但通常有助于模型性能) - X_train_mean = np.mean(X_train, axis=0) - X_train_std = np.std(X_train, axis=0) - X_train = (X_train - X_train_mean) / X_train_std - X_test = (X_test - X_train_mean) / X_train_std # 使用训练集的标准差来标准化测试集 + # 训练对率回归模型 + weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) - # 训练逻辑回归模型 - weights, bias = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, regularization=0.1) - - # 评估模型性能 - accuracy = evaluate_model(X_test, y_test, weights, bias) - print(f"测试集准确率: {accuracy:.2f}") - - # 测试单个数据(可选) - y_pred = predict_logreg(X_test[11], weights, bias) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") + # 测试单个数据 + y_pred = predict_logreg(X_test[11], weights) + print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file