简介
numpy - numeric python, 即数字化的python;python 支持的数值类型有整型、浮点型、布尔型、和复数型;numpy支持更多的数值类型:
1. `bool`:布尔类型,1 个字节,值为 True 或 False。
2. `int`:整数类型,通常为 int64 或 int32 。
3. `intc`:与 C 里的 int 相同,通常为 int32 或 int64。
4. `intp`:用于索引,通常为 int32 或 int64。
5. `int8`:字节(从 -128 到 127)
(tinyint 1字节 -2 ^7 ~ 2^7-1 (-128~127))
6. `int16`:整数(从 -32768 到 32767)
(smallint 2字节 -2 ^15 ~ 2^15-1 (-32768~32765))
7. `int32`:整数(从 -2147483648 到 2147483647)
(int 4字节 -2 ^31~ 2^31-1 (-2147483648~2147483647))
8. `int64`:整数(从 -9223372036854775808 到 9223372036854775807)
(bigint 8字节 -2 ^63 ~ 2^63-1)
9. `uint8`:无符号整数(从 0 到 255) unsigned
10. `uint16`:无符号整数(从 0 到 65535)
11. `uint32`:无符号整数(从 0 到 4294967295)
12. `uint64`:无符号整数(从 0 到 18446744073709551615)
13. `float`:float64 的简写。
14. `float16`:半精度浮点,5 位指数,10 位尾数
15. `float32`:单精度浮点,8 位指数,23 位尾数
16. `float64`:双精度浮点,11 位指数,52 位尾数
17. `complex`:complex128 的简写。
18. `complex64`:复数,由两个 32 位浮点表示。
19. `complex128`:复数,由两个 64 位浮点表示。
在 Numpy 中,上面提到的这些数值类型都被归于 dtype(data-type)
对象的实例。
我们可以用 numpy.dtype(object, align, copy)
来指定数值类型。而在数组里面,可以用 dtype=
参数。
Ndarray
numpy中最重要的一个形式叫数组ndarray - n个维度(dimension)的数组(array);
认识数组
ndarray 具有6个参数:shape、dtype、buffer、offset、strides(数据的步长)、order(以行或者列为主排列顺序)
ndarray数组的属性:
ndim: 数组的维度 nd1.ndim
shape: 形状(5, 4, 3) nd1.shape
size: 数组的总长度 nd1.size
dtype: 查看数据类型 nd1.dtype
创建数组的方法:
使用ndarray方法创建三维数组
nd1 = np.ndarray(shape=(5, 4, 3), dtype=np.int64)</