```python
import numpy as np
import pandas as pd
def cost(theta):

  l_theta=theta**2
  return l_theta
```


```python
def cost_diff(theta_curr):

  return 2*theta_curr
```


```python
def step(x_new,x_old,learning_rate):

  while abs(x_new-x_old)>0.00001:
    x_old=x_new
    d_x=-cost_diff(x_old)
    x_new=x_old+learning_rate*d_x
  
  
  print(x_new)
  
  

```


```python
step(0,1,1)

```

    0



```python
def cost2_diff(theta_curr):
  return 2*theta_curr
```


```python
theta0_old=1;
theta1_old=1;
learning_rate=0.01

def step2(theta0_new,theta0_old,theta1_new,theta1_old,learning_rate):
  
  while abs(theta0_new-theta0_old)>0.00001 and abs(theta1_new-theta1_old)>0.00001:
    theta0_old=theta0_new
    d_1=-cost2_diff(theta0_old)
    theta1_new=theta0_old+learning_rate*d_1
  
    theta1_old=theta1_new
    d_2=-cost2_diff(theta1_old)
    theta1_new=theta1_old+learning_rate*d_2
 


  print(theta0_new)
  print(theta1_new)
  
   

```


```python
step2(0,1,0,1,0.01)
```

    0
    0.0



```python
import numpy as np

t0=1
alpha=0.1
while 1:
  t1=t0-2*(t0-1)*alpha
  if(t1-t0)**2<0.00001:
    break
  t0=t1
  
print(t1)
```

    1.0



```python
import numpy as np

t00 = 1
t10 = 1
alpha = 0.1
while 1:
  t01 = t00 - 4*alpha*(t00-1)
  t11 = t10 - 2*alpha*(t10-1)
  
  if (t01-t00)**2 + (t11 - t10)**2 < 0.01:
    break
  t00 = t01
  t11 = t10
  
print(t01, t10)
```

    1.0 1



```python
def  cal_cost(theta,X,y):
   
    
    m = len(y)
    
    predictions = X.dot(theta)
    cost = (1/2*m) * np.sum(np.square(predictions-y))
    return cost
```


```python
def gradient_descent(X,Y,theta,learning_rate=0.01,iterations=100):
   
    m = len(Y)
    Y = Y.to_numpy().reshape(m,1)
    #cost_history = np.zeros(iterations)
    #theta_history = np.zeros((iterations,2))
    for it in range(iterations):
        
        prediction = np.dot(X,theta)
        
        theta = theta -(1/m)*learning_rate*( X.T.dot((prediction - Y)))
        
        #cost_history[it]  = cal_cost(theta,X,y)
        
    return theta
```


```python
df = pd.read_csv("data.csv") 
X = df.x
Y = df.y

X = (X-np.mean(X))/np.std(X)
lr =0.01
n_iter = 1000



theta = np.random.randn(2,1)

X_b = np.c_[np.ones((len(X),1)),X]
theta = gradient_descent(X_b,Y,theta,lr,n_iter)

print(theta)
```

    [[23.71706365]
     [-6.86672194]]



```python
#question 2

def stochastic_gradient_descent(X, Y, theta, lrate=0.01, n_iter=100):
  m = len(Y)
  cost_history = np.zeros(n_iter)
  Y = Y.to_numpy().reshape(m,1)
  #print(Y.shape)
  #Y = Y.reshape(m,1)
  for it in range(n_iter):
    cost = 0.0
    for i in range(m):
      rand_ind = np.random.randint(0,m)
      X_i = X[rand_ind, :].reshape(1, X.shape[1])
      
      Y_i = Y[rand_ind, :].reshape(1,1)
      
      prediction = np.dot(X_i, theta)
      theta = theta - lrate*(X_i.T.dot(prediction - Y_i))
      cost += cal_cost(theta, X_i, Y_i)
    cost_history[it] = cost
  return theta, cost_history
```


```python
df = pd.read_csv("data.csv") 
X = df.x
Y = df.y

X = (X-np.mean(X))/np.std(X)

n_iterations = 1000;
lrate = 0.01

theta = np.random.rand(2,1)*100
#print(theta)
X_b = np.c_[np.ones((len(X),1)), X]
theta, cost_history = stochastic_gradient_descent(X_b, Y, theta, lrate, n_iterations)

print(theta, cost_history[n_iterations-1])
```

    [[23.74522185]
     [-6.79031234]] 840.8423792848323



```python
def J(X, Y, theta):
  cost = np.sum(np.square(Y-(X.dot(theta))))
  return cost
```


```python
def J1(theta):
  return theta*theta
```


```python
def delta_J(X, Y, theta):
    prediction = np.dot(X,theta)
    return X.T.dot((prediction-Y))
```


```python
def delta_J1(theta):
  return 2*theta

```


