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]]