numpy 基础

nd5.shape#每个维度多少数据`

nd5 = np.array([[[1,3,4],[0,1,3]],[[-1,-3,-5],[-10,-8,-3]]])
# 第一维,几个 2
# 第二维,几个 2
# 第三维,几个 3
nd5`
'''array([[[  1,   3,   4],
        [  0,   1,   3]],

       [[ -1,  -3,  -5],
        [-10,  -8,  -3]]])'''
nd5.shape#每个维度多少数据(2, 2, 3)
nd5.dtype#类型dtype('int32')
nd5.ndim#几维的3
nd5.size#总共多少数据12
nd5.itemsize#每个维度多少个数据4
#改变维度
nd5.reshape((2,6))#当其中一个数为-1时,就以另一个数为准,当第二个数是-1时 就是不管每列几个数,只能有两行
'''array([[  1,   3,   4,   0],
       [  1,   3,  -1,  -3],
       [ -5, -10,  -8,  -3]])'''

numpy创建函数

np.zeros((3,4)#创建三行四列的全都是0的矩阵
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
np.zeros_like(a)#生成和a一样的全都是零的矩阵
# 等差数列 linear lin ------>线性
np.linspace(0,100,num = 51)
array([  0.,   2.,   4.,   6.,   8.,  10.,  12.,  14.,  16.,  18.,  20.,22.,  24.,  26.,  28.,  30.,  32.,  34.,  36.,  38.,  40.,  42.,44.,  46.,  48.,  50.,  52.,  54.,  56.,  58.,  60.,  62.,  64.,66.,  68.,  70.,  72.,  74.,  76.,  78.,  80.,  82.,  84.,  86.,88.,  90.,  92.,  94.,  96.,  98., 100.])

np.random.normal()函数
参数loc(float):正态分布的均值,对应着这个分布的中心。loc=0说明这一个以Y轴为对称轴的正态分布,
参数scale(float):正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线越高瘦。
参数size(int 或者整数元组):输出的值赋在shape里,默认为None# 单位矩阵
np.eye(5)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
# 数组中所有空值设置为3.14
np.full(shape = (2,3,4),fill_value=3.14)

def fun(i,j):
    return i*j
nd = np.fromfunction(fun,shape = (5,5))
fromfunction调用函数

存储数据

tofile()将数组中的数据以二进制格式写进文件
tofile()输出的数据不保存数组形状和元素类型等信息
fromfile()函数读回数据时需要用户指定元素类型,并对数组的形状进行适当的修改
np.fromfile('a.txt',dtype = np.int)
NumPy专用的二进制格式保存数据,它们会自动处理元素类型和形状等信息
如果想将多个数组保存到一个文件中,可以使用savez()
np.savez('c',data1=a,data2= b,data3 = b**2)
np.savetxt('./a.txt',nd)
load()自动识别npz文件,并且返回一个类似于字典的对象,可以通过数组名作为键获取数组的内容
data = np.load('c.npz',allow_pickle=True)
allow_pickle: 可选,布尔值,允许使用 Python pickles 保存对象数组,Python 中的 pickle 用于在保存到磁盘文件或从磁盘文件读取之前,对对象进行序列化和反序列化。
np.loadtxt('./a.txt')
详细地址:https://blog.csdn.net/kebu12345678/article/details/54837245

转换列表list 数组array  矩阵mat

array(a1)   # 列表 --> 数组
mat(a1)      #列表 ----> 矩阵
 a2.tolist()   # 数组 ---> 列表
改变图片 的 旋转 镜像  
rose = Image.open('./rose.jpg')
rose_data = np.array(rose)
Image.fromarray(rose_data[:,::-1])#变哪个在那个后面加-1

级联操作

a = np.random.randint(0,150,size = (30,4))
b = np.random.randint(0,150,size = (28,4))
c = np.random.randint(0,150,size = (50,4))
d = np.concatenate((a,b,c))#必须保证有行或列相同

numpy-堆叠函数stack(),hstack(),vstack()详解和参数axis=0,1,2
地址:https://blog.youkuaiyun.com/yyl424525/article/details/100104177

切割

d = np.random.randint(0,150,size = (8,4))
array([[107, 104,   2,  21],
       [ 30, 133,  13,  35],
       [  7,  14,  81,   4],
       [ 81,  99,  36, 112],
       [  3,  26,  94,  51],
       [ 51,  45,  50,  66],
       [ 33,  55,  51,  29],
       [141,  41, 103,  89]])
np.split(d,indices_or_sections=2)
[array([[107, 104,   2,  21],
        [ 30, 133,  13,  35],
        [  7,  14,  81,   4],
        [ 81,  99,  36, 112]]), array([[  3,  26,  94,  51],
        [ 51,  45,  50,  66],
        [ 33,  55,  51,  29],
        [141,  41, 103,  89]])]
  
np.split(d,indices_or_sections=2,axis = 1)
[array([[107, 104],
        [ 30, 133],
        [  7,  14],
        [ 81,  99],
        [  3,  26],
        [ 51,  45],
        [ 33,  55],
        [141,  41]]), array([[  2,  21],
        [ 13,  35],
        [ 81,   4],
        [ 36, 112],
        [ 94,  51],
        [ 50,  66],
        [ 51,  29],
        [103,  89]])]
np.split(d,indices_or_sections=[1,3,5])
[array([[107, 104,   2,  21]]), array([[ 30, 133,  13,  35],
        [  7,  14,  81,   4]]), array([[ 81,  99,  36, 112],
        [  3,  26,  94,  51]]), array([[ 51,  45,  50,  66],
        [ 33,  55,  51,  29],
        [141,  41, 103,  89]])]
三种切法

numpy的字符串

a = np.array('hello word python')
np.char.replace(a,' ','')
#array('hellowordpython', dtype='<U15')
np.char.join('!',a)
#array('h!e!l!l!o! !w!o!r!d! !p!y!t!h!o!n', dtype='<U33')
np.char.title(a)
#array('Hello Word Python', dtype='<U17')

numpy包中的linalg.matrix_rank方法计算矩阵的秩
numpy线性代数
地址:https://www.cnblogs.com/moon1992/p/4960700.html#_label8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值