AIR*_*AIR 5 python numpy pandas word2vec
我需要保存一个带有两列词嵌入 (Word2Vec) 的 Pandas 数据框,这些词嵌入存储为 ndarrays 的 dim (1300, 300)、一个字符串和另一个具有该字符串的一个热表示的数组。
TYPE content title one_hot_label
------------------------------------------------------------
happy [[-0.25195312, 0.13085938, 0.05053711, -0.0417... [[0.12792969, -0.055908203, 0.011230469, 0.283... [0, 1, 0]
sad [[-0.25195312, 0.13085938, 0.05053711, -0.0417... [[0.12792969, -0.055908203, 0.011230469, 0.283... [0, 1, 0]
happy [[-0.25195312, 0.13085938, 0.05053711, -0.0417... [[0.12792969,-0.055908203, 0.011230469, 0.283... [0, 1, 0]
sad [[-0.25195312, 0.13085938, 0.05053711, -0.0417... [[0.12792969, -0.055908203, 0.011230469, 0.283... [0, 1, 0]
...
...
...
Run Code Online (Sandbox Code Playgroud)
我需要将它保存在我的驱动器中。我尝试将它 ( df.to_picke
)序列化并且只要条目数量很少就可以很好地工作。CSV ( df.to_csv
) 将省略号添加到 Numpy 数组列并to_hdf
给我溢出错误。
有没有办法用这种结构保存大型数据集?
编辑
打电话df.memory_usage(deep=True)
给我:
Index 23840
type 244425
content 5447697600
title 62976000
one_hot_label 309920
dtype: int64
编辑 2
你能给我另一种结构来创建这个嵌入数据集吗?
谢谢
小智 -1
您可以使用此功能来减少数据大小。
def reduce_mem_usage(self, df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[: 3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df.loc[:, col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df.loc[:, col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df.loc[:, col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df.loc[:, col] = df[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df.loc[:, col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df.loc[:, col] = df[col].astype(np.float32)
else:
df.loc[:, col] = df[col].astype(np.float64)
end_mem = df.memory_usage().sum() / 1024**2
if verbose:
print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))
return df
Run Code Online (Sandbox Code Playgroud)