<jupyter_start><jupyter_text>Model-1.1
> Covid-19 Indian Death Predictor using Normal Equation in linear regression with regularization.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
days = np.arange(316)
train_days = np.arange(300)
test_days = np.arange(16)
for i in range(0,316):
  days[i] = i+1
  if(i<300):
    train_days[i] = i+1
  else:
    test_days[i-300] = i+1   
data = pd.read_csv('Covid19_India_Shyam.csv', sep=',',header=None)
shyam = data.values
sample_data = np.arange(316)
for i in range(1,316):
  sample_data[i]= shyam[i][1]
#print(sample_data)
#print(len(days), len(sample_data))
plt.title("Normal equation with regularization: Sample Data plot")
plt.xlabel("Days")
plt.ylabel("No of deaths per day")
plt.scatter(days, sample_data,  color='black')
plt.show()<jupyter_output><empty_output><jupyter_text>New section<jupyter_code>import numpy as np
#for inverse matrix
from numpy.linalg import pinv
import matplotlib.pyplot as plt
x = days
y = sample_data
# form matrix X and Y and coefficent of lembda
X = np.array([np.ones(len(x)),x, np.power(x, 2),np.power(x, 3) ,np.power(x, 4),np.power(x, 5),np.power(x, 6) ]).T
Y = (y[:, np.newaxis])
one = np.identity(X.shape[1])
one[0,0] = 0
lamda = 2
# apply normal equation
theta_final = pinv(X.T.dot(X) + lamda*one).dot(X.T).dot(Y)
print("Showing the theta (or the parameters) for the hypothesis:\n"+ str(theta_final))
print("Ploting the graph:")
y_pred = theta_final.T.dot(X.T)
model_1_output = y_pred
plt.title("Normal Equation with Regularization")
plt.xlabel("Days")
plt.ylabel("No of Deaths per Day")
plt.scatter(x, y,  color='black')
plt.plot(x, y_pred[0,:], color='red', linewidth=5)
plt.show()
# Prediction for january month
# generating fuction of death is y= Hw(X) = [1 + X + X^2 + X^3 + X^4 + X^5 + X^6]*theta_final.T
import numpy as np
jan_days = np.arange(31)
draft = np.arange(31)
res = np.arange(31)
death = np.arange(31)
total_death = 0
for i in range(0,31):
  jan_days[i] = 336+i
draft = np.array([np.ones(len(jan_days)),jan_days, np.power(jan_days, 2),np.power(jan_days, 3) ,np.power(jan_days, 4),np.power(jan_days, 5),np.power(jan_days, 6)]).T
res = np.dot(draft,theta_final)
for i in range(0,30):
  if(res[i]-abs(res[i])>=0.50):
    death[i] = res[i][0]+1
  else:
    death[i] = res[i][0]-1
  total_death = total_death + death[i]
print("The expected number of deaths due to COVID-19 for the month of January, 2021 using Normal equation with regularization : " + str(total_death))

plt.title("Normal equation with regularization: January Month Death Plotting")
plt.xlabel("Days (In January Month)")
plt.ylabel("No of deaths per day (In January Month)")
plt.scatter(jan_days, death,  color='black')
plt.show()<jupyter_output><empty_output><jupyter_text>Model-1.2
Using iterative method with regularization<jupyter_code># Data making
data  = []
import numpy as np
data_e = np.arange(316*2).reshape(316,2)
for i in range(0,316):
    data_e[i][0] = days[i]
    data_e[i][1] = sample_data[i]
data =data_e.tolist()
#data = [(1, 1), (2, 2), (5, 5.5), (6, 8), (9, 10)]
import random
import matplotlib.pyplot as plt
import math
import numpy as np
import functools
from matplotlib.animation import FuncAnimation

"""
Dummy Data for Linear Regression
"""
#data = [(1, 1), (2, 2), (5, 5.5), (6, 8), (9, 10)]    
"""
Plot the line using theta_values
"""
print(type(data))
def plot_line(formula, x_range, order_of_regression, label=None):
    x = np.array(x_range).tolist()  
    y = [formula(update_features(x_i, order_of_regression, 9)) for x_i in x]
    l, = plt.plot(x, y, label=label, color = 'darkorange')
    return l
