NumPy库学习笔记(3) 数组的维度转换和类型转换

参考链接: NumPy官网

参考链接: NumPy: the absolute basics for beginners

参考链接: Quickstart tutorial

参考链接: Broadcasting广播

参考链接: NumPy 中文教程

参考链接: Python数据分析与展示

ndarray数组的变换:

对于创建后的ndarray数组,可以对其进行维度变换和元素类型变换:

ndarray数组的维度变换

方法说明
.reshape(shape)不改变数组元素,返回一个shape形状的数组,原数组不变
.resize(shape)与.reshape()功能一致,但修改原数组
.swapaxes(ax1,ax2)将数组n个维度中两个维度进行调换
.flatten()对数组进行降维,返回折叠后的一维数组,原数组不变

实验1:

>>> 
>>> 
>>> a = np.full((2,3,4),20200910,dtype=np.int32)
>>> b = a.reshape((3,8))
>>> b
array([[20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910],
       [20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910],
       [20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910]])
>>> a
array([[[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]],

       [[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]]])
>>> b[0,0,0] = 111
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    b[0,0,0] = 111
IndexError: too many indices for array
>>> b[0,0] = 111
>>> b
array([[     111, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910],
       [20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910],
       [20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910]])
>>> a
array([[[     111, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]],

       [[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]]])
>>> c = a.resize((3,8))
>>> c
>>> a
array([[     111, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910],
       [20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910],
       [20200910, 20200910, 20200910, 20200910, 20200910, 20200910,
        20200910, 20200910]])
>>> print(c)
None
>>> 
>>> 
>>> 
>>> 
>>> a = np.arange(24).reshape((2,3,4))
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.swapaxes(0,1)
array([[[ 0,  1,  2,  3],
        [12, 13, 14, 15]],

       [[ 4,  5,  6,  7],
        [16, 17, 18, 19]],

       [[ 8,  9, 10, 11],
        [20, 21, 22, 23]]])
>>> 
>>> 

说明:
ravel()和flatten()的区别:ravel()函数只返回一个原ndarray的视图,因此对这个视图的修改会影响到原ndarray,因此内存上更高效.而flatten()函数会返回一个拷贝,与原来的数组相互独立,不会相互影响.
参考链接: Reshaping and flattening multidimensional arrays

实验展示:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> x = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> x
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> x.flatten()
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> x.ravel()
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> xf = x.flatten()
>>> xr = x.ravel()
>>> x
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> xf
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> xr
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> x[0,0] = 99
>>> x
array([[99,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> xf
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> xr
array([99,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> xf[0] = 8888
>>> xf
array([8888,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,
         12])
>>> x
array([[99,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> xr
array([99,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> xr[0] = 7777
>>> xr
array([7777,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,
         12])
>>> x
array([[7777,    2,    3,    4],
       [   5,    6,    7,    8],
       [   9,   10,   11,   12]])
>>> xf
array([8888,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,
         12])
>>> 

ndarray数组的类型转换:
new_a = a.astype(new_type),可以转换为新的类型,astype()方法一定会创建新的数组(原始数据的一个拷贝),即使两个类型一致
ndarray数组向列表的转换:ls = a.tolist()
实验如下:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> a = np.full((2,3,4),20200910,dtype=np.int32)
>>> a
array([[[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]],

       [[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]]])
>>> a2 = a.astype(np.int32)
>>> a2
array([[[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]],

       [[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]]])
>>> a2[0,0,0] = 111
>>> a2
array([[[     111, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]],

       [[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]]])
>>> a
array([[[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]],

       [[20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910],
        [20200910, 20200910, 20200910, 20200910]]])
>>> a3 = a.astype(np.float32)
>>> a3
array([[[20200910., 20200910., 20200910., 20200910.],
        [20200910., 20200910., 20200910., 20200910.],
        [20200910., 20200910., 20200910., 20200910.]],

       [[20200910., 20200910., 20200910., 20200910.],
        [20200910., 20200910., 20200910., 20200910.],
        [20200910., 20200910., 20200910., 20200910.]]], dtype=float32)
>>> 
>>> ls = a.tolist()
>>> ls
[[[20200910, 20200910, 20200910, 20200910], [20200910, 20200910, 20200910, 20200910], [20200910, 20200910, 20200910, 20200910]], [[20200910, 20200910, 20200910, 20200910], [20200910, 20200910, 20200910, 20200910], [20200910, 20200910, 20200910, 20200910]]]
>>> type(ls)
<class 'list'>
>>> len(ls)
2
>>> len(ls[0])
3
>>> len(ls[0][0])
4
>>> 
>>> 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值