0% found this document useful (0 votes)
12 views2 pages

Linear Regression on Boston Dataset

This document loads the Boston housing dataset and splits it into training and test sets. It fits a linear regression model to predict housing prices using various housing features on the training set. It prints the coefficients and variance score, then plots the residual errors of predictions on both training and test sets against a line for zero error.

Uploaded by

cnd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Linear Regression on Boston Dataset

This document loads the Boston housing dataset and splits it into training and test sets. It fits a linear regression model to predict housing prices using various housing features on the training set. It prints the coefficients and variance score, then plots the residual errors of predictions on both training and test sets against a line for zero error.

Uploaded by

cnd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

import matplotlib.

pyplot as plt
import numpy as np
from sklearn import datasets, linear_model, metrics

# load the boston dataset


boston = datasets.load_boston(return_X_y=False)

# defining feature matrix(X) and response vector(y)


X = [Link]
y = [Link]

# splitting X and y into training and testing sets


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
random_state=1)

# create linear regression object


reg = linear_model.LinearRegression()

# train the model using the training sets


[Link](X_train, y_train)

# regression coefficients
print('Coefficients: \n', reg.coef_)

# variance score: 1 means perfect prediction


print('Variance score: {}'.format([Link](X_test, y_test)))

# plot for residual error

## setting plot style


[Link]('fivethirtyeight')

## plotting residual errors in training data


[Link]([Link](X_train), [Link](X_train) - y_train,
color = "green", s = 10, label = 'Train data')

## plotting residual errors in test data


[Link]([Link](X_test), [Link](X_test) - y_test,
color = "blue", s = 10, label = 'Test data')

## plotting line for zero residual error


[Link](y = 0, xmin = 0, xmax = 50, linewidth = 2)

## plotting legend
[Link](loc = 'upper right')

## plot title
[Link]("Residual errors")

## function to show plot


[Link]()

You might also like