首页 > Python资料 博客日记

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

2024-10-31 16:00:05Python资料围观6

这篇文章介绍了【完整版解决方案】sklearn加州房价数据集出错 housing = fetch_california_housing() HTTPError: HTTP Error 403: Forbidden,分享给大家做个参考,收藏Python资料网收获更多编程知识

完整解决方案(一键复制)代码替换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

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


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