python-numpy

np.ndarray

属性

ndim/itemsize/flags

# ndim
len(data.shape)=data.ndim # data: ndarray

# itemsize
data.itemsize # 数据中每个元素的大小,以字节为单位

# flags
data.flags # 数组的存储形式,字节码呈现

numpy/list

不共享内存

list/tuple/dict/ndarray copy()方法

list是浅拷贝,是不完全拷贝
tuple没有copy()方法
dictlist类似,同样是不完全拷贝
np.ndarray是深拷贝

索引

索引注意事项

a=np.random.randn(2,3) # shape: (2,3)
a[0] # shape: (3,)
a[:1] # shape: (1,3)
a[-1:] # shape: (1,3)
a[1:3] # shape: (1,3)
a[:10] # shape: (2,3)  # 超出范围不报错

c=np.arange(10)
c[:30] # shape (10,) 不报错!!!

索引赋值优化

x[:]=x+y # x不会重复申请内存

高级索引-感觉还要再看

https://blog.csdn.net/m0_50470999/article/details/108450726
https://blog.csdn.net/wangwenzhi276/article/details/53436694

索引细节

多条件索引

a=np.random.randn(2,3)
condition=(a>0.01) & (a<0.1) # !!! 必须加括号,否则抛出异常
np.where(condition) # 返回tuple 行/列索引
np.argwhere(condition) # 返回ndarray, 返回的是行列的组合索引

np.logical_and()
np.logical_or()

广播机制

并不是所有情况下都能满足广播机制
满足条件:

  1. 数据补位空
  2. 所有维度相等;或 其中一个维度size=1;或 某个维度数据不存在 !!!

函数

数值创建

np.float32([1,2])
ndarray.float() # 没有这种写法

astype/dtype

数值类型转换要用astype
不要直接使用ndarray.dtype=float32,这种会出现数据重写的错误

a=np.array([1,2])
a=a.astype(np.float16) 
a=a.astype('float32')

np.array()/np.asarray()

# 对于非ndarray的数据 .array()和.asarray()是深拷贝
a=list(range(10))
a1=np.array(a) 
a2=np.array(a)

# 对于ndarray的数据 .array()是深拷贝;.asarray()是浅拷贝
b=list(range(10))
b1=np.array(b) 
b2=np.array(b)

item()/itemset()

等价于索引赋值

reshape/resize

https://blog.csdn.net/weixin_38094100/article/details/115110443

reshape 是浅拷贝,且不支持inplace操作

np.reshape(data,(shape,...))
data.reshape(shape,...)

resize 是深拷贝,既支持inplace操作也支持非inplace操作

data.resize(shape,...) # inplace操作
np.resize(shape,...) # 非inplace操作

np.append()/np.concatnate()/np.insert()

np.concatnate()必须是同维度拼接
np.append()要注意

np.append(a,data) # 不同维度拼接会降维,相对维度数据就指定维度追加
a=np.arange(10).reshape(-1,5)
np.insert(a,1,[100,500],axis=1) # np.insert(ori,index,insert_data,axis)

data.view()是浅拷贝/data.copy()是深拷贝

frombuffer()/fromiter()???

http://c.biancheng.net/numpy/array-create.html

常见函数

data.flatten() # 展平,默认行优先 没有np.flatten()

data.squeeze()
np.squeeze(data) # 数据降维

np.concatnate((a,b)) # 不扩展维度

np.stack([a,b],axis=expand_ndim) # 扩展维度
np.hstack([a,b]) # 行拼接
np.vstack([a,b]) # 列拼接 

np.split(a,[index1,index2],axis) # 分割数组
np.hsplit() # 水平分割 

np.delete(data,index,axis) # index 切片,沿轴整数数组

np.argwhere() # 返回满足条件的索引,以组合的形式表示,返回时的二维数据
np.where() # 返回满足条件的行、列索引,分开表示,返回的是tuple
np.nonzero() # 返回非0的索引,行、列分开返回
np.extrcat(condiction,data) # 自定义condiction条件 ,和条件索引一样数据降维

np.unique(data) # 返回无重复数组,数据降维

np.bitwise_and() # & 按位与 
np.bitwise_or() # | 按位或
np.invert() # ~按位反

# 数学运算
np.degrees() # 将弧度制转换为角度制
np.around() # 四舍五入 # 奇数和偶数的区别!!!
np.floor() # 向下取整
np.ceil() # 向上取整

# 排序
np.sort() # 可以指定排序方法
np.argsort() # 返回排序后的索引,默认列优先
np.argmax()
np.argmin()

# 维度变化
np.c_() # 列拼接 https://blog.csdn.net/miaoyanmm/article/details/80825850
np.r_() # 行拼接

其他不常见函数

np.rollaxis()
np..swapaxes()
np.broadcast()
np.broadcast_to()

# 数学运算
# http://c.biancheng.net/numpy/arithmetic-operation.html

生成结构化数据

https://blog.csdn.net/qq_45488242/article/details/105895653

通过字典来创建结构化数组的数据类型

import numpy as np

# versioin 1
d1={'names':('name','age','weight'),'formats':('U10','i4','f8')}
array1=np.zeros(4,dtype=d1)

# version 2
d2=[('name','U10'),('age','i4'),('weight','f8')]
array2=np.zeros(4,dtype=d2)

ndarray op

迭代数组

np.nditer(data)默认列优先迭代

for x in np.nditer(b,[order='C',[op_flags=['readwrite']]]): # order='F' F表示列优先;op_flags表示在迭代时是否为可修改
	print(x,)

# 利用广播机制进行多个数组迭代
for x,y in np.nditer([a,b]):
	pass

numpy也能处理字符串数据

np.char.xxx模块
http://c.biancheng.net/numpy/string-function.html

numpy矩阵与线性代数运算

numpy.matlib
numpy.linalg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值