使用numpy.concatenate()时报错:TypeError: only integer scalar arrays can be converted to a scalar index
想要实现numpy数组按列拼接(合并),使用numpy.concatenate函数
函数说明
我写成了:
x=np.linspace(1,100,10)
ones=np.ones(len(x))
X=np.concatenate(x,ones,axis=0)
报错:TypeError: only integer scalar arrays can be converted to a scalar index
应该写成:
X=np.concatenate((x,ones),axis=0)
输出:
[ 1. 12. 23. 34. 45. 56. 67. 78. 89. 100. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1.]
补充:要实现增加一列数字,使用numpy.c_[]或者numpy.r_[]
其中numpy.c_[]实现按列添加,numpy.r_[]实现按行添加
np.c_[]
x=np.linspace(1,100,10)
ones=np.ones(len(x))
z=np.c_[ones,x]
[[ 1. 1.]
[ 1. 12.]
[ 1. 23.]
[ 1. 34.]
[ 1. 45.]
[ 1. 56.]
[ 1. 67.]
[ 1. 78.]
[ 1. 89.]
[ 1. 100.]]
np.r_[]
x=np.linspace(1,100,10)
ones=np.ones(len(x))
z=np.r_[ones,x]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 12. 23. 34.
45. 56. 67. 78. 89. 100.]