np 数组转为普通数组_np 数组转为普通数组_Python Numpy 数组操作 (一)

修改数组形状

numpy.reshape

numpy.reshape 函数可以在不改变数据的条件下修改形状,格式如下: numpy.reshape(arr, newshape, order='C')

arr:要修改形状的数组newshape:整数或者整数数组,新的形状应当兼容原有形状order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。

import numpy as np a = np.arange(8)print ('原始数组:')print (a)print ('') b = a.reshape(4,2)print ('修改后的数组:')print (b)

输出结果如下:

原始数组:[0 1 2 3 4 5 6 7]修改后的数组:[[0 1] [2 3] [4 5] [6 7]]

numpy.ndarray.flat

numpy.ndarray.flat 是一个数组元素迭代器:

import numpy as npa = np.arange(9).reshape(3,3) print ('原始数组:')for row in a: print (row) #对数组中每个元素都进行处理,可以使用flat属性,该属性是一个数组元素迭代器:print ('迭代后的数组:')for element in a.flat: print (element)

输出结果如下:

原始数组:[0 1 2][3 4 5][6 7 8]迭代后的数组:012345678

numpy.ndarray.flatten

numpy.ndarray.flatten 返回一份数组拷贝,对拷贝所做的修改不会影响原始数组

ndarray.flatten(order='C')

参数说明:

order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'K' -- 元素在内存中的出现顺序。

import numpy as npa = np.arange(8).reshape(2,4) print ('原数组:')print (a)print ('')# 默认按行 print ('展开的数组:')print (a.flatten())print ('') print ('以 F 风格顺序展开的数组:')print (a.flatten(order = 'F'))

输出结果如下:

原数组:[[0 1 2 3] [4 5 6 7]]展开的数组:[0 1 2 3 4 5 6 7]以 F 风格顺序展开的数组:[0 4 1 5 2 6 3 7]

numpy.ravel

numpy.ravel() 展平的数组元素,顺序通常是"C风格",返回的是数组视图(view,有点类似 C/C++引用reference的意味),修改会影响原始数组。

该函数接收两个参数:

numpy.ravel(a, order='C')

参数说明:

order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'K' -- 元素在内存中的出现顺序。

import numpy as np a = np.arange(8).reshape(2,4) print ('原数组:')print (a)print ('') print ('调用 ravel 函数之后:')print (a.ravel())print ('') print ('以 F 风格顺序调用 ravel 函数之后:')print (a.ravel(order = 'F'))

输出结果如下:

原数组:[[0 1 2 3] [4 5 6 7]]调用 ravel 函数之后:[0 1 2 3 4 5 6 7]以 F 风格顺序调用 ravel 函数之后:[0 4 1 5 2 6 3 7]

翻转数组

numpy.transpose

numpy.transpose 函数用于对换数组的维度,格式如下:

numpy.transpose(arr, axes)

参数说明:

arr:要操作的数组axes:整数列表,对应维度,通常所有维度都会对换。

import numpy as np a = np.arange(12).reshape(3,4) print ('原数组:')print (a )print ('') print ('对换数组:')print (np.transpose(a))

输出结果如下:

原数组:[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]对换数组:[[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]

numpy.ndarray.T 类似 numpy.transpose:

import numpy as npa = np.arange(12).reshape(3,4)print ('原数组:')print (a)print ('') print ('转置数组:')print (a.T)

输出结果如下:

原数组:[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]转置数组:[[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]

numpy.rollaxis

numpy.rollaxis 函数向后滚动特定的轴到一个特定位置,格式如下:

numpy.rollaxis(arr, axis, start)

参数说明:

arr:数组axis:要向后滚动的轴,其它轴的相对位置不会改变start:默认为零,表示完整的滚动。会滚动到特定位置。

import numpy as np # 创建了三维的 ndarraya = np.arange(8).reshape(2,2,2)print ('原数组:')print (a)print ('')# 将轴 2 滚动到轴 0(宽度到深度)print ('调用 rollaxis 函数:')print (np.rollaxis(a,2))# 将轴 0 滚动到轴 1:(宽度到高度)print ('')print ('调用 rollaxis 函数:')print (np.rollaxis(a,2,1))

输出结果如下:

原数组:[[[0 1] [2 3]] [[4 5] [6 7]]]调用 rollaxis 函数:[[[0 2] [4 6]] [[1 3] [5 7]]]调用 rollaxis 函数:[[[0 2] [1 3]] [[4 6] [5 7]]]

numpy.swapaxes

numpy.swapaxes 函数用于交换数组的两个轴,格式如下:

numpy.swapaxes(arr, axis1, axis2)

arr:输入的数组axis1:对应第一个轴的整数axis2:对应第二个轴的整数

import numpy as np # 创建了三维的 ndarraya = np.arange(8).reshape(2,2,2) print ('原数组:')print (a)print ('')# 现在交换轴 0(深度方向)到轴 2(宽度方向) print ('调用 swapaxes 函数后的数组:')print (np.swapaxes(a, 2, 0))

输出结果如下:

原数组:[[[0 1] [2 3]] [[4 5] [6 7]]]调用 swapaxes 函数后的数组:[[[0 4] [2 6]] [[1 5] [3 7]]]

本文参照菜鸟教程 NumPy 数组操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值