python学习numpy(1)
numpy
ndarray
#参数
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
object 数组
dtype 数组元素的数据类型
copy 是否可复制
order 创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)
subok 默认返回一个与基类类型一致的数组
ndmin 指定最小的数据维度
实例
#一维数组
import numpy as np
a = np.array([1,2,3])
#二维数组
a = np.array([[1, 2], [3, 4]])
#指定最小维度
a = np.array([1, 2, 3, 4, 5], ndmin = 2)
output:[[1 2 3 4 5]]
#指定dtype参数
a = np.array([1, 2, 3], dtype = complex)
output:[1.+0.j 2.+0.j 3.+0.j]
numpy数据类型
numpy的数据类型是dtype类的实例
dtype对象实例的构造方式
numpy.dtype(object, align, copy)
object 要转换的类型对象
align 填充内容,如果为true则填充为类似结构体
copy
创建实例
dt = np.dtype(np.int32)
# int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
dt = np.dtype('i4')
# <>表示大端还是小端
dt = np.dtype('<i4')
创建类型字段
# 创建字段名和对应的类型
dt = np.dtype([('age',np.int8)])
# 将创建的类型应用于ndarray
a = np.array([(10,),(20,),(30,)], dtype = dt)
# 可通过字段名存取数据
print(a['age'])
根据字段创建数据
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
print(a)
numpy数据类型
bool_ 布尔型数据类型(True 或者 False)
int_ 默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc 与 C 的 int 类型一样,一般是 int32 或 int 64
intp 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8 字节(-128 to 127)
int16 整数(-32768 to 32767)
int32 整数(-2147483648 to 2147483647)
int64 整数(-9223372036854775808 to 9223372036854775807)
uint8 无符号整数(0 to 255)
uint16 无符号整数(0 to 65535)
uint32 无符号整数(0 to 4294967295)
uint64 无符号整数(0 to 18446744073709551615)
float_ float64 类型的简写
float16 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_ complex128 类型的简写,即 128 位复数
complex64 复数,表示双 32 位浮点数(实数部分和虚数部分)
complex128 复数,表示双 64 位浮点数(实数部分和虚数部分)
NumPy 数组属性
numpy数组的维度称为数组的秩
NumPy 的数组中比较重要 ndarray 对象属性有:
ndarray.ndim 秩,即轴的数量或维度的数量
#返回数组的秩
a = np.arange(24)
print (a.ndim) # a 现只有一个维度
# 现在调整其大小
b = a.reshape(2,4,3) # b 现在拥有三个维度
print (b.ndim)
output:
1
3
ndarray.shape 表示数组的维度
a = np.array([[1,2,3],[4,5,6]])
print (a.shape)
output:
(2, 3)
shape 调整数组大小
rshape 也可以完成调解数组大小
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print (a)
output:
[[1 2]
[3 4]
[5 6]]
创建数组
numpy.empty 创建指定形状且未初始化的数组
numpy.empty(shape, dtype = float, order = 'C')
shape 数组形状
dtype 类型
order 行优先还是列优先
x = np.empty([3,2], dtype = int)
numpy.zeros
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])
output:
[[(0, 0) (0, 0)]
[(0, 0) (0, 0)]]
numpy.ones
numpy从已有数组创建数组
numpy.asarray
numpy.asarray(a, dtype = None, order = None)
a 任意形式的输入参数,可以是,列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组
#动态数组,接受 buffer 输入参数,以流的形式读入转化成 ndarray 对象
numpy.frombuffer
s = b'Hello World'
a = np.frombuffer(s, dtype = 'S1')
#从可迭代的对象中建立数组
numpy.fromiter
list=range(5)
it=iter(list)
# 使用迭代器创建 ndarray
x=np.fromiter(it, dtype=float)
从数值范围创建数组
numpy.arange创建对象
numpy.arange(start, stop, step, dtype)
numpy.linspace创建等差数列数组
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 true 时,数列中包含stop值,反之不包含,默认是True。
retstep 如果为 True 时,生成的数组中会显示间距,反之不显示。
设置间距
a =np.linspace(1,10,10,retstep= True)
output:
(array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
b =np.linspace(1,10,10).reshape([10,1])
output:
[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]
[ 6.]
[ 7.]
[ 8.]
[ 9.]
[10.]]
numpy.logspace生成等比数列
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
start 序列的起始值为:base ** start
stop 序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中
起始值和终点值是base的平方
切片
通过slice切片
a = np.arange(10)
s = slice(2,7,2)
slice(start,stop,step)
#也可以通过冒号分割切片参数
b = a[2:7:2]
如果[]里面只有一个参数 a[5]则返回该索引对应的内容
a[5] = 5
如果为 [2:],表示从该索引开始以后的所有项都将被提取
如果使用了两个参数,如 [2:7],那么则提取两个索引(不包括停止索引)之间的项。
多维数组同样适用上述方法
还可以使用省略号切片,在第一个维度适用省略号代表将第一个维度全取到
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print (a[...,1]) # 第2列元素
print (a[1,...]) # 第2行元素
print (a[...,1:]) # 第2列及剩下的所有元素
output:
[2 4 5]
[3 4 5]
[[2 3]
[4 5]
[5 6]]
numpy高级索引
整数数组索引
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[[0,1,2], [0,1,0]]
获取数组(0,0),(1,1) 和 (2,0)位置的数据
切片和索引结合
a = np.array([[1,2,3], [4,5,6],[7,8,9]])
b = a[1:3, 1:3]
c = a[1:3,[1,2]]
d = a[...,1:]
output:
[[5 6]
[8 9]]
[[5 6]
[8 9]]
[[2 3]
[5 6]
[8 9]]
通过布尔运算
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print (x[x > 5])
output:
[ 6 7 8 9 10 11]
过滤掉非负数元素
a = np.array([1, 2+6j, 5, 3.5+5j])
print (a[np.iscomplex(a)])
花式索引
花式索引指利用整数数组进行索引
如果目标是一维数组,那么索引的结果就是对应位置的元素,如果目标是二维数组,那么就是对应下标的行。
一维数组
x = np.arange(9)
print(x)
# 一维数组读取指定下标对应的元素
x2 = x[[0, 6]]
output:
[0 6]
二维数组
x=np.arange(32).reshape((8,4))
print(x)
print (x[[4,2,1,7]])
output:
[[16 17 18 19]
[ 8 9 10 11]
[ 4 5 6 7]
[28 29 30 31]]
笛卡尔积
x=np.arange(32).reshape((8,4))
print (x[np.ix_([1,5,7,2],[0,3,1,2])])
获取的是(1,0)(1,3)(1,1)(1,2)。。。。。的数据
output:
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
广播
数组形状相同时
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = a * b
当数组形状不相同时
a = np.array([[ 0, 0, 0],
[10,10,10],
[20,20,20],
[30,30,30]])
b = np.array([0,1,2])
print(a + b)
output:
[[ 0 1 2]
[10 11 12]
[20 21 22]
[30 31 32]]

浙公网安备 33010602011771号