1

lets say I have a numpy array

Y =
array([[4.96473614, 6.02336215],
   [2.56213959, 2.74621531],
   [4.36170066, 5.60413956],
   ...,
   [5.93540184, 3.62864816],
   [2.34483661, 2.37333593],
   [6.3250522 , 7.09102362]])

with

Y.shape
(1000,2)

(for example 2dim datapoints with x and y)

How can I easily add a third column to this given array (a z-value) such that

Y.shape
(1000,3)

?

1
  • what should be the values of 3rd column? Commented Jun 24, 2020 at 17:22

2 Answers 2

0

Assume that you have:

  1. Y array of shape (3,2) (3 rows instead of 1000):

     array([[4.96473614, 6.02336215],
            [2.56213959, 2.74621531],
            [4.36170066, 5.60413956]])
    
  2. Y2 array (1-D) of shape (3,):

     array([10, 20, 30])
    

To get your result, you should:

  • first convert Y2 to (3,1) shape (3 rows, 1 column),
  • hstack them.

The code to do it is:

Y = np.hstack([Y, Y2[:, np.newaxis]])

The result is:

array([[ 4.96473614,  6.02336215, 10.        ],
       [ 2.56213959,  2.74621531, 20.        ],
       [ 4.36170066,  5.60413956, 30.        ]])
Sign up to request clarification or add additional context in comments.

Comments

-1

I hope this helps:

  1. Create the desired column in a (1000,1) array.
  2. Call ArrayName.append(DesiredColumn, axis = 1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.