generated from LufsX/Experiment-LogReg
This commit is contained in:
+19
-8
@@ -1,5 +1,6 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
@@ -20,8 +21,8 @@ import numpy as np
|
||||
"""
|
||||
请在这里填写学号和姓名,不填写即成绩无效。
|
||||
"""
|
||||
id = "202210423213"
|
||||
name = "熊浩燃"
|
||||
id = "202210423xxx"
|
||||
name = "test_name"
|
||||
|
||||
def sigmoid(z):
|
||||
return 1 / (1 + np.exp(-z))
|
||||
@@ -30,9 +31,14 @@ def standardize(X):
|
||||
""" 数据标准化:均值为0,标准差为1 """
|
||||
return (X - np.mean(X, axis=0)) / (np.std(X, axis=0) + 1e-8) # 避免除零
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.0005, iterations=3000, batch_size=32, lambda_=0.05):
|
||||
def add_polynomial_features(X, degree=2):
|
||||
""" 增加多项式特征 """
|
||||
poly = PolynomialFeatures(degree)
|
||||
return poly.fit_transform(X)
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.0001, iterations=5000, batch_size=32, lambda_=0.1, degree=2):
|
||||
"""
|
||||
对率回归训练函数,包含批量梯度下降和L2正则化。
|
||||
对率回归训练函数,包含批量梯度下降和L2正则化,并增加多项式特征。
|
||||
|
||||
Args:
|
||||
- X (numpy array): 特征数据。
|
||||
@@ -41,11 +47,13 @@ def train_logreg(X, y, learning_rate=0.0005, iterations=3000, batch_size=32, lam
|
||||
- iterations (int): 迭代次数。
|
||||
- batch_size (int): 每次更新的样本数。
|
||||
- lambda_ (float): L2正则化项的系数。
|
||||
- degree (int): 多项式特征的次数。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
X = standardize(X) # 使用标准化数据
|
||||
X = standardize(X) # 标准化数据
|
||||
X = add_polynomial_features(X, degree=degree) # 增加多项式特征
|
||||
m, n = X.shape
|
||||
weights = np.random.rand(n) * 0.01 # 初始化权重为小的随机数
|
||||
|
||||
@@ -66,17 +74,20 @@ def train_logreg(X, y, learning_rate=0.0005, iterations=3000, batch_size=32, lam
|
||||
|
||||
return weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
def predict_logreg(inX, weights, degree=2):
|
||||
"""
|
||||
对率回归预测函数。
|
||||
|
||||
Args:
|
||||
- inX (numpy array): 输入待分类的样本。
|
||||
- weights (numpy array): 训练得到的权重。
|
||||
- degree (int): 多项式特征的次数。
|
||||
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
inX = standardize(inX.reshape(1, -1)) # 标准化输入
|
||||
inX = add_polynomial_features(inX, degree=degree) # 增加多项式特征
|
||||
probability = sigmoid(np.dot(inX, weights))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
|
||||
@@ -107,8 +118,8 @@ if __name__ == "__main__":
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.0005, iterations=3000, batch_size=32, lambda_=0.05)
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.0001, iterations=5000, batch_size=32, lambda_=0.1, degree=2)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
y_pred = predict_logreg(X_test[11], weights, degree=2)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
|
||||
Reference in New Issue
Block a user