python 矩阵操作--->>np.column_stack用法

 

#举个栗子如下:


x_vals = np.linspace(0, 10, 5)
#print(x_vals)
[ 0.   2.5  5.   7.5 10. ]
1
2
3
4
#转化数组为矩阵

x_vals_column = np.transpose(np.matrix(x_vals))
#print(x_vals_column)
      ([[ 0  ],
        [ 2.5],
        [ 5. ],
        [ 7.5],
        [10. ]])
1
2
3
4
5
6
7
#生成一个列矩阵如下:

ones_column = np.transpose(np.matrix(np.repeat(1, 5)))
#print(ones_column) 
[[1]
[1]
[1]
[1]
[1]]
1
2
3
4
5
6
7
操作一下,函数功能很明确,将2个矩阵按列合并
A = np.column_stack((x_vals_column, ones_column)) 

#print(A)
[[ 0.   1. ]
[ 2.5  1. ]
[ 5.   1. ]
[ 7.5  1. ]
[10.   1. ]]
1
2
3
4
5
6
7
8
将2个矩阵按行合并
b = np.row_stack((x_vals_column, ones_column))
print(B)
[[ 0. ]
[ 2.5]
[ 5. ]
[ 7.5]
[10. ]
[ 1. ]
[ 1. ]
[ 1. ]
[ 1. ]
[ 1. ]]
1
2
3
4
5
6
7
8
9
10
11
12

————————————————
版权声明:本文为CSDN博主「Rock_Huang~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38632246/article/details/86713078

  

posted @ 2021-05-11 13:41  水木清扬  阅读(1928)  评论(0编辑  收藏  举报