From f749c67eef7ed3e9170bb44d24ab28b8b2688436 Mon Sep 17 00:00:00 2001 From: 202210423104 <1961532386@qq.com> Date: Sun, 3 Nov 2024 16:35:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/function.py b/function.py index 0e417d3..461de10 100644 --- a/function.py +++ b/function.py @@ -1,6 +1,5 @@ import numpy as np from sklearn.ensemble import RandomForestClassifier -from sklearn.model_selection import train_test_split, cross_val_score from sklearn.preprocessing import PolynomialFeatures, StandardScaler id = "202210423104" @@ -27,22 +26,14 @@ def preprocess_data(X): return X_poly def train_logreg(X, y): - X_processed = preprocess_data(X) - model = RandomForestClassifier(n_estimators=200, max_depth=15, random_state=42) # 改变参数 + X_processed = preprocess_data(X) # 预处理训练数据 + model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42) model.fit(X_processed, y) - return model + return model # 返回训练好的模型 def predict_logreg(model, X): - if len(X.shape) == 1: - X = X.reshape(1, -1) - X_processed = preprocess_data(X) - return model.predict(X_processed)[0] + X_processed = preprocess_data(X) # 预处理测试数据 + return model.predict(X_processed)[0] # 返回预测结果 if __name__ == "__main__": X, y = load_data("./data/horse-colic.data") - model = train_logreg(X, y) - - # 交叉验证 - X_processed = preprocess_data(X) - cv_scores = cross_val_score(model, X_processed, y, cv=5) - print(f"交叉验证准确率: {np.mean(cv_scores) * 100:.2f}%")