np.c_与np.r_

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np
 
def test():
    '''
    numpy函数np.c_和np.r_学习使用
    '''
    data_list1=[4,6,12,6,0,3,7]
    data_list2=[1,5,2,65,6,7,3]
    data_list3=[1,5,2,65,6]
    print u'np.r_  data_list1和data_list2合并'
    print np.r_[data_list1,data_list2]
    print u'np.r_  data_list1和data_list3合并'
    print np.r_[data_list1,data_list3]
 
 
    print u'np.c_ data_list1和data_list2合并'
    print np.c_[data_list1,data_list2]
    print u'np.c_ data_list1和data_list3合并'
    print np.c_[data_list1,data_list3]
 
 
if __name__=='__main__':

其中,data_list1:1行7列,data_list2:1行7列,data_list1:1行5列,

结果如下

np.r_  data_list1和data_list2合并
[ 4  6 12  6  0  3  7  1  5  2 65  6  7  3]
np.r_  data_list1和data_list3合并
[ 4  6 12  6  0  3  7  1  5  2 65  6]
np.c_ data_list1和data_list2合并
 [[ 4  1]
 [ 6  5]
 [12  2]
 [ 6 65]
 [ 0  6]
 [ 3  7]
 [ 7  3]]
np.c_ data_list1和data_list3合并
ValueError: all the input array dimensions except for the concatenation axis must match exactly
[Finished in 0.2s with exit code 1]

 

简单地总结一下用法就是:

np.r_是按行连接两个矩阵,就是把两矩阵上下相加,要求列数相等。

np.c_是按列连接两个矩阵,就是把两矩阵左右相加,要求行数相等。

猜你喜欢

转载自www.cnblogs.com/fcfc940503/p/10966651.html
np