这很简单,并不是你需要一个库来做的事情。
纯 Python 解决方案
features = ['a', 'b', 'c', 'd', 'e', 'f']
features_lookup = dict(map(reversed, enumerate(features)))
s1 = ['a', 'b', 'c']
s2 = ['a', 'c', 'f']
def create_feature_vector(sample, lookup):
vec = [0]*len(lookup)
for value in sample:
vec[lookup[value]] = 1
return vec
输出:
>>> create_feature_vector(s1, features_lookup)
[1, 1, 1, 0, 0, 0]
>>> create_feature_vector(s2, features_lookup)
[1, 0, 1, 0, 0, 1]
单个特征向量的 Numpy 替代方案
如果您碰巧已经在使用 numpy,如果您的功能集很大,这将更加更有效率:
import numpy as np
features = np.array(['a', 'b', 'c', 'd', 'e', 'f'])
sample_size = 3
def feature_sample_and_vector(sample_size, features):
n = features.size
sample_indices = np.random.choice(range(n), sample_size, replace=False)
sample = features[sample_indices]
vector = np.zeros(n, dtype="uint8")
vector[sample_indices] = 1
return sample, vector
大量样本及其特征向量的 Numpy 替代方案
使用 numpy 可以很好地扩展大型特征集和/或大型样本集。请注意,这种方法会产生重复的样本:
import random
import numpy as np
# Assumes features is already a numpy array
def generate_samples(features, num_samples, sample_size):
n = features.size
vectors = np.zeros((num_samples, n), dtype="uint8")
idxs = [random.sample(range(n), k=sample_size) for _ in range(num_samples)]
cols = np.sort(np.array(idxs), axis=1) # You can remove the sort if having the features in order isn't important
rows = np.repeat(np.arange(num_samples).reshape(-1, 1), sample_size, axis=1)
vectors[rows, cols] = 1
samples = features[cols]
return samples, vectors
演示:
>>> generate_samples(features, 10, 3)
(array([['d', 'e', 'f'],
['a', 'b', 'c'],
['c', 'd', 'e'],
['c', 'd', 'f'],
['a', 'b', 'f'],
['a', 'e', 'f'],
['c', 'd', 'f'],
['b', 'e', 'f'],
['b', 'd', 'f'],
['a', 'c', 'e']], dtype='<U1'),
array([[0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1],
[0, 0, 1, 1, 0, 1],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0]], dtype=uint8))
一个非常简单的时序基准测试,从 26 个特征的特征集中对 100,000 个大小为 12 的样本:
In [2]: features = np.array(list("abcdefghijklmnopqrstuvwxyz"))
In [3]: num_samples = 100000
In [4]: sample_size = 12
In [5]: %timeit generate_samples(features, num_samples, sample_size)
645 ms ± 9.86 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
唯一真正的瓶颈是生成索引所需的列表理解。不幸的是,没有使用np.random.choice() 来生成不替换样本的二维变体,因此您仍然不得不求助于一种相对较慢的方法来生成随机样本索引。