首页 技术 正文
技术 2022年11月12日
0 收藏 308 点赞 4,867 浏览 1330 个字

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

 >>>b=np.mat(np.arange(10)).T
>>>b
matrix([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
>>>np.nonzero(b>2)
(array([3, 4, 5, 6, 7, 8, 9], dtype=int64),
array([0, 0, 0, 0, 0, 0, 0], dtype=int64))
>>>np.nonzero((b.A>2)*(b.A<8))
(array([3, 4, 5, 6, 7], dtype=int64), array([0, 0, 0, 0, 0], dtype=int64))
 >>> x = np.eye(3)
>>> x
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.nonzero(x)
(array([0, 1, 2]), array([0, 1, 2]))
>>>
>>> x[np.nonzero(x)]
array([ 1., 1., 1.])
>>> np.transpose(np.nonzero(x))
array([[0, 0],
[1, 1],
[2, 2]]

其中np.nonzero((b.A>2)*(b.A<8))是返回数组b的值在范围2<b<8的索引。并且必须要求参数时数组,如果是矩阵会报错。

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的参数的元素进行相乘,如果是个标量则返回值是标量

 >>> np.multiply(2.0, 4.0)
8.0
>>>
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.multiply(x1, x2)
array([[ 0., 1., 4.],
[ 0., 4., 10.],
[ 0., 7., 16.]])
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922