Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Help Complete the code for multiple linear regression code. 1. You are required to implement a multiple linear regression from scratch. Problem: For the

Please Help

Complete the code for multiple linear regression code.

1. You are required to implement a multiple linear regression from scratch.

Problem:

For the years 1949 to 1966, the international production, the stock, the domestic consumption and the level of imports were noted in millions of French francs.

The goal is predict the levels of imports from the international production, the stock and the domestic consumption.

import numpy as np import pandas as pd

Read data from a file

df pd.read_csv("x13.txt",sep=r'\s+', header=None, skiprows=42)

df.head() 
0 1 2 3 4 5 6
0 1 1949 1 149.3 4.2 108.1 15.9
1 2 1950 1 161.2 4.1 114.8 16.4
2 3 1951 1 171.5 3.1 123.2 19.0
3 4 1952 1 175.5 3.1 126.9 19.1
4 5 1953 1 180.8 1.1 132.1 18.8

Get X and Y

X = df.iloc[:, 3:6].values

y = df.iloc[:, 6].values
print(X[:5, :])
print(y[:5])
[[149.3 4.2 108.1] [161.2 4.1 114.8] [171.5 3.1 123.2] [175.5 3.1 126.9] [180.8 1.1 132.1]] [15.9 16.4 19. 19.1 18.8] 

Linear Regression

Complete the follwing code by computing b, sse, sst and r.

In [5]:

from numpy.linalg import inv

def multipleLinearRegression(X, y):
 n,m=X.shape
 X1 = np.hstack((np.ones((n,1)), X))
 # Begin your code
 b = None
 sse = 0.0
 sst = 0.0
 r = 0.0
 # End code
 return b, sse, sst, r

In [6]:

print(multipleLinearRegression(X,y))
 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions

Question

Why isnt choosing a legal entity a one-time event?

Answered: 1 week ago