numpy篇

作者: 逃淘桃 | 来源:发表于2018-06-12 15:32 被阅读0次

Numpy


2018-05-21

  1. numpy.prob:
    numpy.prob(a, axis=None, dtype=None, out=None, keepdims=<class 'numpy._globals._NoValue'>)
    返回给定轴上的数组元素的乘积。
    Notes:
    当使用整数类型时,算术是模块化的,在溢出时不会产生错误,如:
x = np.array([536870910, 536870910, 536870910, 536870910])
np.prod(x)
--->>16

空阵列的prod是元素1:

np.prod([])
--->>1.0

Examples:
默认情况下,计算所有的元素的乘积:

import numpy as np
print(np.prod([1., 2.]))
--->>2.0

即使输入数组是二维的:

a = np.prod([[1., 2.], [3., 4.]])
print(a)
--->>24.0

但是我们也可以指定要乘以的轴:

b = np.prod([[1., 2.], [3., 4.]], axis=1)
print(b)
--->>array([2., 12.])

如果x的类型是无符号的,那么输出类型是无符号整数:

x = np.array([1, 2, 3], dtype=np.uint8)
print(np.prod(x).dtype == np.uint)
--->>True

如果X是有符号整数类型,那么输出类型是默认的有符号整数:

x = np.array([1, 2, 3], dtype=np.int8)
print(np.prod(x).dtype == int)
--->>True

2018-05-23

numpy

1.numpy的多维数组切片
例如:

import numpy as np
a = np.arange(36)
print(a)
--->>
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35]
b = a.reshape(6, 6)
print(b)
--->>
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]]
c = b[0, 1:4]
print(c)
--->>
[1 2 3]
d = b[2:4, 1:4]
print(d)
--->>
[[13 14 15]
 [19 20 21]]
e = b[:, 1:4]
print(e)
--->>
[[ 1  2  3]
 [ 7  8  9]
 [13 14 15]
 [19 20 21]
 [25 26 27]
 [31 32 33]]

从例子中我们可以得到结论,对于多维数组,[a:b,c:d,e:f,....],其中第一个逗号前表示在第一维的下标的取值范围,第二个逗号前的表示在第二维方向上的取值范围,以此类推。


2018-05-24

numpy

1.numpy.where(cond, x, y)

  • 三个参数np.where(cond,x,y):满足条件(cond)输出x, 不满足输出y.
  • 一个参数 np.where(arry):输出arry中‘真’值坐标
    np.where(arry,x,y)
  • np.where(arry):如果没有标明xy值,则返回坐标值。
    np.where(array)

2018-05-31

1.arange()函数

  • 函数说明:arange([start,] stop [, step,], dtype=None),根据start与stop指定的范围以及step设定的步长,生成一个ndarray。
    2.reshape()函数
  • 函数说明:给数组一个新的形状而不改变其数据。`numpy.reshape(a, newshape, order='C')

相关文章

网友评论

      本文标题:numpy篇

      本文链接:https://www.haomeiwen.com/subject/bfxreftx.html