I do not understand what .astype () does - does it round numbers? And the second question as a consequence of the first: tell me how to change the function with lambda to change the array as necessary. Now:

[[ 0.69053728] [ 2.7581594 ] [-5.50454002]] A = lambda x: (x >= 1).astype(float) 

I get at the output:

 [[ 0] [ 1] [ 0]] 

How to do to get:

 [[ 0] [ 1] [-1]] 

Those. negative numbers less than -1 should turn into -1.

  • numpy (if we are talking about it) is not quite a python. Here is the link numpy.ndarray.astype - Sour Sourse
  • And what should negative numbers be greater than -1? - MaxU
  • Those numbers that are greater than -1 but less than 1 turn into zero - Pavel ZZZ

1 answer 1

NDArray.astype (type) returns a copy of the array converted to the specified type:

 In [383]: (a >= 1) Out[383]: array([[False], [ True], [False]]) In [384]: (a >= 1).astype(float) Out[384]: array([[0.], [1.], [0.]]) 

The answer to the second question:

use the np.where method:

 In [390]: res = np.where(a < -1, -1, np.where(a >= 1, 1, 0)) In [391]: res Out[391]: array([[ 0], [ 1], [-1]])