0

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?

6
  • 2
    No repro. Got expected result. Commented Jul 2, 2022 at 20:17
  • 1
    I cannot reproduce the problem. When I try np.insert(base_points_array,0,1,1), I get the expected result, and not the claimed result. Do keep in mind that np.insert creates a new array and does not modify the original. Commented Jul 2, 2022 at 20:18
  • 1
    Executing np.insert(base_points_array.T,0,1,1) gives the result you see. Any chance you accidentally transposed your array in between creating it and doing the insert? Commented Jul 2, 2022 at 20:19
  • 2
    Agreed, it looks as though the array somehow got transposed first. As a side note, it isn't necessary to make a temporary list: see also stackoverflow.com/questions/367565/… Commented Jul 2, 2022 at 20:22
  • I don't know. Now it is somehow working what I suggested. Commented Jul 3, 2022 at 5:04

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

I intially gave an up arrow for the answer. but then I realized, that it didn't work somehow. So wanted to cancel the up arrow and clicked on the down arrow, although I didn't want to give a down arrow. I am really sorry about that. Is there a way I can remove the downvote?
@eeqesri It didnt work for you? maybe because you did not assign the np.append() to a new array, since it does not modify the array but make a new array instead.
Indeed you are right. It worked now after saving it in a new array and then printing it.
@eeqesri all right, that is actually my bad for not give a detailed answer, ill edit it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.