Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Import Statements Run the cell below to import the NumPy, Matplotlib, and math packages. [ ] import numpy as np import matplotilib.pyplot as plt import
Import Statements Run the cell below to import the NumPy, Matplotlib, and math packages. [ ] import numpy as np import matplotilib.pyplot as plt import math Four arrays have been created to store the datasets that you will be working with in this lab. Running the cell below will import those arrays into your workspace. We will describe the imported arrays later in this notebook. The cell will also import some functions that will be used to test your code in Problem 7. Run that cell now. fron MATH_599.1ab_08 import x1,y1,x2,y2 fron MATH_599.1ab_68 import unit_test_1, unit_test_2, unit_test_3 Problem 7 - Logistic Regression: Training the Model In this problem, you will train a logistic regression model on X2 and y2. Part 7.A Use the cell below to define a function train_logreg(). The function should accept four parameters named x,y, alpha, and n. Descriptions of the parameters are as follows: - x is expected to be a 2D feature array. - y is expected to be a 10 label array. - alpha is expected to be a learning rate. - steps is the number of iterations of gradient descent to perform during training. The function should apply gradient descent to determine optimal parameters for a logistic regression model. An outline of the steps that the function should perform is provided below. 1. The function add_ones() should be used to create an "extended' feature matrix XE . 2. A coeficient array named betas should be created with all values initialized to zero. The following code can be used for this task: betas = np.zeros (XE.shape[1]) 3. A loop should be executed for a number of iterations indicated by step. Each iteration of the loop should perform the tasks below. a. Use predict_proba() to generate probability estimates. b. Use calculate_NLL() to calculate the NLL score for the current iteration of the model. Store this value in a variable named NLL. c. Calculate the gradient. The lesson titled "Training a Logistic Regression Model' explains how to do this. d. Use the gradient descent update rule to update the parameter estimates. e. Print an update message every 10 iterations. (Code for this has been provided). 4. When the loop is finished, return betas. [ ] def train_logreg( x,y, alpha, steps): If Add code here for 1 in range(steps): " Add code here If (1+1)10==0 : print( (f'Iteration {1+1:003} betas = (betas.round (3)}, NLL ={NLL:.2+} ) return betas Apply your train_logreg() function to the training data stored in X2 and y2. Use steps=3ee and experiment with the learning rate. Your final NLL score should be 74.27 . Store the optimal parameters returned by the function in a variable. [ ] Start coding or generate with AI. Problem 8 - Logistic Regression: Applying the Model The code cell below contains an array named X_new. This array represents a collection of new observations that the model should be applied to. Use the predict_proba() function to apply your optimal model to these observations. Print the resulting probability estimates rounded to 4 decimal places. [ ] X_new = np.array ([ [4.1,1.9,6.4,1.2], [1.6, 8.8,3.6,8.8], [1.5,1.7,1.9,6.2], ]) If Add code here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started