generated from LufsX/Experiment-LogReg
This commit is contained in:
+16
-12
@@ -3,7 +3,7 @@ from sklearn.ensemble import RandomForestClassifier
|
|||||||
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
|
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
|
||||||
from sklearn.impute import SimpleImputer
|
from sklearn.impute import SimpleImputer
|
||||||
from sklearn.metrics import accuracy_score
|
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"
|
id = "202210423104"
|
||||||
name = "张钦诚"
|
name = "张钦诚"
|
||||||
@@ -38,15 +38,20 @@ def preprocess_data(X):
|
|||||||
|
|
||||||
def train_logreg(X, y):
|
def train_logreg(X, y):
|
||||||
X_processed = preprocess_data(X) # 预处理训练数据
|
X_processed = preprocess_data(X) # 预处理训练数据
|
||||||
model = RandomForestClassifier(
|
|
||||||
n_estimators=300, # 增加树的数量
|
# 超参数调优
|
||||||
max_depth=25, # 调整最大深度
|
param_grid = {
|
||||||
min_samples_split=5,
|
'n_estimators': [100, 200, 300],
|
||||||
class_weight='balanced', # 处理类别不平衡
|
'max_depth': [None, 15, 25],
|
||||||
random_state=42
|
'min_samples_split': [2, 5, 10],
|
||||||
)
|
'class_weight': ['balanced', None]
|
||||||
model.fit(X_processed, y)
|
}
|
||||||
return model # 返回训练好的模型
|
|
||||||
|
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):
|
def predict_logreg(X, model):
|
||||||
@@ -57,7 +62,6 @@ def predict_logreg(X, model):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 数据加载和预处理保持不变
|
|
||||||
X, y = load_data("./data/horse-colic.data")
|
X, y = load_data("./data/horse-colic.data")
|
||||||
|
|
||||||
# 划分训练集和测试集
|
# 划分训练集和测试集
|
||||||
@@ -71,4 +75,4 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# 计算准确率
|
# 计算准确率
|
||||||
accuracy = accuracy_score(y_test, y_pred)
|
accuracy = accuracy_score(y_test, y_pred)
|
||||||
print(f"Accuracy: {accuracy:.2f}")
|
print(f"Accuracy: {accuracy:.2f}")
|
||||||
|
|||||||
Reference in New Issue
Block a user