美文网首页维小维写作训练营python学习笔记
python:numpy数组的拼接-水平拼接和竖直拼接

python:numpy数组的拼接-水平拼接和竖直拼接

作者: 书生_Scholar | 来源:发表于2019-08-14 09:05 被阅读0次

    导入numpy,赋值t1和t2

    In [1]:# 数据的拼接
    In [2]:import numpy as np
    In [3]:t1 = np.arange(12).reshape(2,6)
    In [4]:t2 = np.arange(12,24).reshape(2,6)
    In [5]:t1
    Out[5]: 
    array([[ 0,  1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10, 11]])
    In [6]:t2
    Out[6]: 
    array([[12, 13, 14, 15, 16, 17],
           [18, 19, 20, 21, 22, 23]])
    
    • 1、水平拼接
    In [9]:np.hstack((t1,t2))                # 水平拼接
    array([[ 0,  1,  2,  3,  4,  5, 12, 13, 14, 15, 16, 17],
           [ 6,  7,  8,  9, 10, 11, 18, 19, 20, 21, 22, 23]])
    In [11]:np.hstack((t2,t1))                # t1和t2的位置不一样,拼接的结果也不一样
    Out[11]: 
    array([[12, 13, 14, 15, 16, 17,  0,  1,  2,  3,  4,  5],
           [18, 19, 20, 21, 22, 23,  6,  7,  8,  9, 10, 11]])
    
    • 2、垂直拼接
    In [10]:np.vstack((t1,t2))               # 竖直拼接
    Out[10]: 
    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]])
    In [12]:np.vstack((t2,t1))                # t1和t2的位置不一样,拼接的结果也不一样
    Out[12]: 
    array([[12, 13, 14, 15, 16, 17],
           [18, 19, 20, 21, 22, 23],
           [ 0,  1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10, 11]])
    

    相关文章

      网友评论

        本文标题:python:numpy数组的拼接-水平拼接和竖直拼接

        本文链接:https://www.haomeiwen.com/subject/oeewjctx.html