2017-07-26 5 views
0

J'écris actuellement une implémentation de régression linéaire univariée sur python:univariée régression linéaire sortie NaN

# implementation of univariate linear regression 
import numpy as np 


def cost_function(hypothesis, y, m): 
    return (1/(2 * m)) * ((hypothesis - y) ** 2).sum() 


def hypothesis(X, theta): 
    return X.dot(theta) 


def gradient_descent(X, y, theta, m, alpha): 
    for i in range(1500): 
    temp1 = theta[0][0] - alpha * (1/m) * (hypothesis(X, theta) - y).sum() 
    temp2 = theta[1][0] - alpha * (1/m) * ((hypothesis(X, theta) - y) * X[:, 1]).sum() 
    theta[0][0] = temp1 
    theta[1][0] = temp2 

    return theta 

if __name__ == '__main__': 
    data = np.loadtxt('data.txt', delimiter=',') 

    y = data[:, 1] 
    m = y.size 
    X = np.ones(shape=(m, 2)) 
    X[:, 1] = data[:, 0] 
    theta = np.zeros(shape=(2, 1)) 
    alpha = 0.01 

    print(gradient_descent(X, y, theta, m, alpha)) 

Ce code affichera NaN thêta, après avoir à l'infini - je ne peux pas comprendre ce qui se passe mal, mais c'est sûrement quelque chose à voir avec mon changement de thêta dans la fonction de descente en dégradé.

Les données que j'utilise sont un simple jeu de données de paires de régressions linéaires que j'ai mises en ligne - et qui se charge correctement.

Quelqu'un peut-il me diriger dans la bonne direction?

Répondre

0

Le problème que vous voyez est que lorsque vous faites X[:,1] ou data[:,1], vous obtenez des objets de forme (m,). Lorsque vous multipliez un objet de forme (m,) avec une matrice de forme (m, 1), vous obtenez une matrice de taille (m, m)

a = np.array([1,2,3]) 
b = np.array([[4],[5],[6]]) 
(a*b).shape #prints (3,3) 

Si vous y = y.reshape ((m, 1)) dans votre bloc if __name__ et à l'intérieur de votre fonction gradient_descent vous faire

X_1 = X[:,1].reshape((m,1)) 

devrait résoudre le problème. En ce moment ce qui se passe est que lorsque vous faites

((hypothesis(X, theta) - y) * X[:, 1]) 

vous obtenez 100 par 100 matrice, ce qui est pas ce que vous voulez.

code complet I utilisé pour le test est:

# implementation of univariate linear regression 
import numpy as np 


def cost_function(hypothesis, y, m): 
    return (1/(2 * m)) * ((hypothesis - y) ** 2).sum() 


def hypothesis(X, theta): 
    return X.dot(theta) 


def gradient_descent(X, y, theta, m, alpha): 
    X_1 = X[:,1] 
    X_1 = X_1.reshape((m,1)) 
    for i in range(1500): 
    temp1 = theta[0][0] - alpha * (1/m) * (hypothesis(X, theta) - y).sum() 
    temp2 = theta[1][0] - alpha * (1/m) * ((hypothesis(X, theta) - y) * X_1).sum() 
    theta[0][0] = temp1 
    theta[1][0] = temp2 

    return theta 

if __name__ == '__main__': 
    data= np.random.normal(size=(100,2)) 

    y = 30*data[:,0] + data[:, 1] 
    m = y.size 
    X = np.ones(shape=(m, 2)) 
    y = y.reshape((m,1)) 
    X[:, 1] = data[:, 0] 
    theta = np.zeros(shape=(2, 1)) 
    alpha = 0.01 

    print(gradient_descent(X, y, theta, m, alpha)) 
+0

Malheureusement, cela ne semble pas résoudre le problème - même sortie que précédemment. – bag

+0

Vous devez également remodeler X [:, 1]. Je vais éditer –

+0

Toujours pas de chance! La même sortie qu'avant. J'ai inséré y = y.reshape ((m, 1)) directement après la déclaration de m dans le bloc if __name__, et x1 = X [:, 1] .reshape ((m, 1)) en haut de Descente graduelle. – bag