问题

加载加州房价数据时出现 403 错误 HTTP Error 403: Forbidden

from sklearn.datasets import fetch_california_housing

california = fetch_california_housing()
print(california.target.shape) 

解决方案

运行下述代码,然后再运行上述的 fetch_california_housing() 可成功运行

import requests
import os
import tarfile
import numpy as np
from types import SimpleNamespace

from sklearn import datasets
# 参考: 
# https://blog.csdn.net/getalong/article/details/141201658
# https://inria.github.io/scikit-learn-mooc/python_scripts/datasets_california_housing.html

fetch_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: None

This dataset was obtained from the StatLib repository.
https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html

The 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.edu
        columns_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 / households
        data[:, 2] /= data[:, 5]
        
        # avg bed rooms = total bed rooms / households
        data[:, 3] /= data[:, 5]
        
        # avg occupancy = population / households
        data[:, 5] = data[:, 4] / data[:, 5]
        
        # target in units of 100,000
        target = target / 100000.0

        result = {
            'data': data,
            'target': target,
            'feature_names': feature_names,
            'target_names': target_names,
            'DESCR': fetch_california_housing_manual_desc,
        }
        obj = SimpleNamespace(**result)
        return obj

california = fetch_california_housing_manual()
print(california.data)
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