"""
Hypothesis Function
"""
def h(x, theta):
    return np.matmul(theta.T, x)[0][0]
"""
Update features by order of the regression
"""

def update_features(x, order_of_regression, m = 9):
    features = [2]
    for i in range(order_of_regression):
        features.append(math.pow(x, i+1)/ math.pow(m, i+1))
    return np.atleast_2d(features).T

"""
Simultaneous Update
"""
def update_theta(data, alpha, theta, order_of_regression):
    temp = []
    for i in range(order_of_regression+1):
        temp.append(theta[i] - alpha * j_prime_theta(data, theta, order_of_regression, i))
    theta = np.array(temp)
    return theta
    
def reg_j_prime_theta(data, theta, l, order_of_regression, i):
    result = 0
    m = len(data)
    for x, y in data :
        x = update_features(x, order_of_regression)
        result += (h(x, theta) - y) * x[i]
    result += l*theta[i]
    return (1/m) * result

def reg_j(data, theta, l, order_of_regression):
    cost = 0
    m = len(data)
    for x, y in data:
        x = update_features(x, order_of_regression)
        cost += math.pow(h(x, theta) - y, 2)
    reg = 0
    for j in theta:
        reg += math.pow(j, 2)
    reg = reg * l
    return (1/(2*m)) * (cost + reg)

def reg_update_theta(data, alpha, theta, l, order_of_regression):
    temp = []
    for i in range(order_of_regression+1):
        temp.append(theta[i] - alpha * reg_j_prime_theta(data, theta, l, order_of_regression, i))
    theta = np.array(temp)
    return theta

def reg_gradient_descent(data, alpha, l, tolerance, theta=[], order_of_regression =316):
    if len(theta) == 0:
        theta = np.atleast_2d(np.random.random(order_of_regression+1) * 100).T
    prev_j = 100000
    curr_j = reg_j(data, theta, l, order_of_regression)
    print(curr_j)
    cost_history = []
    theta_history = [] 
    counter = 0
    while(abs(curr_j - prev_j) > tolerance):
        try:
            cost_history.append(curr_j)
            theta_history.append(theta)
            theta = reg_update_theta(data, alpha, theta, l, order_of_regression)
            prev_j = curr_j
            curr_j = reg_j(data, theta, l, order_of_regression)
            if counter % 100 == 0:
                print(curr_j)
            counter += 1
        except:
            break
    print("Stopped with Error at %.5f" % prev_j)
    return theta, cost_history, theta_history
reg_theta, reg_cost_history, reg_theta_history = reg_gradient_descent(data, 0.0001, 0.01 , 0.001, order_of_regression = 1)
#print(reg_theta)
#print(reg_cost_history)
#print(reg_theta_history)
f = functools.partial(h, theta = reg_theta_history[-1])
plt.scatter([i[0] for i in data], [i[1] for i in data])
reg = plot_line(f, (np.array(range(10,91))/10).tolist(), order_of_regression=1, label='regularized')
#f = functools.partial(h, theta=theta_history[-1])
#unreg = plot_line(f, (np.array(range(10,91))/10).tolist(), order_of_regression=5, label='not regularized')
#plt.legend([reg, unreg], ['Regularized', 'Not Regularized'])
plt.title('Regularization with GDA')
plt.show()
print(data)

