np.column_stack用法

#举个栗子如下:


x_vals = np.linspace(0, 10, 5)
#print(x_vals)
[ 0.   2.5  5.   7.5 10. ]

#转化数组为矩阵

x_vals_column = np.transpose(np.matrix(x_vals))
#print(x_vals_column)
      ([[ 0  ],
        [ 2.5],
        [ 5. ],
        [ 7.5],
        [10. ]])

#生成一个列矩阵如下:

ones_column = np.transpose(np.matrix(np.repeat(1, 5)))
#print(ones_column) 
[[1]
[1]
[1]
[1]
[1]]

操作一下,函数功能很明确,将2个矩阵按列合并

A = np.column_stack((x_vals_column, ones_column)) 

#print(A)
[[ 0.   1. ]
[ 2.5  1. ]
[ 5.   1. ]
[ 7.5  1. ]
[10.   1. ]]

将2个矩阵按行合并

b = np.row_stack((x_vals_column, ones_column))
print(B)
[[ 0. ]
[ 2.5]
[ 5. ]
[ 7.5]
[10. ]
[ 1. ]
[ 1. ]
[ 1. ]
[ 1. ]
[ 1. ]]

你可能感兴趣的:(深度学习,python)