gpt4 book ai didi

python - 用 2d 矩阵填充 4D 矩阵

转载 作者:行者123 更新时间:2023-11-30 22:13:59 26 4
gpt4 key购买 nike

我想知道是否有一个 numpy 函数可以使它更快。这是我正在尝试做的一个示例。

def _sparse_4D_rand_mat(self, x, y, density):
_4D_mat = np.empty((x, y, x, y))
for i in range(self.size):
for j in range(self.size):
_4D_mat[:,i,j,:] = self._rand_sparse(x, y, density)
return _4D_mat
def _rand_sparse(self, m, n, density, format='csr'):
nnz = max(min(int(m * n * density), m * n), 0)
row = np.random.random_integers(low=0, high=m - 1, size=nnz)
col = np.random.random_integers(low=0, high=n - 1, size=nnz)
data = np.ones(nnz, dtype=float)
data = np.random.dirichlet(data)
return csr_matrix((data, (row, col)), shape=(m, n)).toarray()

感谢您的贡献。我是新人;)

最佳答案

由于密度保持不变,您可以调用一次_rand_sparse来生成一个大的稀疏二维数组,而不是多次调用_rand_sparse来生成许多小的稀疏二维数组,然后使用 reshape 方法将 2D 结果 reshape 为 4D 数组:

_4D_mat = _rand_sparse(x * y * x, y, density)
_4D_mat = _4D_mat.reshape((x, y, x, y))

例如,

import numpy as np
import scipy.sparse as sparse

def _rand_sparse(m, n, density, format='csr'):
nnz = max(min(int(m * n * density), m * n), 0)
# use randint since random_integer is deprecated in NumPy 1.11.0
row = np.random.randint(low=0, high=m, size=nnz)
col = np.random.randint(low=0, high=n, size=nnz)
data = np.ones(nnz, dtype=float)
data = np.random.dirichlet(data)
return sparse.csr_matrix((data, (row, col)), shape=(m, n)).toarray()

def orig(x, y, density):
_4D_mat = np.empty((x, y, x, y))
for i in range(y):
for j in range(x):
_4D_mat[:, i, j, :] = _rand_sparse(x, y, density)
return _4D_mat

def alt(x, y, density):
_4D_mat = _rand_sparse(x * y * x, y, density)
_4D_mat = _4D_mat.reshape((x, y, x, y))
return _4D_mat

x, y, density = 2, 4, 0.5

由于 alt 消除了双 for 循环,因此该解决方案将比 orig 快得多,因为 x 的值>y 变大(即,随着 for 循环中的迭代次数增加)。事实上,即使对于上面使用的小值,alt 也已经比 orig 快(几乎 8 倍):

In [108]: %timeit orig(x, y, density)
100 loops, best of 3: 2.24 ms per loop

In [109]: %timeit alt(x, y, density)
1000 loops, best of 3: 281 µs per loop
<小时/>

I need the sum for each 2D array in the 4D array to be 1

要规范化适当的切片,您可以使用:

totals = np.nansum(_4D_mat, axis=0, keepdims=True)
totals = np.nansum(totals, axis=3, keepdims=True)
_4D_mat /= totals

关于python - 用 2d 矩阵填充 4D 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50628902/

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