csv 如何解压缩一个pkl文件?

vc9ivgsu  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(244)

我需要解压缩一个pkl文件,但是因为我不熟悉pickle和panda,所以我很难尝试去做。
pkl文件的内容如下所示:

{
'woodi': array([-0.07377538,  0.01810472,  0.03796827, -0.01185564, -0.12605625,
   -0.03709966,  0.07863396,  0.04245366, -0.09158159, -0.01418831,
   -0.03165198, -0.01235643,  0.00833164, -0.08156401, -0.10466748,
    0.11343367, -0.1291647 ,  0.02277501, -0.12230705,  0.08400519,
    0.01631752, -0.03204752, -0.10115118,  0.01796065, -0.08914784,
    0.00336748,  0.02858992,  0.13387977, -0.01711662, -0.05058149,
    0.09866285,  0.00623399, -0.11368696,  0.03389056,  0.03049786,
   -0.11235228,  0.03964651,  0.18348881,  0.00356622, -0.09299972,
    0.11804404,  0.10598116,  0.04603285,  0.10211086, -0.07094006,
    0.19667923, -0.22645354, -0.02930884, -0.21891772, -0.07495865]),
'bad-boy': array([-0.01525861, -0.0145514 ,  0.02207321,  0.01273549,  0.0034881 ,
       -0.00045474,  0.01104943,  0.00057228, -0.01515725,  0.00329882,
        0.01570324, -0.03927545,  0.00393151,  0.00355666, -0.00503297,
       -0.01088151, -0.0354947 , -0.010477  , -0.01945165,  0.0312498 ,
        0.00195288, -0.03095445, -0.00803227,  0.02864361, -0.01416729,
        0.00375061,  0.00546439,  0.03621898,  0.01337988, -0.03205173,
        0.00451094,  0.02180656, -0.02587242, -0.01276209,  0.02721113,
       -0.00075289, -0.00218841,  0.00531534, -0.0074188 ,  0.00312647,
        0.00424174,  0.02444418,  0.0222739 , -0.00477895,  0.02220114,
        0.03402764, -0.02423164,  0.00724037, -0.03526915,  0.01470344]),
...
}

我需要获得单词和每个单词的实值向量,然后创建一个csv文件... csv文件的内容必须如下所示:

woodi -0.07377538 0.01810472 ... -0.07495865
bad-boy -0.01525861 -0.0145514 ... 0.01470344

我试过这个python代码:

import pickle
import pandas as pd

fin = 'SGlove.pkl'
fout = 'SGlove.csv'

words, embeddings = pickle.load(open(fin, 'rb'), encoding='latin1')

m, n = embeddings.shape
print("Emebddings contains {} words embedded as vectors of length {}".format(m, n))

df = pd.DataFrame(embeddings)
df.insert(0, "word", words)
df.to_csv(fout, header=False, index=False, sep=" ")

但我得到了以下错误消息:

Traceback (most recent call last):
  File "pkl_to_csv.py", line 10, in <module>
    words, embeddings = pickle.load(open(fin, 'rb'), encoding='latin1')
ValueError: too many values to unpack (expected 2)
sq1bmfud

sq1bmfud1#

martineau是主要的方法。pickle.load()返回一个字典,您需要对它做额外的工作来获取单词和嵌入。
你可以从

import pickle

fin = 'SGlove.pkl'

data_dict = pickle.load(open(fin, 'rb'), encoding='latin1')

单词列表由下式给出

word_list = list(data_dict.keys())

然后,您可以使用以下命令获得相应的嵌入列表

embedding_list = [data_dict[word] for word in word_list]

如果你需要一个所有单词的嵌入的二维数组,你需要使用np.concatenate或类似的方法来得到一个嵌入的数组,例如,如果你想让嵌入的形状是[n_words, len_vector](你看起来想要的),你可以使用

embeddings = np.concatenate([item[None, :] for item in embedding_list], axis=0)
3duebb1j

3duebb1j2#

我认为问题是pickle.load()返回了一个Python字典,这导致了ValueError
我用您提供的链接到的SGlove.pkl文件对此进行了测试,这个前提似乎是正确的,但是字典中似乎没有pickle.load()是与'embeddings'对应的返回值的键,因此这阻止了我进一步研究。
不管怎样,下面的代码大致展示了如何从load()返回的值中提取出两个你想要的值,请描述一下字典中对应于'enbeddings'键的是什么?

**注意:**我已经上传了字典中要返回的键的列表-这里是文本文件的link

import pickle

fin = 'SGlove.pkl'

data_dict = pickle.load(open(fin, 'rb'), encoding='latin1')

words = data_dict['woodi']
embeddings = data_dict['embeddings'] # -> KeyError: 'embeddings'
iih3973s

iih3973s3#

您也可以将其直接加载到pd Dataframe 中,如下所示:

data_fname = 'yourFile.pkl'
df = pd.read_pickle(data_fname)
df.shape

相关问题