🏠

機械学習の回帰データとしては、ボストン住宅価格データではなく、カリフォルニア住宅価格データを使おう

2022/01/19に公開

結論・サンプルコード

回帰のデータセットが欲しい場合、ボストンデータセットではなく、カリフォルニアデータセットを使いましょう。

  • ソースコード
from sklearn.datasets import fetch_california_housing

california_housing = fetch_california_housing()
train_x = pd.DataFrame(california_housing.data, columns=california_housing.feature_names)
train_y = pd.Series(california_housing.target)

train_x.head()
train_y.head()
  • 結果

0 4.526
1 3.585
2 3.521
3 3.413
4 3.422


背景

ボストン住宅価格データはポリティカルにヤバいため、scikit-learnでも取り扱わないことになったようです。

参考:https://twitter.com/tokoroten/status/1394192087453638662


scikit-learnの1系以降では、ボストンデータセットを読みこもうとすると警告文が出てきます。

from sklearn import datasets
boston_data = datasets.load_boston()
/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning:

Function load_boston is 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.
    :func:`~sklearn.datasets.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.

Discussion