目录
Numpy数组操作
修改数组形状
reshape(arr, newshape, order=‘C’)
描述:不改变数据的条件下修改形状
- arr:要修改形状的数组
- newshape:整数或者整数数组,新的形状应当兼容原有形状
- order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。
import numpy as np
a = np.array([[1,2,3,4,5,6],
[1,2,3,4,5,6]])
print('shape a:',a.shape)
a = a.reshape(3,4)
print(a, '\nshape after:{}'.format(a.shape))
flat
描述:数组元素迭代器
import numpy as np
a = np.array([[1,2,3,4,5,6],
[1,2,3,4,5,6]])
print ('原始数组:')
for row in a:
print (row)
#对数组中每个元素都进行处理,可以使用flat属性,该属性是一个数组元素迭代器:
print ('迭代后的数组:')
for element in a.flat:
print (element)
flatten(order=‘C’)
描述:返回一份数组拷贝,对拷贝所做的修改不会影响原始数组
- order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序。
import numpy as np
a = np.array([[1,2,3,4,5,6],
[1,2,3,4,5,6]])
print ('原数组:')
print (a)
print ('\n')
# 默认按行
print ('展开的数组:')
print (a.flatten())
print ('\n')
print ('以 F (即列)顺序展开的数组:')
print (a.flatten(order = 'F'))
ravel(a, order=‘C’)
描述:返回展开数组(效果和flatten类似),返回的是数组视图(view,有点类似 C/C++引用reference的意味),修改会影响原始数组。
- order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序。
import numpy as np
a = np.array([[1,2,3,4,5,6],
[1,2,3,4,5,6]])
print ('原数组:')
print (a)
print ('\n')
print ('调用 ravel 函数之后:')
print (a.ravel())
print ('\n')
print ('以 F 风格顺序调用 ravel 函数之后:')
print (a.ravel(order = 'F'))
翻转数组
transpose(arr, axes)和.T
描述:对换数组的维度
- arr:要操作的数组
- axes:整数列表,对应维度,通常所有维度都会对换。
import numpy as np
a = np.arange(12).reshape(3,4)
print ('原数组:')
print (a )
print ('\n')
print ('对换数组:')
print (np.transpose(a))
rollaxis(arr, axis, start)
描述:向后滚动特定的轴到一个特定位置
- arr:数组
- axis:要向后滚动的轴,其它轴的相对位置不会改变
- start:默认为零,表示完整的滚动。会滚动到特定位置。
import numpy as np
# 创建了三维的 ndarray
a = np.arange(8).reshape(2,2,2)
print ('原数组:')
print (a)
print ('\n')
# 将轴 2 滚动到轴 0(宽度到深度)
print ('调用 rollaxis 函数:')
print (np.rollaxis(a,2))
# 将轴 0 滚动到轴 1:(宽度到高度)
print ('\n')
print ('调用 rollaxis 函数:')
print (np.rollaxis(a,2,1))
swapaxes(arr, axis1, axis2)
描述:交换数组的两个轴
- arr:输入的数组
- axis1:对应第一个轴的整数
- axis2:对应第二个轴的整数
import numpy as np
# 创建了三维的 ndarray
a = np.arange(8).reshape(2,2,2)
print ('原数组:')
print (a)
print ('\n')
# 现在交换轴 0(深度方向)到轴 2(宽度方向)
print ('调用 swapaxes 函数后的数组:')
print (np.swapaxes(a, 2, 0))
改变数据维度(增加、删除)
expand_dims(arr, axis)
描述:通过在指定位置插入新的轴来扩展数组形状。
- arr:输入数组
- axis:新轴插入的位置
import numpy as np
x = np.array(([1,2],[3,4]))
print ('数组 x:')
print (x)
print ('\n')
y = np.expand_dims(x, axis = 0)
print ('数组 y:')
print (y)
print ('\n')
print ('数组 x 和 y 的形状:')
print (x.shape, y.shape)
print ('\n')
# 在位置 1 插入轴
y = np.expand_dims(x, axis = 1)
print ('在位置 1 插入轴之后的数组 y:')
print (y)
print ('\n')
print ('x.ndim 和 y.ndim:')
print (x.ndim,y.ndim)
print ('\n')
print ('x.shape 和 y.shape:')
print (x.shape, y.shape)
squeeze(arr, axis)
描述:从给定数组的形状中删除一维的条目。
- arr:输入数组
- axis:整数或整数元组,用于选择形状中一维条目的子集
import numpy as np
x = np.arange(9).reshape(1,3,3)
print ('数组 x:')
print (x)
print ('\n')
y = np.squeeze(x)
print ('数组 y:')
print (y)
print ('\n')
print ('数组 x 和 y 的形状:')
print (x.shape, y.shape)
newaxis
描述:numpy中包含的newaxis可以给原数组增加一个维度
np.newaxis放的位置不同,产生的新数组也不同
x = np.random.randint(1, 8, size=(2,3))
x1 = x[:, np.newaxis, :]
print(x.shape)
print(x1.shape)
x = np.random.randint(1, 8, size=(2,3))
x1 = x[np.newaxis, :, :]
print(x.shape)
print(x1.shape)
参考
Reference:Link