Calculating vector values ​​using NumPy

I am using NumPy.

I defined a vector xwith NumPy and other variables with numeric values.

I will return a vector of ythe same length as x, but the values y[i]in this vector ymust be calculated from different formulas depending on the corresponding one x[i].

Can I do something clever with NumPy or do I need to iterate through a vector xand for each element to xdetermine whether it is x[i]more or less than a certain value and determine which formula to use for a particular element?

I think I could do something like

y[x > a] = 2*x+7
y[x <= a] = 3*x+9
return y
+4
source share
1 answer

Check out np.where http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html .

y = np.where(x > a, 2 * x + 7, 3 * x + 9)
+6
source

Source: https://habr.com/ru/post/1584750/


All Articles