From adc8e0771b3e2ebcd28b1dcd74ea7f67570414d5 Mon Sep 17 00:00:00 2001 From: 202210423104 <1961532386@qq.com> Date: Sun, 3 Nov 2024 16:51:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/function.py b/function.py index e7c938e..e3015ab 100644 --- a/function.py +++ b/function.py @@ -16,13 +16,8 @@ import numpy as np - numpy: 2.1.1 """ -""" -请在这里填写学号和姓名,不填写即成绩无效。 -姓名请直接**填写中文** -例如: name = "张三" -""" -id = "202210423xxx" -name = "test_name" +id = "202210423104" +name = "张钦诚" # 训练对率回归模型 @@ -46,9 +41,7 @@ def predict_logreg(inX, weights): # 数据加载函数 def load_data(filepath): - data = np.genfromtxt(filepath, delimiter=' ', dtype=float, filling_values=np.nan) - - # 去掉包含NaN的行 + data = np.genfromtxt(filepath, delimiter=',', dtype=float, filling_values=np.nan) # 根据实际分隔符调整 data = data[~np.isnan(data).any(axis=1)] X = data[:, :-1] # 特征 @@ -67,12 +60,8 @@ if __name__ == "__main__": print(f"X_train shape: {X_train.shape}, y_train shape: {y_train.shape}") print(f"X_test shape: {X_test.shape}, y_test shape: {y_test.shape}") - # 确保测试集有足够的数据进行预测 if X_test.shape[0] > 0: - # 训练对率回归模型 weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) - - # 测试单个数据,确保索引有效 index_to_test = 0 if X_test.shape[0] == 1 else 11 y_pred = predict_logreg(X_test[index_to_test], weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[index_to_test]}")