Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Implement Linear regression using NumPy. You should implement your code in the provided linear _ regression.py file. This python file takes a csv file

Question:
Implement Linear regression using NumPy. You should implement your code in the provided linear_regression.py file. This python file takes a csv file and a list of features as input. From these list of features, the last feature will be the target label and the remaining ones will be your training inputs. For example, in the following usage scenario, linear.csv is the csv file and Writing is the target label, while Math and Reading are training inputs.
$python3 linear_regression.py linear.csv Math Reading Writing
Output the RMSE score of prediction.
-provided linear_regression.py file,is as follow:
import numpy as np
import pandas as pd
import math
import sys
import os
#Todo : define necessary functions
def compute_weights(X, y):
"""
Compute linear regression weights using the normal equation.
X: input features with a column of ones (bias term).
y: target variable.
"""
# Normal equation: (X^T * X)^-1* X^T * y
weights = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
return weights
def predict(X, weights):
"""
Predict the target variable using the linear model.
X: input features with a column of ones (bias term).
weights: weights of the linear model.
"""
predictions = X.dot(weights)
return predictions
def rmse(predictions, targets):
"""
Calculate the root mean squared error.
predictions: predicted values.
targets: actual values.
"""
return np.sqrt(((predictions - targets)**2).mean())
def linear_regression(data):
"""
data: input data matrix
return: rmse value
"""
#Todo : fill code here
# Extract the features and target variable
features = data.iloc[:, :-1]
target = data.iloc[:,-1]
# Add a column of ones to the features (for the intercept term)
features = np.hstack([np.ones((features.shape[0],1)), features])
# Compute weights
weights = compute_weights(features, target)
# Predict the target variable
predictions = predict(features, weights)
# Calculate and return RMSE
return rmse(predictions, target)
# do not modify this function
def load_data():
filename = sys.argv[1]
feature_matrix = pd.read_csv(filename)
feature_matrix = feature_matrix.dropna()
features = sys.argv[2:]
#print(feature_matrix[features])
return feature_matrix
if __name__=="__main__":
data = load_data()
RMSE_SCORE = linear_regression(data)
print("RMSE score is : ", RMSE_SCORE)
-linear.csvcontains in the following format: Math, Reading, Writing. There are 1001 lines of data structured in this way.Math, Reading, Writing. There are 1001 lines of data structured in this way.im attaching the image of csv file
image text in transcribed

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions