将1D数组插入2D阵列

发布于 2025-02-12 12:51:35 字数 831 浏览 3 评论 0原文

我正在尝试将列插入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2025-02-19 12:51:35

使用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 array

insert_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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文