generated from LufsX/Experiment-LogReg
This commit is contained in:
+15
-34
@@ -1,32 +1,10 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||
|
||||
注意事项:
|
||||
- 在 function.py 中实现对率回归算法。
|
||||
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
||||
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
||||
|
||||
测评环境:
|
||||
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
||||
- OS: Ubuntu 22.04.4 LTS
|
||||
- Python: 3.10.12
|
||||
- numpy: 2.1.1
|
||||
"""
|
||||
|
||||
|
||||
"""
|
||||
请在这里填写学号和姓名,不填写即成绩无效。
|
||||
"""
|
||||
id = "202210423124"
|
||||
name = "刘子豪"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, reg_lambda=0.01):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
|
||||
@@ -35,6 +13,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
- y (numpy array): 标签数据。
|
||||
- learning_rate (float): 学习率。
|
||||
- iterations (int): 迭代次数。
|
||||
- reg_lambda (float): 正则化强度。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
@@ -47,11 +26,12 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
predictions = 1 / (1 + np.exp(-z)) # 计算sigmoid
|
||||
errors = y - predictions
|
||||
gradient = np.dot(X.T, errors) / m
|
||||
# 加入正则化
|
||||
gradient -= reg_lambda * weights / m
|
||||
weights += learning_rate * gradient
|
||||
|
||||
return weights
|
||||
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""
|
||||
对率回归预测函数。
|
||||
@@ -67,8 +47,6 @@ def predict_logreg(inX, weights):
|
||||
probability = 1 / (1 + np.exp(-z))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据。
|
||||
@@ -86,26 +64,29 @@ def load_data(filepath):
|
||||
line_data = line.strip().split()
|
||||
if line_data:
|
||||
try:
|
||||
data. append([float(x) if x !='?'else np. nan for x in line_data])
|
||||
data.append([float(x) if x !='?' else np.nan for x in line_data])
|
||||
except ValueError as e:
|
||||
print(f"Error processing line: {line}. Error: {e}")
|
||||
data = np.array(data)
|
||||
data = np.nan_to_num(data,nan=np.nanmean(data,axis=0))#将缺失值用平均值代替
|
||||
X = data[:,:-1]#特征
|
||||
y=data[:,-1]#标签
|
||||
data = np.nan_to_num(data, nan=np.nanmean(data, axis=0)) # 将缺失值用平均值代替
|
||||
X = data[:, :-1] # 特征
|
||||
y = data[:, -1] # 标签
|
||||
return X, y
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
|
||||
# 特征标准化
|
||||
scaler = StandardScaler()
|
||||
X_train = scaler.fit_transform(X_train)
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
X_test = scaler.transform(X_test) # 使用同一标准化参数
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, reg_lambda=0.01)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
|
||||
Reference in New Issue
Block a user