generated from LufsX/Experiment-LogReg
This commit is contained in:
+11
-7
@@ -28,7 +28,7 @@ name = "章程鸿"
|
|||||||
# 这里写自己的代码实现
|
# 这里写自己的代码实现
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from sklearn.preprocessing import StandardScaler
|
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
|
||||||
from sklearn.model_selection import train_test_split, KFold
|
from sklearn.model_selection import train_test_split, KFold
|
||||||
|
|
||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
@@ -60,9 +60,8 @@ def train_logreg(X, y, learning_rate=0.1, iterations=2000, lambda_reg=0.01, batc
|
|||||||
# 更新权重
|
# 更新权重
|
||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
|
|
||||||
# 动态调整学习率,逐渐减小
|
# 动态调整学习率
|
||||||
if i % 100 == 0: # 每100次迭代输出一次学习率
|
learning_rate *= 0.99 # 每次迭代稍微减小学习率
|
||||||
print(f"Iteration {i}, Learning Rate: {learning_rate}")
|
|
||||||
|
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
@@ -102,11 +101,16 @@ def load_data(filepath):
|
|||||||
|
|
||||||
return data, labels
|
return data, labels
|
||||||
|
|
||||||
|
|
||||||
def preprocess_data(X):
|
def preprocess_data(X):
|
||||||
"""特征标准化。"""
|
"""特征标准化并增加多项式特征。"""
|
||||||
scaler = StandardScaler()
|
scaler = StandardScaler()
|
||||||
return scaler.fit_transform(X)
|
X_scaled = scaler.fit_transform(X)
|
||||||
|
|
||||||
|
# 添加多项式特征
|
||||||
|
poly = PolynomialFeatures(degree=2, include_bias=False)
|
||||||
|
X_poly = poly.fit_transform(X_scaled)
|
||||||
|
|
||||||
|
return X_poly
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载数据集
|
# 加载数据集
|
||||||
|
|||||||
Reference in New Issue
Block a user