NumPy is the fundamental package for scientific computing with Python(科学计算). It contains among other things:
a powerful N-dimensional array object(功能强大的n维数组对象)
sophisticated (broadcasting) functions
tools for integrating C/C++ and Fortran code
useful linear algebra, Fourier transform, and random number
capabilities(现象代数、傅里叶变换、随机数)
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases
255表示白色
0 表示黑色
import numpy as np
a = np.zeros(5)
a
Out[6]: array([0., 0., 0., 0., 0.])
在默认的情况下,zeros创建的数组元素类型是浮点型的,如果要使用其他类型,可以设置dtype参数进行声明
``
a = np.zeros(5,int)
a
Out[8]: array([0, 0, 0, 0, 0])
a = np.ones([2, 3])
a
Out[11]:
array([[1., 1., 1.],
[1., 1., 1.]])
a = np.zeros([4, 4, 3], np.uint8)
a
Out[15]:
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]], dtype=uint8)
``
liyongling