# ML | Boston Housing Kaggle Challenge with Linear Regression

| | | | | | | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Dataset description taken from

Let’s make a linear regression model predicting prices on housing

Libraries input and dataset.

 ` # Importing libraries ` ` import ` ` numpy as np ` ` import ` ` pandas as pd ` ` import ` ` matplotlib.pyplot as plt ` ` # Data import ` ` from ` ` sklearn.datasets ` ` import ` ` load_boston ` ` boston ` ` = ` ` load_boston () `

Boston data entry and function_names form

 ` boston.data.shape ` ` `

` boston.feature_names `

` ` Converting nd array data to data frame and adding data names into data

 ` data ` ` = ` ` pd.DataFrame (boston.data) ` ` data.columns ` ` = ` ` boston.feature_names ` ` data.head (` ` 10 ` `) ` Adding the "Price" column to the dataset

 ` # Adding the "Price‚" column (target) to the data ` ` boston.target.shape ` ` data [` ` ’Price’ ` `] ` ` = ` ` boston.target ` ` data.head () ` Boston dataset description

 ` data.describe () ` Boston dataset information

` `

` data.info () ` Getting input and output data and further dividing the data into a dataset for training and testing.

` `

` # Input data x = boston.data # Output y = boston.target # splitting data into training and test suites data. from sklearn.cross_validation import train_test_split xtrain, xtest, ytrain, ytest = train_test_split (x, y, test_size = 0.2 , random_state = 0 ) print ( "xtrain shape:" , xtrain.shape) print ( "xtest shape :" , xtest.shape) print ( " ytrain shape: " , ytrain.shape) print ( "ytest shape :" , ytest.shape) ` Applying a linear regression model to a dataset and price prediction.

 ` # Fitting the ML regression model to the learning model ` ` from ` ` sklearn.linear_model ` ` import ` ` LinearRegression ` ` regressor ` ` = ` ` LinearRegression () ` ` regressor.fit (xtrain, ytrain) ` ` # predicting test case results ` ` y_pred ` ` = ` ` regressor.predict (xtest) `

Build a scatter plot to display the forecast results — ytrue value versus y_pred value

 ` # Scatter plot to display the forecast ` ` # results - ytrue value versus y_pred value ` ` plt.scatter (ytest, y_pred, c ` ` = ` ` ’green’ ` `) ` ` plt.xlabel (` ` "Price: in \$ 1000’s" ` `) ` ` plt.ylabel (` ` "Predicted value" ` `) ` ` plt .title (` ` "True value vs predicted value: Linear Regression" ` `) ` ` plt.show () ` Linear regression results, i.e. root mean square error.

 ` # Linear regression results. ` ` from ` ` sklearn.metrics ` ` import ` ` mean _squared_error ` ` mse ` ` = ` ` mean _squared_error (ytest, y_pred) ` ` print ` ` (` ` "Mean Square Error:" ` `, mse) ` ## Shop Learn programming in R: courses

\$FREE Best Python online courses for 2022

\$FREE Best laptop for Fortnite

\$399+ Best laptop for Excel

\$ Best laptop for Solidworks

\$399+ Best laptop for Roblox

\$399+ Best computer for crypto mining

\$499+ Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries