sklearn 加州房价数据集 fetch_california_housing 出错 403: Forbidden 修复方案
问题
加载加州房价数据时出现 403 错误 HTTP Error 403: Forbidden
from sklearn.datasets import fetch_california_housingcalifornia = fetch_california_housing()
print(california.target.shape)
解决方案
运行下述代码,然后再运行上述的 fetch_california_housing()
可成功运行
import requests
import os
import tarfile
import numpy as np
from types import SimpleNamespacefrom sklearn import datasets
# 参考:
# https://blog.csdn.net/getalong/article/details/141201658
# https://inria.github.io/scikit-learn-mooc/python_scripts/datasets_california_housing.htmlfetch_california_housing_manual_desc = '''
.. _california_housing_dataset:California Housing dataset
--------------------------**Data Set Characteristics:**:Number of Instances: 20640:Number of Attributes: 8 numeric, predictive attributes and the target:Attribute Information:- MedInc median income in block group- HouseAge median house age in block group- AveRooms average number of rooms per household- AveBedrms average number of bedrooms per household- Population block group population- AveOccup average number of household members- Latitude block group latitude- Longitude block group longitude:Missing Attribute Values: NoneThis dataset was obtained from the StatLib repository.
https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.htmlThe target variable is the median house value for California districts,
expressed in hundreds of thousands of dollars ($100,000).This dataset was derived from the 1990 U.S. census, using one row per census
block group. A block group is the smallest geographical unit for which the U.S.
Census Bureau publishes sample data (a block group typically has a population
of 600 to 3,000 people).A household is a group of people residing within a home. Since the average
number of rooms and bedrooms in this dataset are provided per household, these
columns may take surprisingly large values for block groups with few households
and many empty houses, such as vacation resorts.It can be downloaded/loaded using the
:func:`sklearn.datasets.fetch_california_housing` function... rubric:: References- Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,Statistics and Probability Letters, 33 (1997) 291-297
'''def download_file(url, directory, filename):# 确保目录存在os.makedirs(directory, exist_ok=True)# 完整文件路径filepath = os.path.join(directory, filename)# 下载文件response = requests.get(url, stream=True)response.raise_for_status() # 检查请求是否成功# 将内容写入文件with open(filepath, 'wb') as file:for chunk in response.iter_content(chunk_size=8192):file.write(chunk)print(f"文件已下载到: {filepath}")def fetch_california_housing_manual():data_home = datasets.get_data_home()archive_path = os.path.join(data_home, 'cal_housing.tgz')if not os.path.exists(archive_path):download_file("https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.tgz", data_home, 'cal_housing.tgz')with tarfile.open(mode="r:gz", name=archive_path) as f:cal_housing = np.loadtxt(f.extractfile("CaliforniaHousing/cal_housing.data"), delimiter=",")# Columns are not in the same order compared to the previous# URL resource on lib.stat.cmu.educolumns_index = [8, 7, 2, 3, 4, 5, 6, 1, 0]cal_housing = cal_housing[:, columns_index]feature_names = ["MedInc","HouseAge","AveRooms","AveBedrms","Population","AveOccup","Latitude","Longitude",]target_names = ['MedHouseVal']target, data = cal_housing[:, 0], cal_housing[:, 1:]# avg rooms = total rooms / householdsdata[:, 2] /= data[:, 5]# avg bed rooms = total bed rooms / householdsdata[:, 3] /= data[:, 5]# avg occupancy = population / householdsdata[:, 5] = data[:, 4] / data[:, 5]# target in units of 100,000target = target / 100000.0result = {'data': data,'target': target,'feature_names': feature_names,'target_names': target_names,'DESCR': fetch_california_housing_manual_desc,}obj = SimpleNamespace(**result)return objcalifornia = fetch_california_housing_manual()
print(california.data)