python矩阵变化_Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法

这篇博客介绍了NumPy库中的nonzero()函数和multiply()函数的使用。nonzero()用于找到数组中非零或布尔条件为真的元素索引,而multiply()则实现两个数组或矩阵元素间的逐个相乘。通过示例展示了这两个函数在处理数组和矩阵时的应用,例如在寻找特定条件的元素和执行元素级乘法操作。这些功能对于数据分析和矩阵运算至关重要。
摘要由CSDN通过智能技术生成

1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引,其中,数值0为False,其余的都为True。

1 >>>b=np.mat(np.arange(10)).T2 >>>b3 matrix([[0],4 [1],5 [2],6 [3],7 [4],8 [5],9 [6],10 [7],11 [8],12 [9]])13 >>>np.nonzero(b>2)14 (array([3, 4, 5, 6, 7, 8, 9], dtype=int64),15 array([0, 0, 0, 0, 0, 0, 0], dtype=int64))16 >>>np.nonzero((b.A>2)*(b.A<8))17 (array([3, 4, 5, 6, 7], dtype=int64), array([0, 0, 0, 0, 0], dtype=int64))

1 >>> x = np.eye(3)2 >>>x3 array([[ 1., 0., 0.],4 [ 0., 1., 0.],5 [ 0., 0., 1.]])6 >>>np.nonzero(x)7 (array([0, 1, 2]), array([0, 1, 2]))8 >>>

9 >>>x[np.nonzero(x)]10 array([ 1., 1., 1.])11 >>>np.transpose(np.nonzero(x))12 array([[0, 0],13 [1, 1],14 [2, 2]]

其中np.nonzero((b.A>2)*(b.A<8))是返回数组b的值在范围2

A common use for nonzero is to find the indices of an array, where a condition is True. Given an array a, the condition a > 3 is a boolean array and since False is interpreted as 0, np.nonzero(a > 3) yields the indices of the a where the condition is true.这个功能和numpy.where()的一种用法一样。

>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])

>>> a > 3

array([[False, False, False],

[ True, True, True],

[ True, True, True]], dtype=bool)

>>> np.nonzero(a > 3)

(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))

2numpy.multiply(x1,x2)

对参数的元素进行相乘

参数:

x1, x2 : array,matrix

数组按位置相乘

返回值:

y : ndarray

对x1和x2的参数的元素进行相乘,如果是个标量则返回值是标量

1 >>> np.multiply(2.0, 4.0)2 8.0

3 >>>

4 >>> x1 = np.arange(9.0).reshape((3, 3))5 >>> x2 = np.arange(3.0)6 >>>np.multiply(x1, x2)7 array([[ 0., 1., 4.],8 [ 0., 4., 10.],9 [ 0., 7., 16.]])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值