0% found this document useful (0 votes)
56 views3 pages

Experiment No 2 ML

This document describes an experiment using linear regression on the Boston housing dataset to predict housing prices. The dataset is split into training and test sets. A linear regression model is trained on the training set and used to predict values for the training and test sets. The coefficients, variance score, and a plot of residual errors for both sets are output to analyze the model's performance. The conclusion states that this experiment demonstrated the basic process for developing a linear regression machine learning application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Experiment No 2 ML

This document describes an experiment using linear regression on the Boston housing dataset to predict housing prices. The dataset is split into training and test sets. A linear regression model is trained on the training set and used to predict values for the training and test sets. The coefficients, variance score, and a plot of residual errors for both sets are output to analyze the model's performance. The conclusion states that this experiment demonstrated the basic process for developing a linear regression machine learning application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment no-2

Aim: A Case study on learning with linear regression.

Program:
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 = boston.data
y = boston.target

# 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


reg.fit(X_train, y_train)

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

# variance score: 1 means perfect prediction


print('Variance score: {}'.format(reg.score(X_test, y_test)))

# plot for residual error

## setting plot style


plt.style.use('fivethirtyeight')
## plotting residual errors in training data
plt.scatter(reg.predict(X_train), reg.predict(X_train) - y_train,
color = "green", s = 10, label = 'Train data')

## plotting residual errors in test data


plt.scatter(reg.predict(X_test), reg.predict(X_test) - y_test,
color = "blue", s = 10, label = 'Test data')

## plotting line for zero residual error


plt.hlines(y = 0, xmin = 0, xmax = 50, linewidth = 2)

## plotting legend
plt.legend(loc = 'upper right')

## plot title
plt.title("Residual errors")

## method call for showing the plot


plt.show()

Output:
Graph:

Conclusion:
Thus we have studied the core concept of machine learning and steps
involved in developing a machine learning application about linear regression.

You might also like