zoukankan      html  css  js  c++  java
  • numpy将多维数组降维成一维

    numpy将多维数组降维成一维

    一、总结

    一句话总结:

    可以用reshape方法,但是感觉flatten方法更好
    pridict_y
    
    [[14.394563 ]
     [ 4.5585423]
     [10.817445 ]
     [12.291978 ]
     [26.076233 ]
     [20.033213 ]
     [11.320534 ]
     [14.528755 ]
     [11.454205 ]
     [ 9.153889 ]
     [12.769189 ]
     [ 5.7419834]
     [25.451023 ]
     [18.215645 ]
     [21.743513 ]
     [ 8.488817 ]
     [17.128687 ]
     [17.53172  ]
     [ 4.953989 ]
     [11.3504   ]
     [ 7.5612407]
     [ 4.2715034]
     [20.316795 ]
     [17.732632 ]
     [ 4.2850647]
     [ 6.971166 ]
     [11.657596 ]
     [24.968727 ]
     [13.93272  ]]
    
    pridict_y.reshape(29,)
    和
    pridict_y.flatten()
    结果都是
    
    array([14.394563 ,  4.5585423, 10.817445 , 12.291978 , 26.076233 ,
           20.033213 , 11.320534 , 14.528755 , 11.454205 ,  9.153889 ,
           12.769189 ,  5.7419834, 25.451023 , 18.215645 , 21.743513 ,
            8.488817 , 17.128687 , 17.53172  ,  4.953989 , 11.3504   ,
            7.5612407,  4.2715034, 20.316795 , 17.732632 ,  4.2850647,
            6.971166 , 11.657596 , 24.968727 , 13.93272  ], dtype=float32)

    二、【python】numpy库ndarray多维数组的维度变换方法

    转自或参考:【python】numpy库ndarray多维数组的维度变换方法:reshape、resize、swapaxes、flatten等详解与实例
    https://blog.csdn.net/brucewong0516/article/details/79185282

    numpy库对多维数组有非常灵巧的处理方式,主要的处理方法有:

    • .reshape(shape) : 不改变数组元素,返回一个shape形状的数组,原数组不变
    • .resize(shape) : 与.reshape()功能一致,但修改原数组
    In [22]: a = np.arange(20)
    #原数组不变
    In [23]: a.reshape([4,5])
    Out[23]:
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19]])
    
    In [24]: a
    Out[24]:
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
           17, 18, 19])
    
    #修改原数组
    In [25]: a.resize([4,5])
    
    In [26]: a
    Out[26]:
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19]])
    
    • .swapaxes(ax1,ax2) : 将数组n个维度中两个维度进行调换,不改变原数组
    In [27]: a.swapaxes(1,0)
    Out[27]:
    array([[ 0,  5, 10, 15],
           [ 1,  6, 11, 16],
           [ 2,  7, 12, 17],
           [ 3,  8, 13, 18],
           [ 4,  9, 14, 19]])
    • .flatten() : 对数组进行降维,返回折叠后的一维数组,原数组不变
    In [29]: a.flatten()
    Out[29]:
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
           17, 18, 19])
     
    我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站: fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
    博主25岁,前端后端算法大数据人工智能都有兴趣。
    大家有啥都可以加博主联系方式(qq404006308,微信fan404006308)互相交流。工作、生活、心境,可以互相启迪。
    聊技术,交朋友,修心境,qq404006308,微信fan404006308
    26岁,真心找女朋友,非诚勿扰,微信fan404006308,qq404006308
    人工智能群:939687837

    作者相关推荐

  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/13655349.html
Copyright © 2011-2022 走看看