您的主要问题似乎是二值化y
不适合您的记忆。您可以使用稀疏数组来避免这种情况。
>>> import numpy as np
>>> from scipy.sparse import csc_matrix
>>> y = np.random.randint(0, 10000, size=5000000) # 5M random integers [0,10K)
您可以将这些标签y
转换为5M x 10K
稀疏矩阵,如下所示:
>>> dtype = np.uint8 # change to np.bool if you want boolean or other data type
>>> rows = np.arange(y.size) # each of the elements of `y` is a row itself
>>> cols = y # `y` indicates the column that is going to be flagged
>>> data = np.ones(y.size, dtype=dtype) # Set to `1` each (row,column) pair
>>> ynew = csc_matrix((data, (rows, cols)), shape=(y.size, y.max()+1), dtype=dtype)
ynew
然后是一个稀疏矩阵,其中每一行都充满了零,除了一个条目:
>>> ynew
<5000000x10000 sparse matrix of type '<type 'numpy.uint8'>'
with 5000000 stored elements in Compressed Sparse Column format>
您将不得不调整您的代码以学习如何处理稀疏矩阵,但这可能是您拥有的最佳选择。此外,您可以从稀疏矩阵中恢复完整的行或列:
>>> row0 = ynew[0].toarray() # row0 is a standard numpy array
对于字符串标签或任意数据类型的标签:
>>> y = ['aaa' + str(i) for i in np.random.randint(0, 10000, size=5000000)] # e.g. 'aaa9937'
首先提取从标签到整数的映射:
>>> labels = np.unique(y) # List of unique labels
>>> mapping = {u:i for i,u in enumerate(labels)}
>>> inv_mapping = {i:u for i,u in enumerate(labels)} # Only needed if you want to recover original labels at some point
上面mapping
将每个标签映射到一个整数(基于它们存储在唯一集合中的顺序labels
)。
然后再次创建稀疏矩阵:
>>> N, M = len(y), labels.size
>>> dtype = np.uint8 # change np.bool if you want boolean
>>> rows = np.arange(N)
>>> cols = [mapping[i] for i in y]
>>> data = np.ones(N, dtype=dtype)
>>> ynew = csc_matrix((data, (rows, cols)), shape=(N, M), dtype=dtype)
如果将来您想知道label X
映射到哪些原始标签,您可以创建(尽管不是必需的)逆映射:
>>> inv_mapping = {i:u for i,u in enumerate(labels)}
>>> inv_mapping[10] # ---> something like 'aaaXXX'