numpy - lookup operation Daquan

This paper record lookup operation encountered in their daily work, continuously updated.

 

extremum

min, max returns extremum

argmin (A, Axis = None, OUT = None), where the return extremum position ; without Axis, the first straightening, find extrema; Axis band, to find a dimension extremum

np.array = B ([[. 1, 2,. 3,. 5], [. 4,. 6, 2,. 6 ]])
 Print (np.max (B))   # returns the maximum value. 6 
Print (np.min (B) )   # returns the minimum. 1 
Print (np.argmax (B))   # returns a maximum position. 5 
Print (np.argmin (B))   # return the first position of the minimum 0 

Print (np.argmin ( B, Axis =. 1))      # [0 2]

 

NaN 值

nan value is expressed by a variety of forms, such as None, np.nan, np.NaN etc.

isNaN , returns a Boolean index

np.array = X (Range (10), DTYPE = np.float) 
Y = np.array (Range (10,20 ))
 Print (x.shape)                       # (10,) 
Print (X)                             # [. 1 of 0. The . 2. 3. 4. 6. The 7. The 5. The 8. The 9. The] 
Print (Y)                             # [. 11 10 14 15 12 is 13 is 18 is 16. 17. 19] 
X [. 3] = None                          # inserted NaN3 
X [. 5] = NP .NaN                        # inserted NaN3 
print (X)                             # [of 0. The 1. 2. 7. The NaN3 4. 6. The 8. The 9. The NaN3] 

# isNaN returns the index 
print(np.isnan(x))                  # [False False False  True False  True False False False False]
print(y[np.isnan(x)])               # [13 15]
print(y[~np.isnan(x)])              # [10 11 12 14 16 17 18 19]

If you want to return the index value, the following operations may be

= np.array DATA4, ([. 1,. 3, np.nan,. 5 ]) 

# # isNaN returns a Boolean value index nan 
Print np.isnan (DATA4,)    # [True False False False] 

# # found WHERE numerical value nan subscript 
Print np.where (np.isnan (DATA4,))          # (Array ([2]),) 
Print np.where (~ np.isnan (DATA4,))         # (Array ([0,. 1,. 3]), )

 

where conditions

WHERE , returns the tuple, a first index value, the second value is null

1. Enter must be an array, not a list

2. The input is typically a one-dimensional row vectors or column vectors are

3. Enter the multi-dimensional index returns two row vectors or column vectors returned different

argwhere , direct return index, returned as a two-dimensional array, a column vector

# List returned error 
Data Range = (10 )
 Print np.where (Data>. 6)       # (Array ([0]),) 

# one-dimensional array 
DATAl = np.array (Range (0, 20 is, 2 ))
 Print NP .Where (DATAl>. 6)      # (array ([. 7,. 8,. 9]),) 
Print np.where (data1.T>. 6)    # (array ([. 7,. 8,. 9]),) 

# two-dimensional array 
= np.array DATA2 ([Range (0, 20 is, 2 )])
 Print np.where (DATA2>. 6)      # (Array ([0, 0, 0]), Array ([. 7,. 8,. 9])) 

# rows and columns 
DATA3 = np.array ([Range (10), Range (10 )])
 Print (DATA3)
 Print np.where(data3>6)         # (array([0, 0, 0, 1, 1, 1]), array([7, 8, 9, 7, 8, 9]))
print np.where(data3.T>6)       # (array([7, 7, 8, 8, 9, 9]), array([0, 1, 0, 1, 0, 1]))

## argwhere 直接返回索引
print np.argwhere(data1>6)
# [[4]
#  [5]
#  [6]
#  [7]
#  [8]
#  [9]]
print np.argwhere(data1.T>6)
# [[4]
#  [5]
#  [6]
#  [7]
#  [8]
#  [9]]

 

where the input may be a plurality of conditions

# 求公共部分
print np.intersect1d([1, 4, 3], [3, 4, 5])          # [3 4]

# 多个条件
data2 = np.array([1,5, 11,16,20])
print np.where(data2>10)                                            # (array([2, 3, 4]),)

print np.where((data2>10) & (data2<18))                             # (array([2, 3]),)
print np.where(np.logical_and(data2>10, data2<18))                  # (array([2, 3]),)
print np.intersect1d(np.where(data2>10)[0], np.where(data2<18)[0])  # [2 3]

 

extract condition

Extract (for condition Condition, arr), according to certain criteria to return elements

print(np.extract(np.isnan(x), x))   # [nan nan]
print(np.extract(np.isnan(x), y))   # [13 15]
print(np.extract(x>8, x))           # [9.]

 

Non-zero elements

nonzero , returns the tuple, a first index value, the second value is null

x = [1, 0, 3, 0]
print(np.nonzero(x))                # (array([0, 2]),)

 

 

To be continued ...

Guess you like

Origin www.cnblogs.com/yanshw/p/11330715.html