繁体   English   中英

创建具有 200k 唯一值的虚拟变量

[英]Creating Dummy Variable with 200k unique value

我正在尝试为分类数据集创建一个虚拟变量,但问题是 python 没有兼容的 ram 来运行代码,因为唯一值太大而无法创建虚拟变量。 它是一个包含 500k 行和 200k 唯一值的大型数据集。 是否可以创建一个具有 200k 唯一值的虚拟变量?

确实执行此操作需要大量 RAM。

至于我能想到的编程解决方案

  • 降维:如果您的 200K 类别之间存在某种关系,并且可以减少这些类别(例如,这些类别的层次结构级别,因此您可以对类别进行分组并按级别执行分析,例如 lvl1 = 10 类别,lvl2 = 100 等... )。 May I ask: what type of data do you have which contains 200K unique category values?
  • 拆分数据集并组合结果:我在下面使用 numpy。 您最终会得到更小的子集,每个子集都针对 200K 类别进行编码(即使某些类别不存在于子集中)。 Then you need to decide how to further process those subsets

不知何故,导入语句破坏了格式,所以我在这里将它们分开:

import numpy as np
import random

而rest的代码:

def np_one_hot_encode(n_categories: int, arr: np.array):
    # Performs one-hot encoding of arr based on n_categories
    # Allows encoding smaller chuncks of a bigger array
    # even if the chunks do not contain 1 occurrence of each category
    # while still producing n_categories columns for each chunks
    result = np.zeros((arr.size, n_categories))
    result[np.arange(arr.size), arr] = 1
    return result


# Testing our encoding function
# even if our input array doesn't contain all categories
# the output does cater for all categories
encoded = np_one_hot_encode(3, np.array([1, 0]))
print('test np_one_hot_encode\n', encoded)
assert np.array_equal(encoded, np.array([[0, 1, 0], [1, 0, 0]]))

# Generating 500K rows with 200K unique categories present at least once
total = int(5e5)
nunique = int(2e5)
uniques = list(range(0, nunique))
random.shuffle(uniques)
values = uniques+(uniques*2)[:total-nunique]
print('Rows count', len(values))
print('Uniques count', len(list(set(values))))

# Produces subsets of the data in (~500K/50 x nuniques) shape:
n_chunks = 50
for i, chunk in enumerate(np.array_split(values, n_chunks)):
    print('chunk', i, 'shape', chunk.shape)
    encoded = np_one_hot_encode(nunique, chunk)
    print('encoded', encoded.shape)

和 output:

test np_one_hot_encode
[[0. 1. 0.]
[1. 0. 0.]]
Rows count 500000
Uniques count 200000
chunk 0 shape (10000,)
encoded (10000, 200000)
chunk 1 shape (10000,)
encoded (10000, 200000)
  • 使用 Dask、Spark 等工具进行分布式处理……因此您可以处理子集的处理

  • 数据库:我能想到的其他解决方案是将您的 model 标准化为数据库(关系或“大”平面数据模型),您可以在其中利用索引仅过滤和处理部分数据(仅某些行和某些类别),因此允许您在 memory 中处理较小的 output

But in the end there is no magic, if ultimately you're tyring to load a NM matrix into memory with N=500K and M=200K, it will take the RAM it needs to take, there is no way around that ,因此最有可能获得的收益是降维或完全不同的数据处理方法(例如分布式计算)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM