<jupyter_start><jupyter_text>gradient descent algo for linear regression<jupyter_code>def hypothesis(theta,x):
    return theta[0]+theta[1]*x

def error(X,Y,theta):
    tot_error = 0
    m = X.shape[0]
    for i in range(m):
        tot_error+=(hypothesis(theta,X[i])-Y[i])**2
    return 0.5*tot_error

def gradient(Y,X,theta):
    grad=np.array([0.0,0.0])
    m=X.shape[0]
    for i in range(m):
        grad[0]+=(hypothesis(theta,X[i])-Y[i])
        grad[1]+=(hypothesis(theta,X[i])-Y[i])*X[i]
    return grad

def batch_gradient(Y,X,theta,batch_size=10):
    grad=np.array([0.0,0.0])
    m=X.shape[0]
    indices = np.arange(m)
    np.random.shuffle(indices)
    indices = indices[:batch_size]
    for i in indices:
        grad[0]+=(hypothesis(theta,X[i])-Y[i])
        grad[1]+=(hypothesis(theta,X[i])-Y[i])*X[i]
    return grad

def gradientDescent(X,Y,learning_rate,max_itr):
    grad=np.array([0.0,0.0], dtype=np.float64)
    
    #theta=np.array([0.0,0.0], dtype=np.float64)
    theta=np.array([-2.0,0.0], dtype=np.float64)
    #above initial theta is taken -2,0 for better 3d plotting of data few cells below
    #but its perfectly fine to take it as 0
    
    error_list = []
    theta_list = []
    for i in range(max_itr):
        grad = gradient(Y,X,theta)
        e = error(X,Y,theta)
        error_list.append(e)
        theta_list.append((theta[0],theta[1]))
        theta[0]=theta[0]-learning_rate*grad[0]
        theta[1]=theta[1]-learning_rate*grad[1]
    return theta,error_list,theta_list

theta,error_list,theta_list = gradientDescent(X,Y,learning_rate=0.001,max_itr=100)
print(theta)
plt.plot(error_list)


plt.scatter(X,Y)
plt.plot(X,hypothesis(theta,X),color="orange")

#testing the model
xtest = np.linspace(-2,6,10)
print(xtest)
plt.scatter(X,Y,label="training data")
plt.plot(xtest,hypothesis(theta,xtest),color="red",label = "predicted")
plt.legend()
plt.show()
from mpl_toolkits.mplot3d import Axes3D<jupyter_output><empty_output><jupyter_text>plotting 3d visualization of loss for theta of own choice<jupyter_code>#plotting 3d loss for different theta values of own choice
t0 = np.arange(-2,3,0.1)
t1 = np.arange(-2,3,0.1)
t0,t1 = np.meshgrid(t0,t1)

J = np.zeros(t0.shape)

m=t0.shape[0]
n=t0.shape[1]

for i in range(m):
    for j in range(n):
        J[i,j] = np.sum((Y - t1[i,j]*X - t0[i,j])**2)
fig = plt.figure()
axes = fig.gca(projection = '3d')
axes.plot_surface(t0,t1,J)
plt.show()<jupyter_output><empty_output><jupyter_text>lets have trajectory for gradient descent<jupyter_code>theta_list = np.array(theta_list)
fig = plt.figure()
axes = fig.gca(projection = '3d')
axes.scatter(theta_list[:,0],theta_list[:,1],error_list,color="black")
axes.plot_surface(t0,t1,J,cmap="rainbow",alpha=0.7)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17316017316017315, 'fraction_numerical': 0.03607503607503607, 'mean_word_length': 4.5239043824701195, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '171132', 'license': 'MIT', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1159, 'n_words': 182, 'path': 'regression/linear_reg.ipynb', 'repo_name': 'nik29213/ml_mini_projects', 'size': '2772'}