Write a function linear regression that fits a linear regression model, and takes the following two...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a function linear regression that fits a linear regression model, and takes the following two arguments as input: 1. x: A numpy array of the shape (N,d) where N is the number of data points, and d is the data dimension. Do not assume anything about N or d other than being a positive integer. 2. Y: A numpy array of the shape (N,) where N is the number of data points. 3. 1am: The regularization coefficient À, which is a scalar positive value. See the objective function below. and returns the linear regression weight vector which is a numpy array with a shape of (d+1,1). Your function should: 1. Have an Intercept Weight: In other words, your fitting model should be minimizing the following mean-squared loss N VE Σ( - (B₁ + B₂x) + B₂x + + Bax)² + AB¹ B. N L(B; X, Y, A)² = i=1 An easy way to do this is by concatenating a constant 1-column to the data matrix (think about the right numpy function and the proper call given the defined loss and weight vector format). Hint: The textbook has provided you with the solution for the least squares optimization with ridge regression which could be helpful. 2. Never Raise An Error, and Return the Solution with the Smallest Euclidean Norm in case the optimal weight vector is not unique. For instance, when the number of data points is smaller than the dimension, many optimal weight vectors exist. Po Hint: Reviewing your linear algebra may be helpful in this case. You may want to use the Moore-Penrose matrix inversion. Note: The regularization coefficient will not be used for the first two problems. However, it would be used later, and we expect you to implement it correctly here. In : def linear regression (X, Y, lam=0): assert X.ndim==2 N = X.shape [0] d = X.shape [1] assert Y.size -- N Y_col Y.reshape (-1,1) #your code here assert beta.shape == (d+1, 1) return beta Write a function linear regression that fits a linear regression model, and takes the following two arguments as input: 1. x: A numpy array of the shape (N,d) where N is the number of data points, and d is the data dimension. Do not assume anything about N or d other than being a positive integer. 2. Y: A numpy array of the shape (N,) where N is the number of data points. 3. 1am: The regularization coefficient À, which is a scalar positive value. See the objective function below. and returns the linear regression weight vector which is a numpy array with a shape of (d+1,1). Your function should: 1. Have an Intercept Weight: In other words, your fitting model should be minimizing the following mean-squared loss N VE Σ( - (B₁ + B₂x) + B₂x + + Bax)² + AB¹ B. N L(B; X, Y, A)² = i=1 An easy way to do this is by concatenating a constant 1-column to the data matrix (think about the right numpy function and the proper call given the defined loss and weight vector format). Hint: The textbook has provided you with the solution for the least squares optimization with ridge regression which could be helpful. 2. Never Raise An Error, and Return the Solution with the Smallest Euclidean Norm in case the optimal weight vector is not unique. For instance, when the number of data points is smaller than the dimension, many optimal weight vectors exist. Po Hint: Reviewing your linear algebra may be helpful in this case. You may want to use the Moore-Penrose matrix inversion. Note: The regularization coefficient will not be used for the first two problems. However, it would be used later, and we expect you to implement it correctly here. In : def linear regression (X, Y, lam=0): assert X.ndim==2 N = X.shape [0] d = X.shape [1] assert Y.size -- N Y_col Y.reshape (-1,1) #your code here assert beta.shape == (d+1, 1) return beta
Expert Answer:
Answer rating: 100% (QA)
Answer Heres the implementation of the linearregression function import numpy as np def linearregres... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
A manufacturing company that produces one single type of a product has a total fixed cost of 4.000.000 TL, a contribution margin rate of 80%, and a unit selling price of 100 TL. The current operating...
-
Explain what equity means as it relates to taxation. ( 2 ) Discuss the two general equity standards of benefits received and ability to pay. Explain what each of these terms mean and how each...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
In Exercises, analyze and sketch a graph of the function. Label any intercepts, relative extrema, points of inflection, and asymptotes. Use a graphing utility to verify your results. f(x)=x16 - x
-
A light beam travels at 1.94 x 108 m/s in quartz. The wavelength of the light in quartz is 355 nm. (a) What is the index of refraction of quartz at this wavelength? (b) If this same light travels...
-
On January I. Newtune Company exchanges 15.000 shares of its common stock for all of the outstanding shares of On-the-Go. Inc. Each of NewTune's shares has a $4 par value and a $50 fair value. The...
-
Genetic Diversity and Distance from Africa It is hypothesized that humans originated in East Africa, and migrated from there. We compute a measure of genetic diversity for different populations, and...
-
WallsFarther Bank has the following balance sheet (in millions of dollars): Cash inflows over the next 30 days from the FI's performing assets are $ 5.5 million. Calculate the LCR for WallsFarther...
-
Oscar, Ltd. is a British subsidiary of an American company. Oscar's functional currency is the pound sterling (). The following exchange rates were in effect during 2021: January 1 1 = ps 1.58 June...
-
Ivanhoe Ltd. purchased a new machine on April 4, 2017, at a cost of $188,000. The company estimated that the machine would have a residual value of $18,000. The machine is expected to be used for...
-
Question 6 A vertical single cylinder engine has a cylinder diameter of 250mm and a stroke of 450mm. The reciprocating parts have a mass of 180kg. The connecting rod is 4 times as long as the crank...
-
Straight - Line DepreciationEquipment acquired at the beginning of the year at a cost of $ 1 2 5 , 0 0 0 has an estimated residual value of $ 5 , 0 0 0 and an estimated useful life of 1 0 years....
-
Assess the Federal net operating loss rules. Scenario: The Smith Company, a C Corporation, in 2 0 2 4 has a loss of $ 2 0 0 , 0 0 0 . During 2 0 2 5 , 2 0 2 6 , 2 0 2 7 , and 2 0 2 8 , the company...
-
On December 3 1 , 2 0 2 3 , Corotel Company s year - end, the unadjusted trial balance included the following items: Account Debit Credit Accounts receivable $ 2 , 1 0 0 , 0 0 0 Allowance for...
-
On July 16, Year 1, Southeastern University hired an associate professor for its Math Department at an annual (12- month) salary of $105,000. The salary is effective for its new school year, which...
-
Monte Hacho Industries is a Buffalo, New York-based manufacturer and distributor of building products for residential, industrial, infrastructure, renewable energy, and conservation markets. In a...
-
A pt. has an onset of dizziness. The pt.s heart rate is 180, BP is 110/70, resp. rate is 18, 02 sat is 98%. This is a reg narrow complex tach rythm. What is the next intervention?
-
In each of the following independent cases, document the system using whatever technique(s) your instructor specifies. a. Dreambox Creations (www.dreamboxcreations.com/) in Diamond Bar, California,...
-
The boiling temperature of water TB at various altitudes his given in the following table. Determine a linear equation in the form TB = mh + b that best fits the data. Use the equation for...
-
Write a program that generates a vector with 20 random elements between -10 and 10 and then finds the sum of the positive elements.
-
Use a MATLAB built-in function to numerically solve: dx/dy = 2x + 1/y + 2 for 0 < x < 8 with y(0) = 2
-
Tesco plc is one of the worlds largest food retailers. Fiscal year 2014 (the year ended February 28, 2015) was a rocky year for the retailer. The companys sales and margins had come under pressure as...
-
Exhibit P2 displays a selected set of financial ratios for the years 20152017 of three fashion retailers: USbased Gap, UK-based Next, and Japan-based Uniqlo (Fast Retailing). Using this set of...
-
In 2009, following the worldwide credit crisis, several US-based car manufacturers, such as Chrysler and General Motors, approached bankruptcy and needed to be bailed out by the US government and...
Study smarter with the SolutionInn App