Python中numpy数组操作 包括改变、增删数据维度的一些函数方法总结

本文总结了Python中numpy库用于修改数组形状、翻转数组以及改变数据维度的相关函数,包括reshape、flat、flatten、ravel、transpose、rollaxis、swapaxes、expand_dims、squeeze和newaxis等。这些函数可用于高效地操作多维数组,实现数据的增删、翻转和重塑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

在这里插入图片描述

参考

ReferenceLink

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值