python向量化计算_python – 在Numpy中向量化特征值计算

编辑:如果您使用的是numpy> = 1.8.0的版本,那么np.linalg.eigvals会在您处理的任何数组的最后两个维度上运行,因此如果您将输入重新整形为(n_subarrays,nrows, ncols)数组你只需要调用一次eigvals:

import numpy as np

A = np.array([[0, 1, 0],

[0, 2, 0],

[0, 0, 3]])

# the input needs to be an array, since matrices can only be 2D.

B = np.repeat(A[np.newaxis,...], 4, 0)

# for arbitrary input arrays you could do something like:

# B = np.vstack(a[np.newaxis,...] for a in input_arrays)

# but for this to work it will be necessary for each element in

# 'input_arrays' to have the same shape

# eigvals will operate over the last two dimensions of the array and return

# a (4, 3) array of eigenvalues

C = np.linalg.eigvals(B)

# reshape this output so that it matches your original example

C.shape = (2, 2, 3)

如果您的输入数组并非都具有相同的尺寸,例如input_arrays [0] .shape ==(2,2),input_arrays [1] .shape ==(3,3)等等,那么你只能在具有匹配尺寸的子集上对该计算进行矢量化.

如果您使用的是较旧版本的numpy,那么遗憾的是我认为没有任何方法可以在多个输入数组上对特征值的计算进行矢量化 – 您只需要在Python中循环输入.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值