Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, below is my code for a MULTIVARIATE LINEAR REGRESSION model. I tried to develop the model from scratch using gradient descent to solve the

Hello, below is my code for a MULTIVARIATE LINEAR REGRESSION model. I tried to develop the model from scratch using gradient descent to solve the parameters. I also included the graph that is plotted when the code runs. I would like to also include a polynomial regression with the graph. If anyone can look over my code to verify that is the case and to show me how include a polynomial regression, I would appreciate it. Thank You.

import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_diabetes

# Load the diabetes dataset

diabetes = load_diabetes()

data = diabetes['data']

target = diabetes['target']

# Shuffle the dataset

np.random.seed(0)

indices = np.random.permutation(data.shape[0])

data, target = data[indices], target[indices]

# Split the data into train, dev, and test sets

train_data = data[:int(0.7 * data.shape[0])]

train_target = target[:int(0.7 * target.shape[0])]

dev_data = data[int(0.7 * data.shape[0]):int(0.85 * data.shape[0])]

dev_target = target[int(0.7 * target.shape[0]):int(0.85 * target.shape[0])]

test_data = data[int(0.85 * data.shape[0]):]

test_target = target[int(0.85 * target.shape[0]):]

# Add a column of ones for the bias term

train_data = np.hstack([np.ones((train_data.shape[0], 1)), train_data])

dev_data = np.hstack([np.ones((dev_data.shape[0], 1)), dev_data])

test_data = np.hstack([np.ones((test_data.shape[0], 1)), test_data])

# Define the gradient descent function

def gradient_descent(data, target, learning_rate, iterations):

theta = np.zeros(data.shape[1])

m = target.size

for iteration in range(iterations):

prediction = np.dot(data, theta)

error = prediction - target

gradient = np.dot(data.T, error) / m

theta -= learning_rate * gradient

return theta

# Train the model using gradient descent

learning_rate = 0.001

iterations = 100000

theta = gradient_descent(train_data, train_target, learning_rate, iterations)

# Make predictions on the test set

test_predictions = np.dot(test_data, theta)

# Plot the points from the model

plt.scatter(test_target, test_predictions)

plt.xlabel("True Values")

plt.ylabel("Predictions")

plt.show()

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

Is it clear what happens if an employee violates the policy?

Answered: 1 week ago