【发布时间】:2021-04-21 09:25:42
【问题描述】:
我正在尝试为分类数据集创建一个虚拟变量,但问题是 python 没有兼容的 ram 来运行代码,因为唯一值太大而无法创建虚拟变量。它是一个包含 500k 行和 200k 唯一值的大型数据集。是否可以创建一个具有 200k 唯一值的虚拟变量?
【问题讨论】:
标签: python jupyter-notebook data-science
我正在尝试为分类数据集创建一个虚拟变量,但问题是 python 没有兼容的 ram 来运行代码,因为唯一值太大而无法创建虚拟变量。它是一个包含 500k 行和 200k 唯一值的大型数据集。是否可以创建一个具有 200k 唯一值的虚拟变量?
【问题讨论】:
标签: python jupyter-notebook data-science
确实执行此操作需要大量 RAM。
就我能想到的编程解决方案而言:
May I ask: what type of data do you have which contains 200K unique category values?
Then you need to decide how to further process those subsets。不知何故,导入语句破坏了格式,所以我在这里将它们分开:
import numpy as np
import random
以及其余的代码:
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)
还有输出:
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 等工具...这样您就可以处理子集
数据库:我能想到的其他解决方案是将您的模型标准化为数据库(关系或“大”平面数据模型),您可以在其中利用索引来过滤和处理部分数据(仅某些行和某些类别),从而允许您在内存中处理较小的输出
But in the end there is no magic, if ultimately you're tyring to load a N-M matrix into memory with N=500K and M=200K, it will take the RAM it needs to take, there is no way around that,因此最有可能获得的收益是降维或完全不同的数据处理方法(例如分布式计算)。
【讨论】:
string type of data: 能不能详细点?