在 sklearn 中使用 datasets.fetch_mldata() 时出现 IO 错误

Posted

技术标签:

【中文标题】在 sklearn 中使用 datasets.fetch_mldata() 时出现 IO 错误【英文标题】:IO error when using datasets.fetch_mldata() in sklearn 【发布时间】:2014-11-23 23:38:59 【问题描述】:

我导入了 fetch_mldata 从 sklearn.datasets 导入 fetch_mldata 并调用:

dataset = fetch_mldata('MNIST original')

但我得到的是以下内容:

> Traceback (most recent call last):   File "<stdin>", line 1, in
> <module>   File
> "C:\Users\Jacob\Development\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
> line 540, in runfile
>     execfile(filename, namespace)   File "C:/Users/Jacob/Documents/Dropbox/Technion/Semester 8/Machine
> learning/Demo3/Demo3.py", line 75, in <module>
>     dataset = fetch_mldata('MNIST original')    File "C:\Users\Jacob\Development\Anaconda\lib\site-packages\sklearn\datasets\mldata.py",
> line 158, in fetch_mldata
>     matlab_dict = io.loadmat(matlab_file, struct_as_record=True)   File
> "C:\Users\Jacob\Development\Anaconda\lib\site-packages\scipy\io\matlab\mio.py",
> line 126, in loadmat
>     matfile_dict = MR.get_variables(variable_names)   File "C:\Users\Jacob\Development\Anaconda\lib\site-packages\scipy\io\matlab\mio5.py",
> line 288, in get_variables
>     res = self.read_var_array(hdr, process)   File "C:\Users\Jacob\Development\Anaconda\lib\site-packages\scipy\io\matlab\mio5.py",
> line 248, in read_var_array
>     return self._matrix_reader.array_from_header(header, process)   File "mio5_utils.pyx", line 616, in
> scipy.io.matlab.mio5_utils.VarReader5.array_from_header
> (scipy\io\matlab\mio5_utils.c:5903)   File "mio5_utils.pyx", line 645,
> in scipy.io.matlab.mio5_utils.VarReader5.array_from_header
> (scipy\io\matlab\mio5_utils.c:5332)   File "mio5_utils.pyx", line 713,
> in scipy.io.matlab.mio5_utils.VarReader5.read_real_complex
> (scipy\io\matlab\mio5_utils.c:6323)   File "mio5_utils.pyx", line 417,
> in scipy.io.matlab.mio5_utils.VarReader5.read_numeric
> (scipy\io\matlab\mio5_utils.c:3873)   File "mio5_utils.pyx", line 353,
> in scipy.io.matlab.mio5_utils.VarReader5.read_element
> (scipy\io\matlab\mio5_utils.c:3595)   File "streams.pyx", line 324, in
> scipy.io.matlab.streams.FileStream.read_string
> (scipy\io\matlab\streams.c:4343) IOError: could not read bytes

我尝试下载更新版本的 sklearn,但没有帮助。 我有关于这个问题的另一个线程,但那里提供的解决方案对我没有帮助。 How to use datasets.fetch_mldata() in sklearn?

有什么想法吗?

【问题讨论】:

【参考方案1】:

供您/其他人参考,我遇到了几乎相同的错误(Ubuntu),包括“IOError:无法读取字节”错误。

我刚刚在

上发布了一个解决方案

How to use datasets.fetch_mldata() in sklearn?

简答 - 使用以下内容:

from sklearn.datasets.mldata import fetch_mldata
    data = fetch_mldata('mnist-original')

dataset = fetch_mldata('mnist-original', data_home='***')

用您的首选位置(数据目录)替换 ***(保留引号)。

【讨论】:

【参考方案2】:

就我而言,根本原因是mnist-original.mat 文件损坏。该文件已损坏,因为我在文件完全下载之前终止了 Python。这在C:\user\Taimi\scikit_learn_data\mldata 留下了部分下载的mnist-original.mat

上面的解决方案对我有用,因为它只是在新位置获取新副本。更直接的解决方案是找到损坏的mnist-original.mat 文件,将其删除并再次尝试运行代码。运行代码将再次下载mnist-original.mat。完整的mnist-original.mat 大小为54,142 KB,因此如果您的连接速度较慢,fetch_mldata() 将需要几分钟才能完成。

【讨论】:

以上是关于在 sklearn 中使用 datasets.fetch_mldata() 时出现 IO 错误的主要内容,如果未能解决你的问题,请参考以下文章

在安卓设备中使用 sklearn

在 Python 中使用 sklearn 使用 MAE 训练线性模型

sklearn中SVM的实现

使用 sklearn 在嵌套交叉验证中使用 GroupKFold

在 sklearn 中使用 DictVectorizer 后如何获得分类特征的重要性

使用 sklearn 在数据中保持零