0

If I set up a four column zeros array:

X_large = np.zeros((X.shape[0], 4)

And I have an array X as below:

X = np.array([
[0, 1]
[2, 2]
[3, 4]
[6, 5]
])

How can I get X_large to take X and have the last two columns show the first value of each row of the array squared and the second value of each row of the array cubed? Meaning:

X_large = [ [0, 1, 0, 1],
            [2, 2, 4, 8],
            [3, 4, 9, 64],
            [6, 5, 36, 125] ] 

This is probably not too hard, but I'm a pretty novice programmer in general.

Thanks!

chr
  • 81
  • 1
  • 9

1 Answers1

1

Calculate the power of X first, then do the column_stack:

np.column_stack((X, X ** [2,3]))
#array([[  0,   1,   0,   1],
#       [  2,   2,   4,   8],
#       [  3,   4,   9,  64],
#       [  6,   5,  36, 125]])

Or use np.power for the power calculation:

np.column_stack((X, np.power(X, [2,3])))
#array([[  0,   1,   0,   1],
#       [  2,   2,   4,   8],
#       [  3,   4,   9,  64],
#       [  6,   5,  36, 125]])
Psidom
  • 209,562
  • 33
  • 339
  • 356