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)

?

jeffs
  • 321
  • 2
  • 9

2 Answers2

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.        ]])
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
-1

I hope this helps:

  1. Create the desired column in a (1000,1) array.
  2. Call ArrayName.append(DesiredColumn, axis = 1)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58