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]}")