What is the error? accuarcy is 1% harwork pays off neither i am able to submit it again then i will i know the accuracy?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dfx = pd.read_csv(‘C:/Users/HP/Desktop/ml/Project 1/Linear_X_Train.csv’)
dfy = pd.read_csv(‘C:/Users/HP/Desktop/ml/Project 1/Linear_Y_Train.csv’)
x = dfx.values
y = dfy.values
#print(x.shape)
#print(x)
#plt.scatter(x,y)
#plt.show()
x = x.reshape((-1,))
y = y.reshape((-1,))
#print(x.shape)
#print(x)
X = (x-x.mean())/x.std()
Y = y
#plt.scatter(X,Y)
#plt.show()

def hypothesis(x,theta):
return(theta[0]+theta[1]*x)

def error(x,y,theta):
m=x.shape[0]
error=0
for i in range(m):
h=hypothesis(x,theta)
error+=(h-y[i])**2
return error

def gradient(x,y,theta):
m=x.shape[0]
grad=np.zeros((2,))
for i in range(m):
h=hypothesis(x[i],theta)
grad[0]=(h-y[i])
grad[1]=(h-y[i])*x[i]
return grad

def gradient_descent(x,y,k=.001):
theta=np.zeros((2,))
m=x.shape[0]
itr=100
err_list=[]
for i in range(itr):
grad=gradient(x,y,theta)
e=error(x,y,theta)
err_list.append(e)
theta[0]=theta[0]-kgrad[0]
theta[1]=theta[1]-k
grad[1]
return theta,err_list

def prediction(x,theta):
m=x.shape[0]
Y_Predicted=[]
for i in range(m):
y=hypothesis(x[i],theta)
Y_Predicted.append(y)
Y_Predicted=np.array(Y_Predicted)
return Y_Predicted

final_theta=np.zeros((2,))
final_theta, error_list = gradient_descent(X,Y)
#plt.plot(final_theta)
#plt.plot(error_list)
#plt.show()
xt=np.arange(-4,6)
plt.plot(xt,hypothesis(xt,theta),color=‘r’,label="Predicted Value ")
plt.show()
#Y_Predicted_Values=prediction(X,final_theta)
#print(Y_Predicted_Values)
dx = pd.read_csv(‘C:/Users/HP/Desktop/ml/Project 1/Linear_X_Test.csv’)
x=dx.values
#print(x.shape)
x=x.reshape((-1,))
#print(x.shape)
y=prediction(x,final_theta)
print(y)
pd.DataFrame(y).to_csv(“C:/Users/HP/Desktop/ml/Project 1/Linear_Y_Test.csv”)

Hey Ashi,
Could you please copy paste your code on cb.lk/ide and share the link, Otherwise i’m confused about the indentation of your code.
I would wait for your response.
Thanks :slight_smile:


how to check accuracy? cant we submit again?

Hey,
Yes you can submit the predictions many times.
You had made some small mistakes in your code. I’ve fixed the bugs now you can predict for new test values with the following code


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dfx = pd.read_csv('Training Data/Linear_X_Train.csv')
dfy = pd.read_csv('Training Data/Linear_Y_Train.csv')
x = dfx.values
y= dfy.values

x = x.reshape((-1,))
y = y.reshape((-1,))

X = x  # data is already normalised
Y = y

def hypothesis(x,theta):
    return(theta[0]+theta[1]*x)
    
def error(x,y,theta):
    m=x.shape[0]
    error=0
    for i in range(m):
        h=hypothesis(x[i],theta)  # you were not computing hypothesis correctly.
        error += (h-y[i])**2
    return error/m

def gradient(x,y,theta):
    m=x.shape[0]
    grad=np.zeros(2)
    for i in range(m):
        h=hypothesis(x[i],theta)
        grad[0]+=(h-y[i])       # Problem was here
        grad[1]+=(h-y[i])*x[i] # and here
    return grad

def gradient_descent(x,y,k=.001):
    theta=np.array([-1.0, 0.6])
    m=x.shape[0]
    itr=50
    err_list=[]
    for i in range(itr):
        grad=gradient(x,y,theta)
        e=error(x,y,theta)
        err_list.append(e)
        theta[0]=theta[0]-k*grad[0]
        theta[1]=theta[1]-k*grad[1]
    return theta,err_list

final_theta, error_list = gradient_descent(X,Y, k= 0.0001)

I hope this works now,
Thanks :slight_smile:

I am not able to submit. Its showing calculating accuracy for half hour and stuck on it.Please look after this problem.

Then you need to check the csv file you generated… That would have some issues.
please share your csv file or the process using to create that csv file.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.