1

I just ended my learning about regression and numpy and pandas in python and when I try my code with Gradient Descent with the dataset it gives me the right thetas and with another, it gave the thetas as none (i tried it with Normal Equation and it worked but I want to know all the ways) and this is link of the dataset that did not work.

import numpy as np
import pandas as pd 
def reg1():
    data=pd.read_csv('D:\\New folder (4)\\02.txt',header=None,names=['x','y'])
    data=data.dropna()
    data.insert(0,'x0',1)
    cols=data.shape[1]
    x=np.matrix(data.iloc[:,0:cols-1].values)
    y=np.matrix(data.iloc[:,cols-1:].values)
    theta=np.matrix([0,0])
    def computecost(x,y,theta):
        z= np.power((x*theta.T-y),2)
        return (sum(z)/(2*len(x)))[0,0]
#this is the gradient descent fun
    def gd(x,y,theta,alpha,iters):
        temp=np.zeros(theta.shape)
        par=int(theta.shape[1])
        for i in range(iters):
            error=((x*theta.T)-y)
            for j in range(par):
                term=np.multiply(error,x[:,j])
                temp[0,j]=theta[0,j]-((alpha/len(x))*np.sum(term))
            theta=temp
        return(theta)
    alpha=0.01
    iters=1000
    return gd(x,y,theta,alpha,iters)
theta=reg1()
print(theta)

I expect the output to be like [[-0.10726546 1.00065638]] but it gave me [[nan nan]]

  • Have you checked that the dataset you used has values of type int or float? If not you can cast those values to float by `y=y.astype(np.float64)` – Rudresh Dixit Jun 30 '19 at 03:38
  • 1
    i did not and i dont think it is necessary becuase i found the problem and it was that alpha's value should be 0.001 not 0.01 but iam really happy because you tried to help me thank you veru much – ISLAM ESSAM Jun 30 '19 at 05:05

1 Answers1

0

for who have same problem

alpha's value should be 0.0001 not 0.01

but this is in my dataset

in another one you sholud change alpha's value untill you have the best compute cost(error cost).

  • As far as I know, In a linear regression problem, if you reach a minimum of the cost function, it'll be the only minimum of the cost function. So, it's one thing to change alpha until you have a solution (instead of nan), but it's another thing if the value of alpha changes the solution... – Itamar Mushkin Jun 30 '19 at 05:29
  • yup it is the only mininmum because of that i think that any one should change the aplha and iters untill he have the min but in my case i had nan – ISLAM ESSAM Jun 30 '19 at 06:33