更快的 3D 矩阵运算 - Python

数据挖掘 Python 麻木的 scipy 矩阵
2022-03-03 00:46:44

我正在使用 Python 中的 3D 矩阵,例如,给定这样的矩阵,大小为 2x3x4:

[[[1 2 1 4]
  [3 2 1 1]
  [4 3 1 4]]

 [[2 1 3 3]
  [1 4 2 1]
  [3 2 3 3]]]

我的任务是在每个维度矩阵的每一行中找到熵值。例如,在上面矩阵的第 1 维的第 1 行中[1,2,1,4],归一化值(因此总和为 1)是[0.125, 0.25, 0.125, 0.5],熵的值由公式计算,-sum(i*log(i))其中 i 是归一化值。生成的矩阵是一个 2x3 矩阵,其中每个维度有 3 个熵值(因为有 3 行)。

这是我每次使用随机矩阵的代码的工作示例:

from scipy.stats import entropy
import numpy as np

matrix = np.random.randint(low=1,high=5,size=(2,3,4)) #how if size is (200,50,1000)
entropy_matrix=np.zeros((matrix.shape[0],matrix.shape[1]))
for i in range(matrix.shape[0]):
    normalized = np.array([float(k)/np.sum(j) for j in matrix[i] for k in j]).reshape(matrix.shape[1],matrix.shape[2])
    entropy_matrix[i] = np.array([entropy(m) for m in normalized])

我的问题是如何扩大这个程序以使用非常大的 3D 矩阵(例如大小为 200x50x1000)?

我在 Windows 10 中使用 Python(带有 Anaconda 发行版)。使用 200x50x1000 的 3D 矩阵大小,我的计算机上的运行时间为 290 秒。

1个回答

如果您使用 numpy 的内置函数(而不是自己重新实现它们)会更快:

import numpy as np
from scipy.stats import entropy

np.apply_along_axis(func1d=entropy, axis=2, arr=matrix)