Почему моя логистическая регрессия предсказывает либо для 0, либо для 1 - PullRequest
1 голос
/ 22 мая 2019

Когда я запускаю свой код, он генерирует тета для 0 или 1 в качестве альтернативы. Это как Первый запуск кода: предсказывает только 0 Второй запуск кода: предсказывает только 1

class logistic_regression():        
    def H(self, X, theta):
        z = np.dot( X, theta)
        return 1 / (1 + np.exp(-z))

    def cost_function(self, X, theta, y):
        h = self.H(X , theta)
        return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()

    def gradient_descent(self, X, y, theta, iters, alpha):
        self.cost = np.zeros(iters)
        h = self.H(X , theta)
        gradient = np.dot( X.T, ( h - y )) / y.shape[0]
        for i in range(iters):
            theta -= alpha * gradient
            self.cost[i] = self.cost_function(X, theta, y)
        return theta, cost

    def predictions(self, X, theta, threshold=0.5):
        return self.H(X, theta) >= threshold

#applying the code

lr = logistic_regression()
new_theta, cost = lr.gradient_descent(X, y, theta, iters, alpha)

result = lr.H(X, new_theta)

print(new_theta)
print('\n')
print(result)

#output 1st time
[  -2.6197013    -8.62233216 -107.01170844]


[0. 0. 0. ... 0. 0. 0.]

#output 2nd time
[  8.55507128 139.32508563 129.80393891]


[1. 1. 1. ... 1. 1. 1.]

Нет сообщения об ошибке, но оно не работает, как ожидалось

...