【完整版解决方案】sklearn加州房价数据集出错 housing = fetch_california_housing() HTTPError: HTTP Error 403: Forbidden

完整解决方案(一键复制)代码替换housing = fetch_california_housing()

翻了几条解决方案要么不全,要么收费,烦死个人下面给出完整解决方案!!!

1、下载数据集

原始数据集: cal_housing.tgz

在这里插入图片描述

2、放置数据集

查找 本地位置,执行代码后进入目标文件夹。

from sklearn import datasets
data_home = datasets.get_data_home()

在这里插入图片描述
放入下载好的数据压缩包,注意不需要解压缩!!
在这里插入图片描述

3、替换代码片段

calhous = fetch_california_housing()
data = calhous.data
target = calhous.target

替换为

from sklearn import datasets
import tarfile
data_home = datasets.get_data_home()
archive_path = os.path.join(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, 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

解决!!!
因为粘贴代码时有缩进,手动删除时可能出现缩进不正确的问题,自行加减空格就行了。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值