将1D数组插入2D阵列
我正在尝试将列插入2D阵列中。 目前,我使用Itertools生成了一个2D阵列。
sample_points=[-1.5, -.8]
base_points = itertools.combinations_with_replacement(sample_points, 3)
base_points_list=list(base_points)
base_points_array=np.asarray(base_points_list)
然后我得到一个看起来像这样的数组:
>>> base_points_array
array([[-1.5, -1.5, -1.5],
[-1.5, -1.5, -0.8],
[-1.5, -0.8, -0.8],
[-0.8, -0.8, -0.8]])
我想在开始时添加一列,以使数组看起来像这样:
[[1 -1.5 -1.5 -1.5]
[1 -1.5 -1.5 -0.8]
[1 -1.5 -0.8 -0.8]
[1 -0.8 -0.8 -0.8]]
所以我使用了命令: np.insert(base_points_array,0,1,1) 因为它应该能够使用广播来做到这一点。 但是我得到了完全不同的东西。行的数量有变化:
array([[ 1. , -1.5, -1.5, -1.5, -0.8],
[ 1. , -1.5, -1.5, -0.8, -0.8],
[ 1. , -1.5, -0.8, -0.8, -0.8]])
我在做什么错?
I am trying to insert a column into a 2D array.
Currently I have a 2D array generated using itertools.
sample_points=[-1.5, -.8]
base_points = itertools.combinations_with_replacement(sample_points, 3)
base_points_list=list(base_points)
base_points_array=np.asarray(base_points_list)
Then I get an array which looks like this:
>>> base_points_array
array([[-1.5, -1.5, -1.5],
[-1.5, -1.5, -0.8],
[-1.5, -0.8, -0.8],
[-0.8, -0.8, -0.8]])
I want to add a column at the beginning so that the array looks like this:
[[1 -1.5 -1.5 -1.5]
[1 -1.5 -1.5 -0.8]
[1 -1.5 -0.8 -0.8]
[1 -0.8 -0.8 -0.8]]
So I used the command:
np.insert(base_points_array,0,1,1)
Because it should be able to do that using broadcasting.
but I get something completely different. the number of rows have changes:
array([[ 1. , -1.5, -1.5, -1.5, -0.8],
[ 1. , -1.5, -1.5, -0.8, -0.8],
[ 1. , -1.5, -0.8, -0.8, -0.8]])
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
np.append
。但是,如果您要插入的数组为1D数组[1,1,1,1]
您需要先以1个为单位扩展插入数组的尺寸
insert_array = = np.expand_dims(insert_array,1)
,然后您可以使用附录方法
base_points_array = np.append(insert_array,base_points_array,1)
Using the
np.append
. But if your array to insert is 1D arrayinsert_array= [1, 1, 1, 1]
You need to expand the dimension of your inserting array by 1 first, you can do it with
insert_array= np.expand_dims(insert_array, 1)
And then you can use the append method
base_points_array= np.append(insert_array, base_points_array, 1)