使用已安装的驱动器在colab中加载.npz

时间:2020-04-12 15:18:13

标签: python numpy google-colaboratory

如何在Google colab笔记本中加载从Google驱动器装入的.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

1 个答案:

答案 0 :(得分:0)

from scipy.sparse import load_npz
data = load_npz('/content/drive/My Drive/ColabNotebooks/project/data/dat.npz')

Documentation