Python:大数据的单热编码
作者:互联网
尝试将字符串标签编码为单热编码时,我不断遇到内存问题.大约有500万行和大约10000种不同的标签.我尝试过以下内容但仍然遇到内存错误:
from sklearn import preprocessing
lb = preprocessing.LabelBinarizer()
label_fitter = lb.fit(y)
y = label_fitter.transform(y)
我也尝试过这样的事情:
import numpy as np
def one_hot_encoding(y):
unique_values = set(y)
label_length = len(unique_values)
enu_uniq = zip(unique_values , range(len(unique_values)))
dict1 = dict(enu_uniq)
values = []
for i in y:
temp = np.zeros((label_length,), dtype="float32")
if i in dict1:
temp[dict1[i]] = 1.0
values.append(temp)
return np.array(values)
仍然得到记忆错误.有提示吗?有些人在堆栈中问同样的问题,但没有答案似乎有点有用.
解决方法:
你的主要问题似乎是二进制化的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
上面的映射将每个标签映射到一个整数(基于它们存储在唯一集标签中的顺序).
然后再次创建稀疏矩阵:
>>> 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)
如果将来您想知道原始标签映射到的标签X,您可以创建(尽管不需要)逆映射:
>>> inv_mapping = {i:u for i,u in enumerate(labels)}
>>> inv_mapping[10] # ---> something like 'aaaXXX'
标签:python,one-hot-encoding 来源: https://codeday.me/bug/20190623/1266716.html