Python 如何手动加载sklearn数据集?
我想从Python 如何手动加载sklearn数据集?,python,scikit-learn,Python,Scikit Learn,我想从sklearndatatsets(加州房价)加载一个更大的数据集。由于代理问题(数据集下载已损坏),使用默认命令对我无效 下载后,我将其放在数据集找到的目录中。get_data_home()(/home/username/scikit_learn_data/)并将文件放在该文件夹中以及该目录下的cal_housing文件夹和CaliforniaHousing文件夹中,以检查所有选项。我还尝试使用data_home参数指定自定义位置 d = datasets.fetch_california
sklearn
datatsets(加州房价)加载一个更大的数据集。由于代理问题(数据集下载已损坏),使用默认命令对我无效
下载后,我将其放在数据集找到的目录中。get_data_home()(/home/username/scikit_learn_data/
)并将文件放在该文件夹中以及该目录下的cal_housing文件夹和CaliforniaHousing文件夹中,以检查所有选项。我还尝试使用data_home参数指定自定义位置
d = datasets.fetch_california_housing(data_home='/home/username/scikit_learn_data/')
什么都不管用
如何手动加载数据集
注:
要测试手动加载是否有效,请设置
download\u if_missing=False
我也有同样的问题,因为我的开发环境无法访问web,因此默认的“download\u if_missing”当然无法工作
如果您遵循URL,您将能够从web上保存cal_housing.tgz文件。但是如果缺少fork并且需要一个pkz文件,fetch_california_housing方法实际上会在默认的download_中进行更多的转换(正如上面Vivek所指出的)。我在这里找到了创建.pkz文件的解决方法:()。我在这里转载了奥列恩的答案(荣誉!):
引述:
"
我可以给你一个解决办法:
使用web浏览器手动下载数据:
确保下载的文件名为calu housing.tgz。
执行以下Python代码:
import numpy as np
import os
import tarfile
from sklearn.externals import joblib
from sklearn.datasets.base import _pkl_filepath, get_data_home
archive_path = "cal_housing.tgz" # change the path if it's not in the current directory
data_home = get_data_home(data_home=None) # change data_home if you are not using ~/scikit_learn_data
if not os.path.exists(data_home):
os.makedirs(data_home)
filepath = _pkl_filepath(data_home, 'cal_housing.pkz')
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]
joblib.dump(cal_housing, filepath, compress=6)
之后,您应该能够毫无问题地使用fetch_california_housing()。这是因为上面的代码是fetch_california_housing()在下载数据后第一次调用它时所做的:它准备数据并将其保存在Scikit Learn的数据目录(默认情况下为$HOME/Scikit_Learn_data)的缓存中。下次调用fetch_california_housing()时,它只加载此目录中的数据,因此不需要下载
希望这有帮助,
奥雷连
"
我也有同样的问题,因为我的开发环境无法访问web,所以默认的“如果缺少下载”当然无法工作 如果您遵循URL,您将能够从web上保存cal_housing.tgz文件。但是如果缺少fork并且需要一个pkz文件,fetch_california_housing方法实际上会在默认的download_中进行更多的转换(正如上面Vivek所指出的)。我在这里找到了创建.pkz文件的解决方法:()。我在这里转载了奥列恩的答案(荣誉!): 引述: " 我可以给你一个解决办法: 使用web浏览器手动下载数据: 确保下载的文件名为calu housing.tgz。 执行以下Python代码:
import numpy as np
import os
import tarfile
from sklearn.externals import joblib
from sklearn.datasets.base import _pkl_filepath, get_data_home
archive_path = "cal_housing.tgz" # change the path if it's not in the current directory
data_home = get_data_home(data_home=None) # change data_home if you are not using ~/scikit_learn_data
if not os.path.exists(data_home):
os.makedirs(data_home)
filepath = _pkl_filepath(data_home, 'cal_housing.pkz')
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]
joblib.dump(cal_housing, filepath, compress=6)
之后,您应该能够毫无问题地使用fetch_california_housing()。这是因为上面的代码是fetch_california_housing()在下载数据后第一次调用它时所做的:它准备数据并将其保存在Scikit Learn的数据目录(默认情况下为$HOME/Scikit_Learn_data)的缓存中。下次调用fetch_california_housing()时,它只加载此目录中的数据,因此不需要下载
希望这有帮助,
奥雷连
"
Auréliens的末尾引用另一个选项是:
from sklearn.datasets import california_housing
data = california_housing.fetch_california_housing()
calf_hous_df = pd.DataFrame(data= data.data, columns=data.feature_names)
calf_hous_df.head(5)
Out[105]:
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude \
0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88
1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86
2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85
3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85
4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85
Longitude
0 -122.23
1 -122.22
2 -122.24
3 -122.25
4 -122.25
我知道这有点难看,因为您必须更改内部python包文件。但是它很管用。你试过这个吗?隐马尔可夫模型。。datatset以两个特定格式的文件的形式出现,这些文件应该一起加载,因此不确定它是否可以工作。能否显示您的代码和错误?您是如何下载的,下载了什么,以及将其放在哪里的?要使用
fetch_california_housing
,在data_home
目录中应该有一个文件cal_housing.pkz
您下载了什么文件,放在哪里?您尝试过这个吗?隐马尔可夫模型。。datatset以两个特定格式的文件的形式出现,这些文件应该一起加载,因此不确定它是否可以工作。能否显示您的代码和错误?您是如何下载的,下载了什么,以及将其放在哪里的?要使用fetch_california_housing
,在data_home
目录中应该有一个文件calu housing.pkz
您下载了什么文件,放在哪里?请解释一下!请解释一下!绝妙的解决方案,为我工作!如果sklearn.externals import joblib的这一行不起作用,请直接使用import joblib
brilliant solution,为我工作!如果sklearn.externals import joblib中的这一行不起作用,请直接使用import joblib
from sklearn.datasets import california_housing
data = california_housing.fetch_california_housing()
calf_hous_df = pd.DataFrame(data= data.data, columns=data.feature_names)
calf_hous_df.head(5)
Out[105]:
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude \
0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88
1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86
2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85
3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85
4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85
Longitude
0 -122.23
1 -122.22
2 -122.24
3 -122.25
4 -122.25