generated from LufsX/Experiment-LogReg
This commit is contained in:
+5
-7
@@ -44,12 +44,13 @@ def load_data(filepath):
|
||||
# 分离特征和标签
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
|
||||
# 标准化特征数据
|
||||
X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
|
||||
|
||||
return X, y
|
||||
|
||||
|
||||
|
||||
# 这里实现对率回归训练函数
|
||||
def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_strength=0.01):
|
||||
def train_logreg(X, y, learning_rate=0.001, num_iterations=5000, regularization_strength=0.001):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
|
||||
@@ -70,8 +71,6 @@ def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_s
|
||||
gradient = (1 / m) * np.dot(X.T, (predictions - y)) + regularization_strength * weights
|
||||
weights -= learning_rate * gradient
|
||||
return weights
|
||||
|
||||
|
||||
# 这里实现对率回归预测函数
|
||||
def predict_logreg(inX, weights):
|
||||
"""
|
||||
@@ -86,7 +85,6 @@ def predict_logreg(inX, weights):
|
||||
"""
|
||||
probability = 1 / (1 + np.exp(-np.dot(inX, weights)))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
# 加载训练集
|
||||
|
||||
Reference in New Issue
Block a user