From 817111606bf0ef122077ed50da5d07568792727b Mon Sep 17 00:00:00 2001 From: 202210423104 <1961532386@qq.com> Date: Sun, 3 Nov 2024 16:57:52 +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 | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/function.py b/function.py index 4211bad..a00a9fa 100644 --- a/function.py +++ b/function.py @@ -9,7 +9,7 @@ def load_data(filepath): data = [] with open(filepath, 'r') as file: for line in file: - line_data = line.strip().split(',') + line_data = line.strip().split() if line_data: try: data.append([float(x) if x != '?' else np.nan for x in line_data]) @@ -31,7 +31,7 @@ def preprocess_data(X): def train_logreg(X, y): X_processed = preprocess_data(X) # 预处理训练数据 - model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42) + model = RandomForestClassifier(n_estimators=200, max_depth=20, random_state=42) model.fit(X_processed, y) return model # 返回训练好的模型 @@ -43,19 +43,4 @@ def predict_logreg(X, model): return model.predict(X_processed)[0] # 确保 model 是训练好的模型 if __name__ == "__main__": - # 加载训练集 - X_train, y_train = load_data("./data/horse-colic.data") - - # 加载测试集 - X_test, y_test = load_data("./data/horse-colic.test") - - # 训练对率回归模型 - model = train_logreg(X_train, y_train) - - # 确保测试集中有数据 - if X_test.shape[0] > 0: - # 预测测试集的第一个样本 - y_pred = predict_logreg(X_test[0], model) - print(f"预测结果: {y_pred}, 实际结果: {y_test[0]}") - else: - print("测试集没有数据,无法进行预测。") + X, y = load_data("./data/horse-colic.data")