为什么sklearn提供的一键编码器这么慢?
hana::tuple
输出:
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(sparse=False, handle_unknown='ignore')
categories = np.expand_dims(np.array(list(map(chr, np.arange(97,123)))), axis=1)
enc.fit(categories)
vec = np.expand_dims(list(map(chr, np.random.randint(97,123,1000))), axis=1)
%timeit enc.transform(np.array([['b']]))
%timeit (categories == 'b')[:,0] *1
比幼稚的方法慢100倍,而实际上通过使用569 µs ± 3.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
3.23 µs ± 58.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
的元素是唯一的知识可能会更快。