【问题标题】:How can I make logistic regression(Gradient Decendent) Code? [closed]如何制作逻辑回归(梯度下降)代码? [关闭]
【发布时间】:2019-09-13 18:43:38
【问题描述】:

我是大一和初学者, 我在制作逻辑回归算法时遇到了麻烦。 我在我的教科书中附上了代码。我应该填写什么代码? 4~5行以内就好了。 非常感谢

from sklearn import datasets
import numpy as np
from sklearn.metrics import accuracy_score

X, y = datasets.make_classification(
    n_samples=200, n_features=2, random_state=333,
    n_informative=2, n_redundant=0, n_clusters_per_class=1)

def sigmoid(s):
    return 1 / (1 + np.exp(-s))

def loss(y, h):
    return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()

def gradient(X, y, w):
    return -(y * X) / (1 + np.exp(-y * np.dot(X, w)))


X_bias = np.append(np.ones((X.shape[0], 1)), X, axis=1)
y = np.array([[1] if label == 0 else [0] for label in y])
w = np.array([[random.uniform(-1, 1)] for _ in range(X.shape[1]+1)])
max_iter = 100
learning_rate = 0.1
threshold = 0.5

for _ in range(max_iter):

# fill in the blanks


probabilities = sigmoid(np.dot(X_bias, w))
predictions = [[1] if p > threshold else [0] for p in probabilities]
print("loss: %.2f, accuracy: %.2f" %
(loss(y, probabilities), accuracy_score(y, predictions)))

填空

【问题讨论】:

    标签: python machine-learning logistic-regression


    【解决方案1】:

    基本上很简单。

    定义假设函数:

    theta0 = 0
    theta1 = 0
    
    def hyp(x): return theta0 + theta1*x
    

    定义成本函数:

    def cost(hyp, x, y):
        total1 = 0
        total2 = 0
    
        for i in range(1, len(x)):
            total1 += hyp(x[i]) - y[i]
            total2 += (hyp(x[i]) - y[i]) * x[i]
    
    return total1 / len(x), total2 / len(x)
    

    调用函数:

    for i in range(50):
        s1, s2 = cost(hyp, x, y)
        theta1 = theta1 - alpha * s2
        theta0 = theta0 - alpha * s1
    

    学习参数将会更新。

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2015-07-15
      • 2014-03-21
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 2020-02-25
      相关资源
      最近更新 更多