Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用Google Colab时如何保存np.array的结果以备将来使用_Arrays_Pickle_Google Colaboratory - Fatal编程技术网

Arrays 使用Google Colab时如何保存np.array的结果以备将来使用

Arrays 使用Google Colab时如何保存np.array的结果以备将来使用,arrays,pickle,google-colaboratory,Arrays,Pickle,Google Colaboratory,我正在做一个信息检索项目。为此,我使用谷歌Colab。我正在计算一些特性输入特性,通过执行for循环得到标签,这花费了我大约4个小时来完成 最后,我将结果附加到一个数组中: input_features = np.array(input_features) labels = np.array(labels) 所以我的问题是: 在使用google colab时,是否可以保存这些结果以便将来使用 我发现了两个可能适用的选项,但我不知道这些文件是在哪里创建的 1将其保存为csv文件。我的代码是: f

我正在做一个信息检索项目。为此,我使用谷歌Colab。我正在计算一些特性输入特性,通过执行for循环得到标签,这花费了我大约4个小时来完成

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

input_features = np.array(input_features)
labels = np.array(labels)
所以我的问题是: 在使用google colab时,是否可以保存这些结果以便将来使用

我发现了两个可能适用的选项,但我不知道这些文件是在哪里创建的

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保存数组的结果,我在这里按照以下说明操作:

要重新加载它们,请执行以下操作:

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时,我会对pickle执行以下操作:

#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)
但问题是,我在谷歌硬盘上看不到任何要创建的文件

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

为了详细解释我想做什么以及我为这个问题做了什么,我做了很长的描述


提前感谢您的帮助。

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

Colab保存您的代码。如果您引用的for-loop操作没有花费大量时间来运行,那么只需保留代码,并在每次连接笔记本时运行它即可。 退房此函数允许您将数组保存到二进制文件。然后,当你重新连接笔记本时,你可以重新上传你的二进制文件。更好的是,您可以将二进制文件存储在GoogleDrive上,并像这样引用它。
当您断开连接并重新连接时,Google Colaboratory笔记本实例永远不能保证能够访问相同的资源,因为它们在虚拟机上运行。因此,无法将数据保存在Colab中。以下是一些解决方案:

Colab保存您的代码。如果您引用的for-loop操作没有花费大量时间来运行,那么只需保留代码,并在每次连接笔记本时运行它即可。 退房此函数允许您将数组保存到二进制文件。然后,当你重新连接笔记本时,你可以重新上传你的二进制文件。更好的是,您可以将二进制文件存储在GoogleDrive上,并像这样引用它。
@尼科斯·帕帕斯也来看看。看起来这可能和你问的差不多。非常感谢你的回答,因为你让我有了不同的想法。为了让其他人知道,可能会有一个与我类似的问题,您可以创建csv文件:!ls查看您的google drive中有哪些文件导入csv文件\u name\u csv=[original_table]和openfile\u name\u csv.csv,w为f:writer=csv.writer f writer.writerowsfile\u name\u csv,在左角按文件的小图标,这是您的csv文件,您可以从那里下载。@Nikos Papas也看一下。看起来这可能和你问的差不多。非常感谢你的回答,因为你让我有了不同的想法。为了让其他人知道,可能会有一个与我类似的问题,您可以创建csv文件:!ls查看google drive中有哪些文件导入csv文件\u name\u csv=[original\u table]和openfile\u name\u csv.csv,w为f:writer=csv.writer/writer.writerowsfile\u name\u csv,在左角按文件的小图标,这是您的csv文件,您可以从那里下载它。
#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)