SoFunction
Updated on 2024-11-15

Specific examples of usage in Python

I. Basic knowledge

function is a vectorized version of the ternary expression x if condition else y. It has two uses:

(condition,x,y) When there are three parameters within where, the first parameter represents the condition, when the condition is valid where method returns x, when the condition is not valid where returns y

(condition) When there is only one parameter in where, that parameter represents the condition, when the condition is established, where returns the coordinates of each element that meets the condition, and the return is in the form of a tuple.

II. Specific examples

(condition,x,y)

(1) Example 1:

There are two arrays of values and one boolean array. When the boolean array is True, the value of xarr is output, otherwise the value of yarr is output

Code:

import numpy as np
xarr = ([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = ([2.1, 2.2, 2.3, 2.4, 2.5])
carr = ([True, False, True, True, False])
result = (carr, xarr, yarr)
print(result)

Results:

[1.1 2.2 1.3 1.4 2.5]

(2) Example 2:

The second and third arguments to where do not need to be arrays and can be scalars. a typical use of where in data analysis is to generate a new array based on an array.

Suppose you have a randomly generated matrix of data and you want to replace all positive values with 2 and all negative values with -2.

Code:

import numpy as np
arr = (4, 4)
print(f'arr is {arr}')
brr = (arr > 0, 2, -2)
print(f'brr is {brr}')

Results:

arr is [[ 0.25269699  0.65883562 -0.25147374 -1.39408775]
 [-0.53498966 -0.97424514 -1.13900344  0.53646289]
 [ 1.51928884  0.80805854 -0.82968494  0.82861136]
 [ 0.09549692  0.59623201  0.50521756  1.648034  ]]
brr is [[ 2  2 -2 -2]
 [-2 -2 -2  2]
 [ 2  2 -2  2]
 [ 2  2  2  2]]

(3) Example 3:

It is also possible to unite scalars and arrays using:

Replace only positive values with 2:

Code:

import numpy as np
arr = (4, 4)
print(f'arr is {arr}')
brr = (arr > 0, 2, arr)
print(f'brr is {brr}')

Results:

arr is [[ 0.30064659 -0.5195743   0.05916467  0.58790562]
 [ 1.0921678  -0.30010407 -0.43318393  0.60455133]
 [-0.35091718  0.01738908 -0.3067928  -0.0439254 ]
 [ 0.59166385  1.04319898 -0.73044529  0.10357739]]
brr is [[ 2.         -0.5195743   2.          2.        ]
 [ 2.         -0.30010407 -0.43318393  2.        ]
 [-0.35091718  2.         -0.3067928  -0.0439254 ]
 [ 2.          2.         -0.73044529  2.        ]]

2. (condition)

Returns the coordinates

Example:

import numpy as np
a = ([2, 4, 6, 8, 10])
# One-dimensional matrices
result_1 = (a > 5)
print(result_1)
b = (4, 4)
# Two-dimensional matrices
print(b)
result_2 = (b > 0)
print(result_2)

Results:

Output from spyder call 'get_namespace_view':
(array([2, 3, 4], dtype=int64),)
[[-0.83362412 -2.23605027  0.15374728  0.70877121]
 [-0.30212209  0.56606258  0.95593288  1.03250978]
 [-0.85764257  1.48541971  0.73199465  1.66331547]
 [-0.22020036  0.46416537 -0.75622715  0.32649036]]
(array([0, 0, 1, 1, 1, 2, 2, 2, 3, 3], dtype=int64), array([2, 3, 1, 2, 3, 1, 2, 3, 1, 3], dtype=int64))

summarize

The arguments passed to can be either arrays of equal size or scalars. When no arguments are passed and only the condition is passed, the output is the coordinates that satisfy the condition.

This article on Python () usage is introduced to this article, more related Python () usage content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!