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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
eeqesri
  • 217
  • 1
  • 2
  • 9
  • 2
    No repro. Got expected result. – TheMaster Jul 02 '22 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. – Karl Knechtel Jul 02 '22 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`? – slothrop Jul 02 '22 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 https://stackoverflow.com/questions/367565/how-do-i-build-a-numpy-array-from-a-generator – Karl Knechtel Jul 02 '22 at 20:22
  • I don't know. Now it is somehow working what I suggested. – eeqesri Jul 03 '22 at 05:04
  • I just realized, that I indeed did a transpose on the array before inserting. – eeqesri Jul 03 '22 at 05:12

1 Answers1

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)

Yusuf Syam
  • 701
  • 1
  • 4
  • 18
  • 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 Jul 03 '22 at 05:04
  • @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. – Yusuf Syam Jul 03 '22 at 05:12
  • Indeed you are right. It worked now after saving it in a new array and then printing it. – eeqesri Jul 03 '22 at 05:15
  • @eeqesri all right, that is actually my bad for not give a detailed answer, ill edit it – Yusuf Syam Jul 03 '22 at 05:17