gpt4 book ai didi

python - 在 Numpy/Python 中快速稀疏矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:40 25 4
gpt4 key购买 nike

我需要(快速)稀疏矩阵。

Rarefaction - 将丰度矩阵转换为均匀的采样深度。

在这个例子中,每一行都是一个样本,采样深度是行的总和。我想通过 min(rowsums(matrix)) 样本随机采样(替换)矩阵。

假设我有一个矩阵:

>>> m = [ [0, 9, 0],
... [0, 3, 3],
... [0, 4, 4] ]

稀疏函数逐行随机抽样并替换 min(rowsums(matrix)) 次(在本例中为 6)。

>>> rf = rarefaction(m)
>>> rf
[ [0, 6, 0], # sum = 6
[0, 3, 3], # sum = 6
[0, 3, 3] ] # sum = 6

结果是随机的,但行总和始终相同。

>>> rf = rarefaction(m)
>>> rf
[ [0, 6, 0], # sum = 6
[0, 2, 4], # sum = 6
[0, 4, 2], ] # sum = 6

PyCogent有一个逐行执行此操作的函数,但它在大型矩阵上非常慢。

我感觉 Numpy 中有一个函数可以做到这一点,但我不确定它会被称为什么。

最佳答案

import numpy as np
from numpy.random import RandomState

def rarefaction(M, seed=0):
prng = RandomState(seed) # reproducible results
noccur = np.sum(M, axis=1) # number of occurrences for each sample
nvar = M.shape[1] # number of variables
depth = np.min(noccur) # sampling depth

Mrarefied = np.empty_like(M)
for i in range(M.shape[0]): # for each sample
p = M[i] / float(noccur[i]) # relative frequency / probability
choice = prng.choice(nvar, depth, p=p)
Mrarefied[i] = np.bincount(choice, minlength=nvar)

return Mrarefied

例子:

>>> M = np.array([[0, 9, 0], [0, 3, 3], [0, 4, 4]])
>>> M
array([[0, 9, 0],
[0, 3, 3],
[0, 4, 4]])
>>> rarefaction(M)
array([[0, 6, 0],
[0, 2, 4],
[0, 3, 3]])
>>> rarefaction(M, seed=1)
array([[0, 6, 0],
[0, 4, 2],
[0, 3, 3]])
>>> rarefaction(M, seed=2)
array([[0, 6, 0],
[0, 3, 3],
[0, 3, 3]])

干杯,大卫

关于python - 在 Numpy/Python 中快速稀疏矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com