- 使用np.random.randn(5)创建的为一个秩为1的数据结构,往往我们由于这个问题导致python代码产生bug,如图所示:
当我们使用a.shape时可以查看它的数据结构a=np.random.randn(5) [-1.48338769 -0.9962106 0.40807657 -0.60869681 1.80409923]
print(a.shape) (5,)
使用np.dot运算时,我们期望得到一个矩阵的乘积,然而确得到的是
为了解决这个问题,我们可以有这几种方式:print(np.dot(a,a.T)) 6.98468693706927
或者print(np.random.randn(5,1)) [[-0.9360015 ] [ 0.88972801] [-0.0660762 ] [ 1.09440092] [ 0.56603413]]
创建一个行向量或者列向量,或者使用rashape进行校正print(np.random.randn(1,5)) [[ 0.97008722 -0.51529414 -0.62981021 0.86658057 0.68974524]]
往往我们也可以使用assert来检验数据结构是否是我们所期待的数据结构print(a.reshape(5,1))
python-总结numpy
最新推荐文章于 2022-06-22 19:48:30 发布