0% found this document useful (0 votes)
63 views

Optimization in Machine Learning

This document contains the code for a machine learning assignment involving linear regression, polynomial regression, and model selection. It includes code to load and preprocess data, fit linear and polynomial regression models, make predictions, and calculate error. It finds that a 5th degree polynomial model has the lowest error according to MSE.

Uploaded by

let rock
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Optimization in Machine Learning

This document contains the code for a machine learning assignment involving linear regression, polynomial regression, and model selection. It includes code to load and preprocess data, fit linear and polynomial regression models, make predictions, and calculate error. It finds that a 5th degree polynomial model has the lowest error according to MSE.

Uploaded by

let rock
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab Assignment 3

Optimization for Machine Learning

Submitted By:
Ramanpreet
B20CS052

IMPORTS

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error as mse

Q-1

df1 = pd.read_excel('/content/2_col.xlsx')
B = df1.values

x, y = B[:,0] , B[:,1]
A=np.column_stack((x,np.ones((len(x),1),dtype=float)))
beta=np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,y.T))
print(beta)
plt.scatter(x, y)
plt.plot(x, beta[0]*x + beta[1], 'r')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Prediction:

print(beta[0]*2 + beta[1])
-1390.6445915186453
Q-2
df2 = pd.read_excel('/content/2_col.xlsx')
B2 = df2.values
x2, y2 = B2[:,0] , B2[:,1]

def n_fitting_polynomial(x,y,n):
  A = np.array(x**n)
  for i in range(n-1,0,-1):
    A = np.column_stack((A,x**i))
  A = np.column_stack((A,np.ones((len(x),1),dtype=float)))
  beta = np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,y.T))
  print(beta)
  res = 0
  for i in range(n+1):
    res += beta[i]*x**(n-i)
  plt.scatter(x,y)
  plt.plot(x, res, 'r')
  plt.xlabel('x')
  plt.ylabel('y')
  plt.show()

  return beta

def calc(beta,degree,val):
  res = [beta[i]*val**(degree-i) for i in range(degree+1)]
  return sum(res)

2-degree polynomial

beta2 = n_fitting_polynomial(x2,y2,2)
[ 31310.91593703 -384643.61065031 1181749.13983807]

price2 = calc(beta2,2,2)
price2
537705.582285569
3-degree polynomial

beta3 = n_fitting_polynomial(x2,y2,3)
[ -135585.375 2532648. -15766344. 32710704. ]

price3 = calc(beta3,3,2)
price3
10223925.0

4-degree polynomial

beta4 = n_fitting_polynomial(x2,y2,4)
[-3.65797116e+04 4.54842164e+05 -5.99098655e+04 -1.68368416e+07
5.23416014e+07]

price4 = calc(beta4,4,2)
price4
21481740.635879323

5-degree polynomial

beta5 = n_fitting_polynomial(x2,y2,5)
[-4.34027734e+03 5.50332188e+04 -7.43710000e+04 -9.89990500e+05
4.56700000e+05 1.13929040e+07]

price5 = calc(beta5,5,2)
price5
8493016.625

6-degree polynomial

beta6 = n_fitting_polynomial(x2,y2,6)
[-3.00625687e+02 1.54627930e+03 1.99325000e+04 -5.48053750e+04 -
6.27080000e+05 1.96088000e+05 9.40822400e+06]

price6 = calc(beta6,6,2)
price6
7202797.8935546875

7-degree polynomial

beta7 = n_fitting_polynomial(x2,y2,7)
[ 6.63420410e+01 -9.42580078e+02 -2.32010156e+03 6.35066250e+04
4.00968750e+03 -1.08943800e+06 -4.91849600e+06 2.90088320e+07]
Q-3

dp = pd.read_csv('/content/6 columns - Copy.csv')


B = dp.values

x, y, z = B[:,1] ,B[:,2], B[:,0]


A=np.column_stack((x,y,np.ones((len(x),1),dtype=float)))
beta=np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,z.T))
print(beta)

[ 4.33930426 23.3429297 182.53262995]

Prediction:
print(beta[0]*2 + beta[1]*5 + beta[2])
307.9258869834045

Q-4

x, y, z = B[:,1] ,B[:,2], B[:,0]


A=np.column_stack((x**2,x*y,y**2,x,y,np.ones((len(x),1),dtype=float)))
beta=np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,z.T))
print(beta)

[ 1.56265814e-02 3.48832087e+00 3.14829103e+01 -7.43950856e+00 -


3.30505362e+02 9.70605241e+02]

Prediction:

print(beta[0]*2**2 + beta[1]*5*2 + beta[2]*5**2 + beta[3]*2 + beta[4]*5 + beta[5])


125.21788836917483
Q-5

ai = B[:,1:4]
z = B[:,0]
A=np.column_stack((ai,np.ones((ai.shape[0],1),dtype=float)))
beta=np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,z.T))
print(beta)

[ 4.34125631 27.28640279 -17.50093919 194.0354734 ]

Prediction:

print(beta[0]*2 + beta[1]*5 + beta[2]*1 + beta[3])


321.6490608125281

Q-6

x, y, z = B[:,4] ,B[:,5], B[:,0]


A=np.column_stack((x,y,np.ones((len(x),1),dtype=float)))
beta=np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,z.T))
print(beta)

[ 21.38161439 42.04713217 423.59251181]

Q-7

db = pd.read_csv('/content/Census data (Chandigarh).csv')


B = db.values

x, y = B[:,0], B[:,1]
y_log = np.log(y)
A = np.column_stack((x, np.ones(len(y))))
beta = np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,y_log.T))
[ 4.52269555e-02, -7.70006445e+01]
y_pred = np.exp(np.dot(A,beta))
plt.plot(x, y, '.', label = 'original')
plt.plot(x, y_pred, 'r', label = 'predicted')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
## prediction for year 2021

y = np.exp(beta[1] + beta[0]*2021)
1799523.7169414083

Q-8

def MSE(true,pred):
  return mse(true,pred)

def polynomial_fit(x, y, p):


  A = np.array([x**(p-i) for i in range(p+1)]).T
  beta = np.dot(np.linalg.inv(np.dot(A.T,A)),np.dot(A.T,y.T))
  y_pred = np.dot(A,beta)
  loss = MSE(y, y_pred)
  return beta, y_pred, loss

data = pd.read_excel('2_col.xlsx')
B = data.values
x, y = B[:,0], B[:,1]

loss_val,beta_val = [], []
fig, axs = plt.subplots(5, 4, figsize = (24,24))
for i in range(1,21):
  beta, y_pred, loss  = polynomial_fit(x,y,i)
  axs[(i-1)//4][(i-1)%4].scatter(x,y)
  axs[(i-1)//4][(i-1)%4].plot(x,y_pred, 'r')
  axs[(i-1)//4][(i-1)%4].set_title(f'Degree: {i}')
  loss_val.append(loss)
  beta_val.append(beta)
mse_loss = float('inf')
degree = 0
for i in range(len(loss_val)):
    if loss_val[i] < mse_loss:
        mse_loss = loss_val[i]
        degree = i+1
print('Degree of polynomial with lowest MSE:',degree)
Degree of polynomial with lowest MSE: 5

You might also like