```python
#wrong answer check
alpha0=0.01
alpha_prev=0.01
precision=0.000001
theta_0=0
df =lambda x:2*x
prev=theta_0
count =0
while count<100000:
  
  alpha_curr=alpha_prev-df(alpha_prev)/2
  curr=prev-alpha_curr*df(prev)
  count=count+1
  alpha_prev=alpha_curr
  prev=curr
  
print(prev)
```

    0.0



```python
#question 3
import numpy as np
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd 
from sympy import *


def steepest_descent(X, Y, theta, lrate=0.01, n_iter=1000):
  cost_history = np.zeros(n_iter)
  alpha_history = np.zeros(n_iter)
  m = len(Y)
  Y = Y.to_numpy().reshape(m,1)
  #lrate_history = np.zeros(n_iter)
  
  alpha = Symbol('alpha')
  phi = J(X, Y, theta - alpha*delta_J(X,Y,theta))
  
  old_a = np.random.randn()
  #print(phi)
  for it in range(n_iter):
    diff_phi = phi.diff(alpha)
    new_a = old_a - (diff_phi.subs(alpha, old_a)/diff_phi.diff(alpha).subs(alpha, old_a))
    prediction = np.dot(X, theta)
    theta = theta - new_a*(X.T.dot(prediction - Y))
    cost_history[it] = cal_cost(theta, X, Y)
    alpha_history[it] = old_a
    old_a = new_a
  
  return theta, cost_history, alpha_history
```


```python

```


```python
df = pd.read_csv("data.csv")
X = df.x
Y = df.y

X = (X-np.mean(X))/np.std(X)

n_iterations = 1000;
lrate = 0.01

theta = np.random.rand(2,1)*100
#print(theta)
X_b = np.c_[np.ones((len(X),1)), X]
theta, cost_history, alpha_h = steepest_descent(X_b, Y, theta, lrate, n_iterations)

print(theta, cost_history[n_iterations-1], alpha_h[n_iterations-1])
```

    [[23.7180851063830]
     [-6.86699544725538]] 73914.56724393774 0.010638297872340422



```python

```


```python




```


```python
def L(theta):
  return theta**2
alpha = Symbol('alpha')
theta = Symbol('theta')
#L = theta**2
delta_L = L(theta).diff(theta)
n_iter = 1000

t0 = np.random.randn()*100
old_a = np.random.randn()

phi = L(t0-alpha*delta_L.subs(theta, t0))

for it in range(n_iter):
  diff_phi = phi.diff(alpha)
  new_a = old_a - (diff_phi.subs(alpha, old_a)/diff_phi.diff(alpha).subs(alpha, old_a))
  t0 = t0 - new_a*delta_L.subs(theta, t0)
  old_a = new_a
 

print(t0, new_a)
```

    0 0.500000000000000



```python
def L1(theta1, theta2):
  return theta1**2 + theta2**2
alpha = Symbol('alpha')
theta1 = Symbol('theta1')
theta2 = Symbol('theta2')
#L = theta**2
delta_L1 = L1(theta1, theta2).diff(theta1)
delta_L2 = L1(theta1, theta2).diff(theta2)
n_iter = 1000

t0 = np.random.randn()*100
t1 = np.random.randn()*100
old_a = np.random.randn()

phi_0 = L1(t0 - alpha*delta_L1.subs(theta1, t0), t1)
phi_1 = L1(t0, t1 - alpha*delta_L2.subs(theta2, t1))

for it in range(n_iter):
  diff_phi = phi_0.diff(alpha)
  new_a = old_a - (diff_phi.subs(alpha, old_a)/diff_phi.diff(alpha).subs(alpha, old_a))
  t0 = t0 - new_a*delta_L1.subs(theta1, t0)
  t1 = t1 - new_a*delta_L2.subs(theta2, t1)
  old_a = new_a
 
print(t0, t1, new_a)
```

    0 0 0.500000000000000


e6ad29f22ae0f887d3784bc37049aac2f2421caf
ipynb
Jupyter Notebook
IE/406_machine_learning/m_v_joshi/2019/lab_sessions/solutions/mylab2.ipynb
u-square2/AcadVault
571612c928a1357c849cf9a06b47bab15dc5afa3
MIT
2021-11-08T03:54:46.000Z
2021-11-08T04:31:52.000Z
IE/406_machine_learning/m_v_joshi/2019/lab_sessions/solutions/mylab2.ipynb
u-square2/AcadVault
571612c928a1357c849cf9a06b47bab15dc5afa3
MIT
2020-02-08T20:16:17.000Z
2020-02-08T20:24:31.000Z
IE/406_machine_learning/m_v_joshi/2019/lab_sessions/solutions/mylab2.ipynb
u-square2/AcadVault
571612c928a1357c849cf9a06b47bab15dc5afa3
MIT
2022-01-28T05:28:33.000Z
2022-01-28T05:28:54.000Z
Qwen/Qwen-72B
1. YES
2. YES