欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Numpy - 知识点总结(三)

程序员文章站 2022-05-29 18:15:19
...

广播

广播是指numpy在算数运算期间处理不同形状的数组的能力,对数组的算数运算通常在相应的元素上进行。如果两个阵列具有完全相同的形状,则这些操作被无缝执行;

迭代

Numpy包包含一个迭代器对象numpy.nditer,它是一个有效的多维迭代器对象,可以用于在数组上进行迭代。数组的每一个元素可使用Python的标准接口iterator访问;

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
for i in np.nditer(arr1):
    print(i)

这里注意,是numpy包,而不是ndarray对象,基于ndarray对象的操作方法相对较少,后续讲解的,基本都是numpy的操作方法;

数组操作

reshape:不改变数据的条件下,改变数据的形状,与resize类似;

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
arr2 = arr1.reshape((3,4))
print(arr1)
print(arr2)

Numpy - 知识点总结(三)

flat:数组上的一维迭代器#与nditer类似

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
for i in arr1.flat:
    print(i,end=' ')

Numpy - 知识点总结(三)

flatten:返回迭代为一维的数组副本

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
print('arr1',arr1)
arr2 = arr1.flatten()
print(arr2)
print(arr1)
Numpy - 知识点总结(三)

ravel:返回连续的展开数组

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
print('arr1',arr1)
arr2 = arr1.ravel()
print(arr2)
print(arr1)

Numpy - 知识点总结(三)

transpose:翻转数组的维度

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
print('arr1',arr1)
arr2 = np.transpose(arr1)
print(arr2)
print(arr1)

Numpy - 知识点总结(三)

ndarray.Tself.transpose:数组转秩

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
print('arr1',arr1)
arr2 = arr1.T
print(arr2)
print(arr1)

Numpy - 知识点总结(三)

rollaxis:向后滚动指定的轴

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
print('arr1',arr1)
arr2 = np.rollaxis(arr1,axis=1)
print(arr2)
print(arr1)

Numpy - 知识点总结(三)

注:如果理解这里向后滚动指定的轴,以上面arr1为例,arr1的维度为(4,3),为一个2维数组,一共就又两个轴,0轴和1轴,这里指定了向后滚动的轴为1轴,即为1轴的内容和0轴的内容交换,即二维数组的转秩;

swapaxes:互换数组的两个轴

import numpy as np
arr1 = np.arange(0,12).reshape((4,3))
print('arr1',arr1)
arr2 = np.swapaxes(arr1,axis1=0,axis2=1)
print(arr2)
print(arr1)
Numpy - 知识点总结(三)

broadcast:产生模仿广播的对象

import numpy as np
a = [1,2,3]
b = ['a','b','c']
c = np.broadcast(a,b)
print(c.shape)
print(c)
for i in c:
    print(i)

Numpy - 知识点总结(三)

broadcast_to:将数组广播到新形状(将数组广播成指定维度的数组)

import numpy as np
a = np.array([1,2,3,4])
c = np.broadcast_to(a,(2,4))
print('a using broadcast to :',c.shape)
print(c)
b = np.array([4,5,6,7]).reshape([2,2])
c = np.broadcast_to(b,(3,2,2))
print('b using broadcast to :',c.shape)
print(c)

Numpy - 知识点总结(三)

expand_dims: 扩展数组的形状

import numpy as np
a = np.array([1,2,3,4])
b = np.expand_dims(a,axis=0)
c = np.expand_dims(a,axis=1)
print(a)
print(b)
print(c)

Numpy - 知识点总结(三)

squeeze:从数组的形状中删除单维条目(只有在指定的轴axis上维度为1时,该方法才有效,否则会报错)

import numpy as np
a = np.array([1,2,3,4])
b = np.expand_dims(a,axis=0)
c = np.expand_dims(a,axis=1)
print(a)
print(b)
print(c)
d = np.expand_dims(b,axis=0)
b = d.squeeze(axis=0)
print(b)

Numpy - 知识点总结(三)

concatenate:沿着现存的轴连接数据序列(需要理解现存的轴的意思,现存的轴,表示原数据仅有轴,一维的为0,二维的为0和1)

如果两个供连接的数组,在连接轴上的维度不一致,则无法连接且会报错ValueError,另外该方法接受的数据为元组,且不会改变数组的维数;

import numpy as np
a = np.array([1,2,3,4]).reshape(2,2)
b = np.array([5,6,7,8]).reshape(2,2)
c = np.concatenate((a,b),axis=1)
print(c)

Numpy - 知识点总结(三)

stack:沿着新轴连接数组序列(会增加数组的维数)

import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.stack([a,b],axis=0)
print(c)
c = np.stack([a,b],axis=1)
print(c)

Numpy - 知识点总结(三)

hstack:水平折叠序列中的数组(列方向)(拼接功能)

vstack:竖直折叠序列中的数组(行方向)(拼接功能)

import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.stack([a,b],axis=0)
print(c)
c = np.stack([a,b],axis=1)
print(c)
c = np.hstack((a,b))
print(c)
c = np.vstack((a,b))
print(c)

Numpy - 知识点总结(三)

split:将一个数组分割成多个子数组(不会改变数组的维数)

指定分割成几个数组
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9])
b  = np.split(a,3)
print(b)
for i  in b:
    print(i)
Numpy - 知识点总结(三)

指定分割数字

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9])
b  = np.split(a,3)
print(b)
for i  in b:
    print(i)
b  = np.split(a,[4,6])
print(b)

Numpy - 知识点总结(三)

hsplit:将一个数组水平分割成多个子数组(按列)

vsplit:将一个数组竖直分割成多个子数组(按行)

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = np.hsplit(a,3)
print(b)
for i in b:
    print(i)
b = np.vsplit(a,3)
for i in b:
    print(i)

Numpy - 知识点总结(三)

resize:返回指定形状的新数组

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = np.resize(a,[1,9])
print(b)

Numpy - 知识点总结(三)

与reshape进行对比

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = np.resize(a,[1,9])
print(b)
c = np.reshape(a,[1,9],order='F')
print(c)
c = np.reshape(a,[1,9],order='C')
print(c)

Numpy - 知识点总结(三)

append:将值添加到数组的末尾

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = [[2,3,4]]
c = np.append(a,b,axis=0)
print(c)

Numpy - 知识点总结(三)

insert:沿指定轴将值插入到指定下标之前

如果不提供axis,则原数组会被展开,然后在提供的obj索引处插入值

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = np.insert(a,9,[1,1,1])
print(b)

如果提供axis的话,按在object指定的axis轴上相应的位置处插入值

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = np.insert(a,1,[1,1,1],axis=0)
print(b)
b = np.insert(a,1,[1,1,1],axis=1)
print(b)

Numpy - 知识点总结(三)

delete:返回删掉某个轴的子数组的新数组

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]).reshape([3,3])
b = np.delete(a,1,axis=1)
print(b)

Numpy - 知识点总结(三)

unique:寻找数组内唯一元素;

import numpy as np
a = np.array([1,2,3,3,5,3,7,8,9]).reshape([3,3])
b = np.unique(a)
print(b)
Numpy - 知识点总结(三)

相关标签: python numpy