HiThis is a new assignment. I already finished all the part on Q3a and Q3b, but just one minor error need to be fixed. And I also need full help on Q3c. Could you make it cheaper since I only need full help on one question.Thanks!
hw9 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 1/21 In [1]: # Initialize Otter import otter grader = otter.Notebook("hw9.ipynb") In [2]: # Imports import pandas as pd import numpy as np import matplotlib.pyplot as plt import csv import re import seaborn as sns import sklearn 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 2/21 import sklearn.datasets import sklearn.linear_model np.random.seed(42) plt.style.use('fivethirtyeight') sns.set() sns.set_context("talk") # Set some parameters plt.rcParams['figure.figsize'] = (12, 9) plt.rcParams['font.size'] = 16 np.set_printoptions(4) # We will use plot_3d helper function to help us visualize gradients from hw9_utils import plot_3d %matplotlib inline In [3]: # Run this cell to load our sample data part_1_data = pd.read_csv("https://github.com/DS-100/su20/raw/gh-pages/resources part_1_data.head() Out[3]: In [4]: def scatter(x, y): """ Generate a scatter plot using x and y Keyword arguments: x -- the vector of values x y -- the vector of values y """ plt.figure(figsize=(8, 6)) plt.scatter(x, y, marker='.') plt.xlabel('x') plt.ylabel('y') 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 3/21 linear_model x = part_1_data['x'] y = part_1_data['y'] scatter(x,y) ∗ ∗ ∗ ∗ In [5]: def linear_model(x, theta): """ Returns the estimate of y given x and theta Keyword arguments: x -- the vector of values x theta -- the scalar theta """ return theta * x 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 4/21 In [6]: def average_squared_loss(y, y_hat): """ Returns the averge squared loss for observations y and predictions y_hat. Keyword arguments: y -- the vector of true values y y_hat -- the vector of predicted values y_hat """ return np.mean((y - y_hat) ** 2) In [7]: def visualize(x, y, thetas): """ Plots the average l2 loss for given x, y as a function of theta. Use the functions you wrote for linear_model and l2_loss. Keyword arguments: x -- the vector of values x y -- the vector of values y thetas -- an array containing different estimates of the scalar theta """ avg_loss = np.array([average_squared_loss(linear_model(x, theta), y) for the plt.figure(figsize=(8,6)) plt.plot(thetas, avg_loss) plt.xlabel("Theta") plt.ylabel("Average Loss") thetas = np.linspace(-1, 5, 70) visualize(x, y, thetas) 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 5/21 find_theta In [8]: def find_theta(x, y): """ Find optimal theta given x and y Keyword arguments: x -- the vector of values x y -- the vector of values y """ sums = np.sum(x*y) 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 6/21 theta_hat = 1.550264808596222 q1 visualize squared = np.sum(x**2) return sums / squared theta_hat_simple = find_theta(x, y) print(f'theta_hat = {theta_hat_simple}') In [9]: grader.check("q1") Out[9]: In [10]: theta_opt = find_theta(x, y) visualize(x, y, thetas) plt.axvline(x=theta_opt, color='r'); In [11]: theta_opt_2 = find_theta(x, y) scatter(x, y) line_values = linear_model(x, theta_opt_2) plt.plot(x, line_values, color='r'); 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 7/21 In [12]: def visualize_residual(x, y): """ Plot a scatter plot of the residuals, the remaining values after removing the linear model from our data. Keyword arguments: x -- the vector of values x y -- the vector of values y """ ... theta_hat = find_theta(x, y) y_sin = y - linear_model(x, theta_hat) plt.scatter(x, y_sin) plt.xlabel('x') plt.ylabel('residual (true y - estimated y)') plt.title('Residual vs x for Linear Model') plt.axhline(y=0, color='r') visualize_residual(x, y) 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 8/21 sin_model In [17]: def sin_model(x, theta): """ Predict the estimate of y given x, theta_1, theta_2 Keyword arguments: x -- the vector of values x theta -- a vector of length 2, where theta[0] = theta_1 and theta[1] = theta """ theta_1 = theta[0] theta_2 = theta[1] return theta_1 * x + np.sin(theta_2 * x) 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 9/21 sin_MSE sin_MSE_dt1 sin_MSE_dt2 theta sin_MSE_gradient dt1 dt2 dt part_1_data np.mean np.sum sin_model In [32]: def sin_MSE(theta, x, y): """ Compute the numerical value of the l2 loss of our sinusoidal model given the Keyword arguments: theta -- the vector of values theta x -- the vector of x values y -- the vector of y values """ x = part_1_data['x'] y = part_1_data['y'] return np.mean((y - (sin_model(x, theta)))**2) def sin_MSE_dt1(theta, x, y): """ Compute the numerical value of the partial of l2 loss with respect to theta_ Keyword arguments: theta -- the vector of values theta x -- the vector of x values y -- the vector of y values """ x = part_1_data['x'] y = part_1_data['y'] return -2 * np.mean((y - (sin_model(x, theta))) * x) def sin_MSE_dt2(theta, x, y): """ 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 10/21 q2 gradient_descent(df, initial_guess, alpha, n) df initial_guess Compute the numerical value of the partial of l2 loss with respect to theta_ Keyword arguments: theta -- the vector of values theta x -- the vector of x values y -- the vector of y values """ x = part_1_data['x'] y = part_1_data['y'] return -2 * np.mean((y - (sin_model(x, theta))) * (x * np.cos(theta[1] * x)) # This function calls dt1 and dt2 and returns the gradient dt. It is already imp def sin_MSE_gradient(theta, x, y): """ Returns the gradient of l2 loss with respect to vector theta Keyword arguments: theta -- the vector of values theta x -- the vector of x values y -- the vector of y values """ return np.array([sin_MSE_dt1(theta, x, y), sin_MSE_dt2(theta, x, y)]) In [33]: grader.check("q2") Out[33]: 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 11/21 grad_desc theta data alpha alpha theta theta theta_history theta loss_history theta num_iter sin_MSE sin_MSE_gradient In [29]: def init_theta(): """Creates an initial theta [0, 0] of shape (2,) as a starting point for gra return np.zeros((2,)) def grad_desc(loss_f, gradient_loss_f, theta, data, num_iter=20, alpha=0.1): """ Run gradient descent update for a finite number of iterations and static lea Keyword arguments: loss_f -- the loss function to be minimized (used for computing loss_history gradient_loss_f -- the gradient of the loss function to be minimized theta -- the vector of values theta to use at first iteration data -- the data used in the model num_iter -- the max number of iterations 2021/7/28 hw9 https://data100.datahub.berkeley.edu/user/luzgool/nbconvert/html/su21/hw/hw9/hw9.ipynb?download=false 12/21 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_42/1607380412.py in
33 34 theta_start = init_theta() ---> 35 theta_hat, thetas_used, losses_calculated = grad_desc(sin_MSE, sin_MSE_g radient, theta_start, part_1_data, num_iter=20, alpha=0.1) 36 for b, l in zip(thetas_used, losses_calculated): 37 print(f"theta: {b}, Loss: {l}") /tmp/ipykernel_42/1607380412.py in grad_desc(loss_f, gradient_loss_f, theta, dat a, num_iter, alpha) 24 for i in range(num_iter): 25 theta_history.append(theta) ---> 26 loss = loss_f(theta)