Numpy 初步、ndarray、创建数组
【目录】
-  简介
-  ndarray介绍
-  ndarray用法
-  ndarray基本操作
一、numpy简介
Numpy(Numerical Python)是一个开源的Python科学计算库,用于快速处理任意维度的数组。
Numpy支持常见的数组和矩阵操作。对于同样的数值计算任务,使用Numpy比直接使用Python要简洁的多。
Numpy使用ndarray对象来处理多维数组,该对象是一个快速而灵活的大数据容器。
二、ndarray介绍
NumPy provides an N-dimensional array type, the ndarray,
which describes a collection of “items” of the same type.ndarray == n-dimensional array n维数组
2.1 为什么需要ndarray?
由于python对数据类型不敏感,及python的数据类型一般是Any。
这会导致原生python处理数据时会增加处理开销,类比C/C++中的链表。
处理统一数据类型的数据时,我们往往希望数据的存储及运算是有规律。
2.2 内存块风格

- ndarray 就是C/C++中的数组,数据类型统一,存储空间地址连续
- Python list 则是C/C++中的链表,数据类型不一定统一,寻址方式通过指针连接。
- numpy内置了并行运算功能,当系统有多个核心时,做某种计算时,numpy会自动做并行计算
- Numpy底层使用C语言编写,内部解除了GIL(全局解释器锁),其对数组的操作速度不受Python解释器的限制,其效率远高于纯Python代码。
三、ndarray 用法
3.1 ndarray 属性
| 属性名字 | 解释 | 
|---|---|
| ndarray.shape | 数组的维度形状 | 
| ndarray.ndim | 数组的维数 | 
| ndarray.size | 数组中的元素数量 | 
| ndarray.itemsize | 一个数组元素的长度(字节) | 
| ndarray.dtype | 数组元素的类型 | 
3.2 ndarray的数据类型
a = np.zeros((3, 4), dtype='i2')	# dtype=int16
b = np.zeros((3, 4), dtype=bool)	# dtype=bool
| 名称 | 描述 | 简写 | 
|---|---|---|
| np.bool | 用一个字节存储的布尔类型(True或False) | ‘b’ | 
| np.int8 | 一个字节大小,-128 至 127 | ‘i’ | 
| np.int16 | 整数,-32768 至 32767 | ‘i2’ | 
| np.int32 | 整数,-2^31 至 2^32 -1 | ‘i4’ | 
| np.int64 | 整数,-2^63 至 2^63 - 1 | ‘i8’ | 
| np.uint8 | 无符号整数,0 至 255 | ‘u’ | 
| np.uint16 | 无符号整数,0 至 65535 | ‘u2’ | 
| np.uint32 | 无符号整数,0 至 2^32 - 1 | ‘u4’ | 
| np.uint64 | 无符号整数,0 至 2^64 - 1 | ‘u8’ | 
| np.float16 | 半精度浮点数:16位,正负号1位,指数5位,精度10位 | ‘f2’ | 
| np.float32 | 单精度浮点数:32位,正负号1位,指数8位,精度23位 | ‘f4’ | 
| np.float64 | 双精度浮点数:64位,正负号1位,指数11位,精度52位 | ‘f8’ | 
| np.complex64 | 复数,分别用两个32位浮点数表示实部和虚部 | ‘c8’ | 
| np.complex128 | 复数,分别用两个64位浮点数表示实部和虚部 | ‘c16’ | 
| np.object_ | python对象 | ‘O’ | 
| np.string_ | 字符串 | ‘S’ | 
| np.unicode_ | unicode类型 | ‘U’ | 
- 若不指定,整数默认int64,小数默认float64
四、ndarray 基本操作
4.1 生成数组的方法
- np.zeros(shape, dtype)
- np.ones(shape, dtype)
- np.zeros_like(a, dtype)
- np.ones_like(a, dtype)
- a 为某一数组array或者列表的shape
zeros = np.zeros((3, 4), dtype=int)
"""
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
"""
4.2 从现有数组生成
- np.array(object, dtype)
- np.asarray(a, dtype)
- asarray直接复制内存地址,与原有数组共用一个内存
a = np.array([[1,2,3],[4,5,6]])
# 从现有的数组当中创建
a1 = np.array(a)
# 相当于索引的形式,并没有真正的创建一个新的
a2 = np.asarray(a)
print(id(a))	# 2390986778320
print(id(a1))	# 2390986784464
print(id(a2))	# 2390986778320
4.3 生成固定范围的数组
- np.linspace()
- np.arrange()
- np.logspace()
4.3.1 np.linspace(start, stop, num, endpoint, dtype)
创建等差数组 — 指定数据数量
- 参数: 
  - start: 序列的起始值
- stop: 序列的终止值
- num: 要生成的等间隔样例数量,默认为50
- endpoint: 序列中是否包含stop值,默认为ture
 
# 生成等间隔的数组
np.linspace(0, 100, 11)
# 返回结果
array([  0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.])
4.3.2 np.arange(start, stop, step, dtype)
创建等差数组 — 指定步长
- 参数 
  - step: 步长,默认值为1
 
# 生成等差数组
np.arange(10, 50, 2)
# 返回结果
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
       44, 46, 48])
4.3.3 np.logspace(start, stop, num, base, dtype)
创建等比数组 — 指定底数base
- 参数: 
  - num: 要生成的等比数列数量,默认为50
- base: 生成底数为base的等比数组,默认底数是10
 
# 生成底数为3的等比数组
np.logspace(0, 2, 3, base=3, dtype=int)
# 返回结果
array([1 3 9])



