f = functools.partial(h, theta = reg_theta_history[-1])
plt.scatter([i[0] for i in data], [i[1] for i in data])
reg = plot_line(f, (np.array(range(10,91))/10).tolist(), order_of_regression=50, label='regularized')
#f = functools.partial(h, theta=theta_history[-1])
#unreg = plot_line(f, (np.array(range(10,91))/10).tolist(), order_of_regression=5, label='not regularized')
#plt.legend([reg, unreg], ['Regularized', 'Not Regularized'])
plt.title('Effect of Regularization')
plt.show()
print(data)<jupyter_output><empty_output><jupyter_text>Model-1.3
>Using locally weighted Linear Regression<jupyter_code>def LWR(x, y, cp, tau):
    gamma = np.exp(np.sum((x-cp)**2, axis = 1)/(-2*tau*tau))
    w = (x.T) * gamma
    #print(w)
    Jw = np.linalg.pinv(w@x)@w@y
    #print(Jw)
    return (np.dot(cp, Jw))
def Locally_weighted_Reg (x, y, tau):
  #weight  = []
  y_pred = []
  x = np.c_[np.ones(len(x)), x]
  from sklearn.model_selection import train_test_split
  x_train, x_test, y_train, y_test = train_test_split(x, y, train_size = 0.8, random_state = 3)
  for cp in x_test:
    predict = [LWR (x_train, y_train, cp, tau)]
    #print(predict)
    y_pred.append(predict)
  # Plotting graphs
  plt.title('Locally Weighted Regression')
  plt.xlabel('Days')
  plt.ylabel('Deaths')
  plt.scatter(x[:,1],y)
  plt.scatter(x_test[:,1], y_pred, color = 'darkorange')
  plt.show()
  return (y_pred,y_test)
tau = 0.08
(model_3_output, model_3_actual) = Locally_weighted_Reg (days, sample_data, tau)



"""##prediction
jan_days_list = []
jan_days_list = jan_days.tolist()
Jan_pred = []
for i in jan_days_list:
  pred = [LWR(x, y, i, tau)]
  Jan_pred.append(pred)
plt.scatter(Jandays, Jan_pred) 

# x, y are actual data points... There is no need to use splitted data
"""
################# error calculation #####################
def rmse(predictions, targets):
    differences = predictions - targets                       #the DIFFERENCEs.
    differences_squared = differences ** 2                    #the SQUAREs of ^
    mean_of_differences_squared = differences_squared.mean()  #the MEAN of ^
    rmse_val = np.sqrt(mean_of_differences_squared)           #ROOT of ^
    return rmse_val
rmse(sample_data,model_1_output)
rmse(model_3_output, model_3_actual)
#rmse(model_2_output, model_2_actual)<jupyter_output><empty_output><jupyter_text>draft<jupyter_code>"""def get_WeightMatrix_for_LOWES(query_point, Training_examples, Bandwidth): 
  # M is the No of training examples 
  M = Training_examples.shape[0] 
  # Initialising W with identity matrix 
  W = np.mat(np.eye(M)) 
  # calculating weights for query points 
  for i in range(M): 
    xi = Training_examples[i] 
    denominator = (-2 * Bandwidth * Bandwidth) 
    W[i] = np.exp(np.dot((xi-query_point), (xi-query_point).T)/denominator) 
    return W

# function to make predictions 
def predict(training_examples, Y, query_x, Bandwidth):
  M = training_examples.shape[0]
  all_ones = np.ones((M, 1))
  X_ = np.hstack((training_examples, all_ones))
  qx = np.mat([query_x, 1])
  W = get_WeightMatrix_for_LOWES(qx, X_, Bandwidth) 
  # calculating parameter theta 
  theta = np.linalg.pinv(X_.T*(W * X_))*(X_.T*(W * Y)) 
  # calculating predictions 
  pred = np.dot(qx, theta) 
  return theta, pred

Bandwidth = 0.1
X_test = np.linspace(-2, 2, 20) 
Y_test = [] 
for query in X_test: 
  theta, pred = predict(X, Y, query, Bandwidth) 
  Y_test.append(pred[0][0]) 
horizontal_axis = np.array(X) 
vertical_axis = np.array(Y) 
plt.title("Tau / Bandwidth Param %.2f"% Bandwidth) 
plt.scatter(horizontal_axis, vertical_axis) 
Y_test = np.array(Y_test) 
plt.scatter(X_test, Y_test, color ='red') 
plt.show() 
"""<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup