我正在做一个信息检索项目。为此,我正在使用 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)
但问题是我没有在我的谷歌驱动器中看到任何要创建的文件。
我的代码是正确的还是我错过了代码的某些部分?
详细的描述,希望能详细解释我想要做什么以及我为这个问题做了什么。
预先感谢您的帮助。