【问题标题】:How to save the results of an np.array for future use when using Google Colab使用 Google Colab 时如何保存 np.array 的结果以供将来使用
【发布时间】:2020-08-24 04:40:31
【问题描述】:

我正在做一个信息检索项目。为此,我正在使用 Google Colab。我正处于计算一些特征(“input_features”)并且通过执行 for 循环获得标签(“labels”)的阶段,这花了我大约4小时完成。

所以最后我将结果附加到一个数组中:

input_features = np.array(input_features)
labels = np.array(labels)

所以我的问题是: 是否可以保存这些结果以便在使用 google colab 时将其用于将来的目的?

我找到了 2 个可能适用的选项,但我不知道这些文件是在哪里创建的。

1) 将它们保存为 csv 文件。我的代码是:

from numpy import savetxt
# save to csv file
savetxt('input_features.csv', input_features, delimiter=',')
savetxt('labels.csv', labels, delimiter=',')

为了加载它们:

from numpy import loadtxt
# load array
input_features = loadtxt('input_features.csv', delimiter=',')
labels = loadtxt('labels.csv', delimiter=',')
# print the array
print(input_features)
print(labels)

但是当我打印时我仍然没有得到任何东西。


2) 使用 pickle 保存数组的结果,我从这里按照以下说明进行操作: https://colab.research.google.com/drive/1EAFQxQ68FfsThpVcNU7m8vqt4UZL0Le1#scrollTo=gZ7OTLo3pw8M

from google.colab import files
import pickle
def features_pickeled(input_features, results):
  input_features = input_features + '.txt'
  pickle.dump(results, open(input_features, 'wb'))
  files.download(input_features)
def labels_pickeled(labels, results):
  labels = labels + '.txt'
  pickle.dump(results, open(labels, 'wb'))
  files.download(labels)

然后将它们加载回来:

def load_from_local():
  loaded_features = {}
  uploaded = files.upload()
  for input_features in uploaded.keys():
      unpickeled_features = uploaded[input_features]
      loaded[input_features] = pickle.load(BytesIO(data)) 
  return loaded_features 
def load_from_local():
  loaded_labels = {}
  uploaded = files.upload()
  for labels in uploaded.keys():
      unpickeled_labels = uploaded[labels]
      loaded[labels] = pickle.load(BytesIO(data))
  return loaded_labes

#How do I print the pickled files to see if I have them ready for use???

当使用 python 时,我会为泡菜做这样的事情:

#Create pickle file
with open("name.pickle", "wb") as pickle_file:
     pickle.dump(name, pickle_file)
#Load the pickle file
with open("name.pickle", "rb") as name_pickled:
     name_b = pickle.load(name_pickled)

但问题是我没有在我的谷歌驱动器中看到任何要创建的文件。

我的代码是正确的还是我遗漏了部分代码?

为了希望详细说明我想要做什么以及我为这个问题做了什么。

提前感谢您的帮助。

【问题讨论】:

    标签: arrays pickle google-colaboratory


    【解决方案1】:
    # Mount driver to authenticate yourself to gdrive
    from google.colab import drive
    drive.mount('/content/gdrive')
    
    #---
    
    # Import necessary libraries
    import numpy as np
    from numpy import savetxt
    import pandas as pd
    
    #---
    
    # Create array
    arr = np.array([1, 2, 3, 4, 5])
    
    # save to csv file
    savetxt('arr.csv', arr, delimiter=',')  # You will see the results if you press in the File icon (left panel)
    

    然后您可以通过以下方式再次加载它:

    # You can copy the path when you find your file in the file icon
    arr = pd.read_csv('/content/arr.csv', sep=',', header=None) # You can also save your result as a txt file
    arr
    

    【讨论】:

      【解决方案2】:

      Google Colaboratory 笔记本实例无法保证在您断开连接并重新连接时能够访问相同的资源,因为它们是在虚拟机上运行的。因此,您无法在 Colab 中“保存”您的数据。以下是一些解决方案:

      1. Colab 会保存您的代码。如果您引用的 for 循环操作不需要很长时间即可运行,只需保留代码并在每次连接笔记本时运行它。
      2. 查看np.save。此功能允许您将数组保存到二进制文件中。然后,您可以在重新连接笔记本时重新上传二进制文件。更好的是,您可以将二进制文件存储在 Google Drive 上,mount your drive to your notebook,然后像这样引用它。

      【讨论】:

      • @Nikos Papas 也可以看看this question。看起来这可能与您的要求相似。
      • 非常感谢您的回答,因为您让我有了不同的想法。让其他人知道,这可能和我有类似的问题,您可以创建 csv 文件:!ls #see what files do you have in your google drive import csv file_name_csv= [original_table] with open("file_name_csv.csv", "w") as f: writer = csv.writer(f) writer.writerows(file_name_csv) 在左下角,您按下“文件”的小图标,然后您的 csv 文件从那里你可以下载它。
      • @LedianK。您是否介意展示您的完整代码,以了解您是如何将数组保存到 gdrive 目录以及如何将其再次加载到 colab 中的?
      • @YogeshRiyat 我发布这个问题已经有一段时间了,但您可以查看下面的代码。希望这对你有帮助:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多