generated from LufsX/Experiment-LogReg
This commit is contained in:
+33
-16
@@ -1,36 +1,41 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.preprocessing import StandardScaler
|
from sklearn.preprocessing import StandardScaler
|
||||||
from sklearn.model_selection import KFold
|
from sklearn.model_selection import KFold
|
||||||
|
|
||||||
"""
|
"""
|
||||||
实验说明:
|
实验说明:
|
||||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||||
|
|
||||||
注意事项:
|
注意事项:
|
||||||
- 在 function.py 中实现对率回归算法。
|
- 在 function.py 中实现对率回归算法。
|
||||||
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
||||||
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
||||||
测评环境:
|
|
||||||
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
测评环境:
|
||||||
- OS: Ubuntu 22.04.4 LTS
|
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
||||||
- Python: 3.10.12
|
- OS: Ubuntu 22.04.4 LTS
|
||||||
- numpy: 2.1.1
|
- Python: 3.10.12
|
||||||
|
- numpy: 2.1.1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
"""
|
"""
|
||||||
请在这里填写学号和姓名,不填写即成绩无效。
|
请在这里填写学号和姓名,不填写即成绩无效。
|
||||||
"""
|
"""
|
||||||
id = "202210423117"
|
id = "202210423117"
|
||||||
name = "程悦"
|
name = "程悦"
|
||||||
|
|
||||||
|
|
||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1.0 / (1 + np.exp(-z))
|
return 1.0 / (1 + np.exp(-z))
|
||||||
|
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.05,
|
def train_logreg(X, y, learning_rate=0.05,
|
||||||
iterations=1500,
|
iterations=1500,
|
||||||
batch_size=64, regularization='L2', lambda_reg=0.05, cv=5):
|
batch_size=64, regularization='L2', lambda_reg=0.05, cv=5):
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
kf = KFold(n_splits=cv, shuffle=True)
|
kf = KFold(n_splits=cv, shuffle=True)
|
||||||
weights_list = []
|
weights_list = []
|
||||||
|
fold_scores = []
|
||||||
for train_index, val_index in kf.split(X):
|
for train_index, val_index in kf.split(X):
|
||||||
X_train, X_val = X[train_index], X[val_index]
|
X_train, X_val = X[train_index], X[val_index]
|
||||||
y_train, y_val = y[train_index], y[val_index]
|
y_train, y_val = y[train_index], y[val_index]
|
||||||
@@ -52,8 +57,20 @@ def train_logreg(X, y, learning_rate=0.05,
|
|||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
if i % 100 == 0:
|
if i % 100 == 0:
|
||||||
learning_rate *= 0.95
|
learning_rate *= 0.95
|
||||||
|
# 在验证集上计算准确率
|
||||||
|
predictions = [predict_logreg(x, weights) for x in X_val]
|
||||||
|
score = np.mean(predictions == y_val)
|
||||||
|
fold_scores.append(score)
|
||||||
weights_list.append(weights)
|
weights_list.append(weights)
|
||||||
return weights_list[-1]
|
# 对权重进行加权平均融合,权重为各折的准确率
|
||||||
|
total_score = sum(fold_scores)
|
||||||
|
if total_score == 0:
|
||||||
|
return weights_list[-1]
|
||||||
|
averaged_weights = np.zeros(n)
|
||||||
|
for weight, score in zip(weights_list, fold_scores):
|
||||||
|
averaged_weights += weight * score
|
||||||
|
averaged_weights /= total_score
|
||||||
|
return averaged_weights
|
||||||
|
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
@@ -92,8 +109,8 @@ def accuracy(X, y, weights):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
X_train, y_train = load_data("./horse - colic.data")
|
X_train, y_train = load_data("./horse-colic.data")
|
||||||
X_test, y_test = load_data("./horse - colic.test")
|
X_test, y_test = load_data("./horse-colic.test")
|
||||||
|
|
||||||
final_weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000, batch_size=32, regularization='None',
|
final_weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000, batch_size=32, regularization='None',
|
||||||
lambda_reg=0.01)
|
lambda_reg=0.01)
|
||||||
|
|||||||
Reference in New Issue
Block a user