我正在使用Python、numpy和scikit-learn。我有存储在 SQL 表中的键和值数据。我将其检索为元组列表,返回为:[(id, value),...]
。每个 id 在列表中仅出现一次,并且元组按 id 升序排列。这个过程完成了几次,因此我有多个key: value
对列表。这样:
dataset = []
for sample in samples:
listOfTuplePairs = getDataFromSQL(sample) # get a [(id, value),...] list
dataset.append(listOfTuplePairs)
key 可能会在不同的样本中重复,并且每行的长度可能不同。示例数据集
可能是:
dataset = [[(1, 0.13), (2, 2.05)],
[(2, 0.23), (4, 7.35), (5, 5.60)],
[(2, 0.61), (3, 4.45)]]
可以看出,每一行都是一个样本,并且一些id(本例中为2)出现在多个样本中。
问题:我希望构建一个适合使用 scikit-learn 处理的单个(可能稀疏)numpy 数组。与每个样本的特定键 (id) 相关的值应在同一“列”中对齐(如果这是正确的术语),以便上述示例的矩阵如下所示:
ids = 1 2 3 4 5
------------------------------
dataset = [(0.13, 2.05, null, null, null),
(null, 0.23, null, 7.35, 5.60),
(null, 0.61, 4.45, null, null)]
如您所见,我还希望从矩阵中删除 id(尽管我需要保留它们的列表,以便我知道矩阵中的值与什么相关。键的每个初始列表:值
对可能包含数千行,并且可能有数千个样本,因此生成的矩阵可能非常大。请提供考虑速度(在Python限制内)、内存效率和代码清晰度的答案。
非常非常感谢您的帮助。
最佳答案
这是一种基于 NumPy 的方法来创建稀疏矩阵 coo_matrix
重点关注内存效率 -
from scipy.sparse import coo_matrix
# Construct row IDs
lens = np.array([len(item) for item in dataset])
shifts_arr = np.zeros(lens.sum(),dtype=int)
shifts_arr[lens[:-1].cumsum()] = 1
row = shifts_arr.cumsum()
# Extract values from dataset into a NumPy array
arr = np.concatenate(dataset)
# Get the unique column IDs to be used for col-indexing into output array
col = np.unique(arr[:,0],return_inverse=True)[1]
# Determine the output shape
out_shp = (row.max()+1,col.max()+1)
# Finally create a sparse marix with the row,col indices and col-2 of arr
sp_out = coo_matrix((arr[:,1],(row,col)), shape=out_shp)
请注意,如果 ID
应该是输出数组中的列号,您可以将使用 np.unique
替换为我们提供此类唯一 ID像这样的东西-
col = (arr[:,0]-1).astype(int)
这应该会给我们带来良好的性能提升!
示例运行 -
In [264]: dataset = [[(1, 0.13), (2, 2.05)],
...: [(2, 0.23), (4, 7.35), (5, 5.60)],
...: [(2, 0.61), (3, 4.45)]]
In [265]: sp_out.todense() # Using .todense() to show output
Out[265]:
matrix([[ 0.13, 2.05, 0. , 0. , 0. ],
[ 0. , 0.23, 0. , 7.35, 5.6 ],
[ 0. , 0.61, 4.45, 0. , 0. ]])
关于python - 如何从多个向量构造 numpy 数组,其中数据按 id 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38415354/