Creating an array using a current array and equation

Viewed 15

How can I create an array utilizing a current array?

For example

X = [1,2,3,4]

I would like to use an equation such as y=2x to get a new array for y

It should be

Y = [2,4,6,8]

But how could I plug it in, using numpy, to get that array?

1 Answers

If you are looking specifically for numpy solution, a simple approach could be like

import numpy as np
y = lambda x: 2*x
X = [1,2,3,4]
Y = np.apply_along_axis(y,0,X)

Gives

Y : array([2, 4, 6, 8])
Related