jonkrohn / ML-foundations

Machine Learning Foundations: Linear Algebra, Calculus, Statistics & Computer Science
MIT License
3.39k stars 1.63k forks source link

Code "from sklearn.datasets import load_boston" in 6-statistics.ipynb in Ordinary Least Squares Exercises Section is throwing Error in CoLab because `load_boston` has been removed from scikit-learn since version 1.2 #9

Closed sagarvelankar closed 3 weeks ago

sagarvelankar commented 1 year ago

Code "from sklearn.datasets import load_boston" in 6-statistics.ipynb in Ordinary Least Squares Exercises Section is throwing Error in CoLab because load_boston has been removed from scikit-learn since version 1.2

File : https://github.com/jonkrohn/ML-foundations/blob/master/notebooks/6-statistics.ipynb

Section : Ordinary Least Squares Exercises

Code : from sklearn.datasets import load_boston

Error : ImportError Traceback (most recent call last) in <cell line: 1>() ----> 1 from sklearn.datasets import load_boston

/usr/local/lib/python3.10/dist-packages/sklearn/datasets/init.py in getattr(name) 154 """ 155 ) --> 156 raise ImportError(msg) 157 try: 158 return globals()[name]

ImportError: load_boston has been removed from scikit-learn since version 1.2.

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
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 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.

[1] M Carlisle. "Racist data destruction?" https://medium.com/@docintangible/racist-data-destruction-113e3eff54a8

[2] Harrison Jr, David, and Daniel L. Rubinfeld. "Hedonic housing prices and the demand for clean air." Journal of environmental economics and management 5.1 (1978): 81-102. https://www.researchgate.net/publication/4974606_Hedonic_housing_prices_and_the_demand_for_clean_air

NOTE: If your import is failing due to a missing package, you can manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the "Open Examples" button below.

Siddharthrajrana commented 1 year ago

from sklearn.datasets import fetch_openml

boston = fetch_openml(data_id=42165, as_frame=True) X = boston.data y = boston.target

jonkrohn commented 1 year ago

Thanks, Siddharth!

Jon Krohn, Ph.D.

Co-Founder and Chief Data Scientist

https://nebula.io/ 408 W 14th St, New York NY 10014

Author of Deep Learning Illustrated https://www.deeplearningillustrated.com/, a #1-bestseller on neural networks.

Host of SuperDataScience https://www.jonkrohn.com/podcast, the industry's most listened-to podcast.

Creator of an award-winning math and machine learning YouTube channel https://www.youtube.com/JonKrohnLearns.

https://www.linkedin.com/in/jonkrohn/[image: Twitter - Free social media icons] https://twitter.com/JonKrohnLearns https://github.com/jonkrohn

On Thu, 31 Aug 2023 at 07:51, Siddharth Raj Rana @.***> wrote:

from sklearn.datasets import fetch_openml

boston = fetch_openml(data_id=42165, as_frame=True) X = boston.data y = boston.target

— Reply to this email directly, view it on GitHub https://github.com/jonkrohn/ML-foundations/issues/9#issuecomment-1700893417, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACMXANYSAU7GN5AXSFUOG2TXYB3FFANCNFSM6AAAAAAZAPAEJI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jonkrohn commented 3 weeks ago

All-righty, given the ethical dilemmas associated with the Boston housing dataset, I've updated the Jupyter notebook to use the California housing dataset instead. I'll make the commit shortly (probably within the next couple hours).