diff --git a/function.py b/function.py index 68185f2..d2bd3bb 100644 --- a/function.py +++ b/function.py @@ -3,7 +3,7 @@ from sklearn.ensemble import RandomForestClassifier from sklearn.preprocessing import PolynomialFeatures, StandardScaler from sklearn.impute import SimpleImputer from sklearn.metrics import accuracy_score -from sklearn.model_selection import train_test_split +from sklearn.model_selection import train_test_split, GridSearchCV id = "202210423104" name = "张钦诚" @@ -38,15 +38,20 @@ def preprocess_data(X): def train_logreg(X, y): X_processed = preprocess_data(X) # 预处理训练数据 - model = RandomForestClassifier( - n_estimators=300, # 增加树的数量 - max_depth=25, # 调整最大深度 - min_samples_split=5, - class_weight='balanced', # 处理类别不平衡 - random_state=42 - ) - model.fit(X_processed, y) - return model # 返回训练好的模型 + + # 超参数调优 + param_grid = { + 'n_estimators': [100, 200, 300], + 'max_depth': [None, 15, 25], + 'min_samples_split': [2, 5, 10], + 'class_weight': ['balanced', None] + } + + model = RandomForestClassifier(random_state=42) + grid_search = GridSearchCV(model, param_grid, cv=5, scoring='accuracy', n_jobs=-1) + grid_search.fit(X_processed, y) + + return grid_search.best_estimator_ # 返回最佳模型 def predict_logreg(X, model): @@ -57,7 +62,6 @@ def predict_logreg(X, model): if __name__ == "__main__": - # 数据加载和预处理保持不变 X, y = load_data("./data/horse-colic.data") # 划分训练集和测试集 @@ -71,4 +75,4 @@ if __name__ == "__main__": # 计算准确率 accuracy = accuracy_score(y_test, y_pred) - print(f"Accuracy: {accuracy:.2f}") \ No newline at end of file + print(f"Accuracy: {accuracy:.2f}")