generated from LufsX/Experiment-LogReg
This commit is contained in:
+12
-21
@@ -3,12 +3,11 @@ 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, GridSearchCV
|
from sklearn.model_selection import train_test_split
|
||||||
|
|
||||||
id = "202210423104"
|
id = "202210423104"
|
||||||
name = "张钦诚"
|
name = "张钦诚"
|
||||||
|
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
data = []
|
data = []
|
||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
@@ -27,7 +26,6 @@ def load_data(filepath):
|
|||||||
y = data[:, -1] # 标签
|
y = data[:, -1] # 标签
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
|
|
||||||
def preprocess_data(X):
|
def preprocess_data(X):
|
||||||
scaler = StandardScaler()
|
scaler = StandardScaler()
|
||||||
X_standardized = scaler.fit_transform(X)
|
X_standardized = scaler.fit_transform(X)
|
||||||
@@ -35,24 +33,17 @@ def preprocess_data(X):
|
|||||||
X_poly = poly.fit_transform(X_standardized)
|
X_poly = poly.fit_transform(X_standardized)
|
||||||
return X_poly
|
return X_poly
|
||||||
|
|
||||||
|
|
||||||
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, # 增加树的数量
|
||||||
param_grid = {
|
max_depth=25, # 调整最大深度
|
||||||
'n_estimators': [100, 200, 300],
|
min_samples_split=5,
|
||||||
'max_depth': [None, 15, 25],
|
class_weight='balanced', # 处理类别不平衡
|
||||||
'min_samples_split': [2, 5, 10],
|
random_state=42 # 固定随机种子
|
||||||
'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):
|
||||||
if len(X.shape) == 1:
|
if len(X.shape) == 1:
|
||||||
@@ -60,11 +51,11 @@ def predict_logreg(X, model):
|
|||||||
X_processed = preprocess_data(X) # 预处理测试数据
|
X_processed = preprocess_data(X) # 预处理测试数据
|
||||||
return model.predict(X_processed)[0] # 确保 model 是训练好的模型
|
return model.predict(X_processed)[0] # 确保 model 是训练好的模型
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# 数据加载和预处理保持不变
|
||||||
X, y = load_data("./data/horse-colic.data")
|
X, y = load_data("./data/horse-colic.data")
|
||||||
|
|
||||||
# 划分训练集和测试集
|
# 划分训练集和测试集时固定随机种子
|
||||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||||
|
|
||||||
# 训练模型
|
# 训练模型
|
||||||
|
|||||||
Reference in New Issue
Block a user