Логистическая регрессия: реализация Python (курс ML Эндрю Нг) - PullRequest
0 голосов
/ 27 февраля 2019

Я пытался реализовать регуляризованную версию логистической регрессии в python, используя структуру, аналогичную программе в октаве, за исключением того, что я также написал Gradient Descent.Дело в том, что я озадачен тем, почему стоимостные показатели имеют тенденцию увеличиваться, почему снижаются до определенного значения.Предложения будут высоко оценены.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataset = pd.read_csv('G:\Coding\python\Machine Learning\Andrew_Python\Logistic Regression\ex2data2.txt',header=None)

data = dataset.values
[m,n] = dataset.shape

X = np.append( np.ones((m,1)) , data[:,:n-1].reshape(m,n-1), axis=1)
y = data[:,n-1].reshape(m,1)

y1 = np.asarray(np.where(y==1)[0])
y0 = np.asarray(np.where(y==0)[0])

plt.plot(X[y1,1],X[y1,2],'bo',X[y0,1],X[y0,2],'ro')
plt.xlabel('Chip 1')
plt.ylabel('Chip 2')
plt.legend(['Approved','Not Approved'])
plt.show()

def mapFeature(X):
    out = np.ones([m,1])
    X1 = X[:,1].reshape(m,1)
    X2 = X[:,2].reshape(m,1)
    for i in range(1,7):
        for j in range(0,i+1):
            out = np.append(out, np.multiply(np.power(X1,i-j),np.power(X2,j)), axis=1)
    return out

X_reg = np.matrix(mapFeature(X))

m,n = np.shape(X_reg)

theta = np.zeros([n,1])

print(X_reg.shape)
print(y.shape)

lmda =1

def sigmoid(theta,X):
    h = np.dot(X,theta)
    return 1/ (1 + np.exp(-h))

def computeCost(theta,X,y,lmda = 0.): 
    H = sigmoid( theta, X)
    term1 = np.dot( -y.T , np.log(H))
    term2 = np.dot( 1-y.T , np.log(1-H))
    reg = np.dot(theta[1:].T, theta[1:])*lmda/2
    J = np.sum(term1 - term2 + reg)/m
    return J

alpha = 1 

def gradientDescent(X, y, theta, m, lmda, iter, alpha):
    J = []
    for i in range(1,400):
        H = sigmoid(theta, X)
        theta_reg = theta
        theta_reg[0,0]=0
        theta = theta - np.dot( np.transpose(X), H - y )*alpha/m + ( theta_reg )*(alpha*lmda)/m
        J.append( computeCost(theta, X,y, lmda) )
    return theta,J
theta,J = gradientDescent(X_reg,y,theta,m,lmda,iter,alpha)

Ссылка на используемые данные (не мои): https://github.com/SaveTheRbtz/ml-class/blob/master/ex2/ex2data2.txt

Редактировать: Я решил проблему, избавившись от theta_reg и заменив ее на

theta0 = np.dot(X.transpose() , (H-y))[0]
theta1 = np.dot(X.transpose() , (H-y))[1:] + lmda*theta[1:]
grad = np.append( theta0 , theta1 , axis=0)
theta = theta - (grad)*alpha/m

Дает ожидаемый ответ, но я до сих пор не понял, почему theta_reg не работает.

...