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)

laptop for engineering students?

ML | Boston Housing Kaggle Challenge with Linear Regression __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).

2973

Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

ML | Boston Housing Kaggle Challenge with Linear Regression __del__: Questions

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

We hope this article has helped you to resolve the problem. Apart from ML | Boston Housing Kaggle Challenge with Linear Regression, check other __del__-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Carlo Robinson

Shanghai | 2023-02-08

Simply put and clear. Thank you for sharing. ML | Boston Housing Kaggle Challenge with Linear Regression and other issues with os Python module was always my weak point 😁. I am just not quite sure it is the best method

Davies Emmerson

New York | 2023-02-08

Thanks for explaining! I was stuck with ML | Boston Housing Kaggle Challenge with Linear Regression for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Carlo OConnell

San Francisco | 2023-02-08

array Python module is always a bit confusing 😭 ML | Boston Housing Kaggle Challenge with Linear Regression is not the only problem I encountered. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


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

Python add suffix / add prefix to strings in a list

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

Python - Move item to the end of the list

Python - Print list vertically