generated from LufsX/Experiment-LogReg
This commit is contained in:
+1
-40
@@ -31,12 +31,7 @@ from sklearn.preprocessing import StandardScaler
|
||||
def sigmoid(intX): #激活函数,逻辑回归中,将线性组合转换为概率
|
||||
return 1.0 / (1 + np.exp(-intX))
|
||||
|
||||
def train_logreg(X, y, numIter=150): #训练逻辑回归模型的函数,使用梯度下降算法。
|
||||
def sigmoid(z):
|
||||
return 1.0 / (1 + np.exp(-z))
|
||||
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64):
|
||||
def train_logreg(X, y, numIter=150): # 训练逻辑回归模型的函数,使用梯度下降算法。
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n) # 初始化权重为0而不是1
|
||||
for j in range(numIter):
|
||||
@@ -49,43 +44,9 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64):
|
||||
weights = weights + alpha * error * X[randIndex]
|
||||
del(dataIndex[randIndex])
|
||||
return weights
|
||||
weights = np.random.randn(n) * 0.01
|
||||
best_weights = weights.copy()
|
||||
best_accuracy = 0
|
||||
wait = 0
|
||||
|
||||
for i in range(iterations):
|
||||
indices = np.random.permutation(m)
|
||||
X_shuffled = X[indices]
|
||||
y_shuffled = y[indices]
|
||||
|
||||
for j in range(0, m, batch_size):
|
||||
X_batch = X_shuffled[j:j + batch_size]
|
||||
y_batch = y_shuffled[j:j + batch_size]
|
||||
z = np.dot(X_batch, weights)
|
||||
h = sigmoid(z)
|
||||
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
|
||||
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
# 计算训练集准确率
|
||||
train_accuracy = accuracy(X, y, weights)
|
||||
if train_accuracy > best_accuracy:
|
||||
best_accuracy = train_accuracy
|
||||
best_weights = weights.copy()
|
||||
wait = 0
|
||||
else:
|
||||
wait += 1
|
||||
|
||||
return best_weights
|
||||
|
||||
|
||||
def predict_logreg(X, weights):
|
||||
predictions = []
|
||||
for x in X:
|
||||
prob = sigmoid(np.dot(x, weights))
|
||||
predictions.append(1 if prob > 0.5 else 0)
|
||||
return predictions
|
||||
z = np.dot(X, weights)
|
||||
h = sigmoid(z)
|
||||
return np.where(h > 0.5, 1, 0)
|
||||
|
||||
Reference in New Issue
Block a user