【问题标题】:Loading .npz in colab using mounted drive使用已安装的驱动器在 colab 中加载 .npz
【发布时间】:2020-07-25 04:11:33
【问题描述】:

如何在 google colab notebook 中加载已从 google drive 挂载的 .npz 文件?

给定以下代码

from google.colab import drive
import numpy as np
drive.mount('/content/drive')
data = np.load('/content/drive/My Drive/Colab Notebooks/project/data/dat.npz')

我收到此错误

ValueError                                Traceback (most recent call last)
<ipython-input-40-168dd0fea229> in <module>()
      1 data={}
      2 for path in glob('/content/drive/My Drive/Colab Notebooks/lol/data/unzipped/*'):
----> 3   data[os.path.basename(os.path.splitext(path)[0])]=np.load(path)

/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    455             # Try a pickle
    456             if not allow_pickle:
--> 457                 raise ValueError("Cannot load file containing pickled data "
    458                                  "when allow_pickle=False")
    459             try:

ValueError: Cannot load file containing pickled data when allow_pickle=False

我已经尝试设置allow_pickle=True,但是导致了这个错误

---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    459             try:
--> 460                 return pickle.load(fid, **pickle_kwargs)
    461             except Exception:

EOFError: Ran out of input

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
1 frames
/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    461             except Exception:
    462                 raise IOError(
--> 463                     "Failed to interpret file %s as a pickle" % repr(file))
    464     finally:
    465         if own_fid:

OSError: Failed to interpret file '/content/drive/My Drive/Colab Notebooks/lol/data/unzipped/euk_test.npz' as a pickle

Python 版本:3.6.9

Numpy 版本:1.18.2

【问题讨论】:

    标签: python numpy google-colaboratory


    【解决方案1】:
    from scipy.sparse import load_npz
    data = load_npz('/content/drive/My Drive/ColabNotebooks/project/data/dat.npz')
    

    Documentation

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。