sklearn.datasets.load_boston

sklearn.datasets.load_boston(*, return_X_y=False)[source]

DEPRECATED: load_boston is deprecated in 1.0 and will be removed in 1.2.

The Boston housing prices dataset has an ethical problem. You can refer to the documentation of this function for further details.

The scikit-learn maintainers therefore strongly discourage the use of this dataset unless the purpose of the code is to study and educate about ethical issues in data science and machine learning.

In this special case, you can fetch the dataset from the original source:

import pandas as pd
import numpy as np

data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]

Alternative datasets include the California housing dataset (i.e. fetch_california_housing) and the Ames housing dataset. You can load the datasets as follows:

from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()

for the California housing dataset and:

from sklearn.datasets import fetch_openml
housing = fetch_openml(name="house_prices", as_frame=True)

for the Ames housing dataset.

Load and return the Boston house-prices dataset (regression).

Samples total

506

Dimensionality

13

Features

real, positive

Targets

real 5. - 50.

Read more in the User Guide.

Warning

The Boston housing prices dataset has an ethical problem: as investigated in [1], the authors of this dataset engineered a non-invertible variable “B” assuming that racial self-segregation had a positive impact on house prices [2]. Furthermore the goal of the research that led to the creation of this dataset was to study the impact of air quality but it did not give adequate demonstration of the validity of this assumption.

The scikit-learn maintainers therefore strongly discourage the use of this dataset unless the purpose of the code is to study and educate about ethical issues in data science and machine learning.

In this special case, you can fetch the dataset from the original source:

import pandas as pd  # doctest: +SKIP
import numpy as np

data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]

Alternative datasets include the California housing dataset [3] (i.e. fetch_california_housing) and Ames housing dataset [4]. You can load the datasets as follows:

from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()

for the California housing dataset and:

from sklearn.datasets import fetch_openml
housing = fetch_openml(name="house_prices", as_frame=True)

for the Ames housing dataset.

Parameters:
return_X_ybool, default=False

If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.

New in version 0.18.

Returns:
dataBunch

Dictionary-like object, with the following attributes.

datandarray of shape (506, 13)

The data matrix.

targetndarray of shape (506,)

The regression target.

filenamestr

The physical location of boston csv dataset.

New in version 0.20.

DESCRstr

The full description of the dataset.

feature_namesndarray

The names of features

(data, target)tuple if return_X_y is True

A tuple of two ndarrays. The first contains a 2D array of shape (506, 13) with each row representing one sample and each column representing the features. The second array of shape (506,) contains the target samples.

New in version 0.18.

Notes

Changed in version 0.20: Fixed a wrong data point at [445, 0].

References

Examples

>>> import warnings
>>> from sklearn.datasets import load_boston
>>> with warnings.catch_warnings():
...     # You should probably not use this dataset.
...     warnings.filterwarnings("ignore")
...     X, y = load_boston(return_X_y=True)
>>> print(X.shape)
(506, 13)