generated from LufsX/Experiment-LogReg
This commit is contained in:
+32
-9
@@ -1,8 +1,11 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.model_selection import train_test_split, GridSearchCV
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.metrics import accuracy_score, roc_auc_score
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.ensemble import VotingClassifier
|
||||
"""
|
||||
实验说明:
|
||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||
@@ -28,7 +31,6 @@ name = "廖子豪"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据。
|
||||
@@ -136,18 +138,39 @@ if __name__ == "__main__":
|
||||
|
||||
# 训练对率回归模型,使用早停法
|
||||
best_weights = None
|
||||
best_accuracy = 0
|
||||
best_auc = 0
|
||||
for iter in range(1, 1001):
|
||||
weights = train_logreg(X_train, y_train, numIter=iter, learning_rate=0.01, lambda_reg=0.1)
|
||||
y_pred = [predict_logreg(x, weights) for x in X_val]
|
||||
accuracy = accuracy_score(y_val, y_pred)
|
||||
if accuracy > best_accuracy:
|
||||
best_accuracy = accuracy
|
||||
auc = roc_auc_score(y_val, y_pred)
|
||||
if auc > best_auc:
|
||||
best_auc = auc
|
||||
best_weights = weights
|
||||
if iter > 10 and accuracy == best_accuracy:
|
||||
if iter > 10 and auc == best_auc:
|
||||
break # 早停
|
||||
|
||||
# 使用最佳权重在测试集上评估模型
|
||||
y_pred_test = [predict_logreg(x, best_weights) for x in X_test]
|
||||
accuracy_test = accuracy_score(y_test, y_pred_test)
|
||||
print(f"Test Accuracy: {accuracy_test}")
|
||||
print(f"Test Accuracy: {accuracy_test}")
|
||||
|
||||
# 随机森林模型
|
||||
param_grid = {
|
||||
'n_estimators': [100, 200, 300],
|
||||
'max_depth': [5, 10, 15],
|
||||
'learning_rate': [0.01, 0.05, 0.1]
|
||||
}
|
||||
rf = RandomForestClassifier()
|
||||
grid_search = GridSearchCV(estimator=rf, param_grid=param_grid, cv=5, scoring='accuracy')
|
||||
grid_search.fit(X_train, y_train)
|
||||
best_rf = grid_search.best_estimator_
|
||||
print("Best parameters:", grid_search.best_params_)
|
||||
print("Best accuracy:", grid_search.best_score_)
|
||||
|
||||
# 集成学习 - 投票分类器
|
||||
log_clf = LogisticRegression()
|
||||
rnd_clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
||||
voting_clf = VotingClassifier(estimators=[('lr', log_clf), ('rf', rnd_clf)], voting='soft')
|
||||
voting_clf.fit(X_train, y_train)
|
||||
y_pred = voting_clf.predict(X_test)
|
||||
print("Voting classifier accuracy:", accuracy_score(y_test, y_pred))
|
||||
Reference in New Issue
Block a user