前言
一、排序
numpy.sort(a, axis=-1, kind='quicksort', order=None)
np. random. seed( 20201028 )
x = np. random. rand( 3 , 3 ) * 10
x = np. around( x, 2 )
print ( x)
y = np. sort( x, axis= 0 )
print ( y)
y = np. sort( x, axis= 1 )
print ( y)
y = np. sort( x)
print ( y)
tp = np. dtype( [ ( 'name' , 'S10' ) , ( 'age' , np. int ) ] )
a = np. array( [ ( "dawang" , 21 ) , ( "yiyi" , 18 ) ] , dtype= tp)
b = np. sort( a, order= 'name' )
print ( b)
b = np. sort( a, order= 'age' )
print ( b)
numpy.argsort()
np. random. seed( 20201028 )
x = np. random. randint( 0 , 10 , 10 )
print ( x)
y = np. argsort( x)
print ( y)
print ( x[ y] )
y = np. argsort( - x)
print ( y)
print ( x[ y] )
np. random. seed( 20201028 )
x = np. random. rand( 3 , 3 ) * 10
x = np. around( x, 2 )
print ( x)
y = np. argsort( x, axis= 0 )
print ( y)
y = np. argsort( x, axis= 1 )
y = np. argsort( x)
print ( y)
[ [ 1 0 2 ]
[ 2 1 0 ]
[ 0 2 1 ] ]
z = np. array( [ np. take( x[ i] , np. argsort( x[ i] ) ) for i in range ( 3 ) ] )
print ( z)
numpy.lexsort()
np. random. seed( 20201029 )
x = np. random. rand( 3 , 3 ) * 10
x = np. around( x, 2 )
print ( x)
index = np. lexsort( [ x[ : , 0 ] ] )
y = x[ index]
print ( y)
index = np. lexsort( [ - 1 * x[ : , 0 ] ] )
y = x[ index]
print ( y)
index_2 = np. lexsort( [ x[ : , 1 ] ] )
index_ = np. lexsort( [ x[ 1 , : ] ] )
print ( index_)
numpy.partition()
以索引是 kth 的元素为基准,将元素分成两部分,即大于该元素的放在其后面,小于该元素的放在其前面。 一维
np. random. seed( 100 )
x = np. random. randint( 1 , 30 , 10 )
print ( x)
y = np. partition( x, kth= 2 )
print ( y)
np. random. seed( 100 )
x = np. random. randint( 1 , 30 , [ 8 , 3 ] )
print ( x)
z = np. partition( x, kth= 2 , axis= 0 )
print ( z)
z = np. partition( x, kth= - 3 , axis= 0 )
print ( z)
二、搜索
numpy.argmax() & numpy.argmin()
numpy.argmax()
是返回数组最大值的索引,numpy.argmin()
与之相反。一维数组
np. random. seed( 20201028 )
x = np. random. rand( 5 ) * 10
x = np. around( x, 2 )
print ( x)
y = np. argmax( x)
print ( y)
print ( x[ y] )
np. random. seed( 20201028 )
x = np. random. rand( 3 , 3 ) * 10
x = np. around( x, 2 )
print ( x)
y = np. argmax( x)
print ( y)
y = np. argmax( x, axis= 0 )
print ( y)
y = np. argmax( x, axis= 1 )
print ( y)
numppy.nonzero()
x = np. array( [ 0 , 2 , 3 , 1 ] )
print ( x)
print ( x. shape)
print ( x. ndim)
y = np. nonzero( x)
print ( y)
print ( np. transpose( y) )
print ( x[ np. nonzero( x) ] )
x = np. array( [ [ 3 , 0 , 0 ] , [ 0 , 4 , 0 ] , [ 5 , 6 , 0 ] ] )
print ( x)
print ( x. shape)
print ( x. ndim)
y = np. nonzero( x)
print ( y)
y = np. transpose( np. nonzero( x) )
print ( y)
y = x[ np. nonzero( x) ]
print ( y)
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
print ( x)
y = x > 5
print ( y)
y = np. nonzero( x > 5 )
print ( y)
y = x[ np. nonzero( x > 5 ) ]
print ( y)
y = x[ x > 3 ]
print ( y)
numpy.where(condition, x, y)
满足条件condition
,输出x
,不满足输出y
。
x = np. arange( 10 )
print ( x)
y = np. where( x < 5 , x, 10 * x)
print ( y)
x = np. array( [ [ 0 , 1 , 2 ] ,
[ 0 , 2 , 4 ] ,
[ 0 , 3 , 6 ] ] )
y = np. where( x < 4 , x, - 1 )
print ( y)
只有condition
,没有x
和y
,则输出满足条件 元素的坐标(等价于numpy.nonzero
)。 一维数组
x = np. array( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )
y = np. where( x > 5 )
print ( y)
print ( x[ y] )
y = np. nonzero( x > 5 )
print ( y)
print ( x[ y] )
x = np. array( [ [ 11 , 12 , 13 , 14 , 15 ] ,
[ 16 , 17 , 18 , 19 , 20 ] ,
[ 21 , 22 , 23 , 24 , 25 ] ,
[ 26 , 27 , 28 , 29 , 30 ] ,
[ 31 , 32 , 33 , 34 , 35 ] ] )
y = np. where( x > 25 )
print ( y)
print ( x[ y] )
y = np. nonzero( x > 25 )
print ( y)
print ( x[ y] )
numpy.searchsorted(a, v, side='left')
a:一维输入数组。 v:插入a
数组的值,可以为单个元素,list
或者ndarray
。 side:查询方向,当为left
时,将返回第一个符合条件的元素下标;当为right
时,将返回最后一个符合条件的元素下标。 此处仅考虑数组a为升数组的情况。
x = np. array( [ 0 , 1 , 5 , 9 , 11 , 18 , 26 , 33 ] )
y = np. searchsorted( x, 11 )
print ( y)
y = np. searchsorted( x, 11 , side= 'right' )
print ( y)
三、计数
numpy.count_nonzero()
x = np. array( [ 0 , 1 , 5 , 9 , 0 , 18 , 26 , 33 ] )
y = np. count_nonzero( x)
print ( y)
x = np. array( [ [ 0 , 1 , 2 ] , [ 2 , 1 , 3 ] , [ 0 , 3 , 4 ] ] )
y = np. count_nonzero( x)
print ( y)
y_1 = np. count_nonzero( x, axis= 0 )
print ( y_1)
y_2 = np. count_nonzero( x, axis= 1 )
print ( y_2)
四、集合
1.构造集合
x = np. unique( [ 1 , 1 , 3 , 2 , 3 , 3 ] )
print ( x)
x = np. array( [ [ 1 , 1 ] , [ 2 , 3 ] ] )
u = np. unique( x)
print ( u)
x = np. array( [ [ 1 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 2 , 3 , 4 ] ] )
y = np. unique( x, axis= 0 )
print ( y)
x = np. array( [ 'a' , 'b' , 'b' , 'c' , 'a' ] )
u, index = np. unique( x, return_index= True )
print ( u)
print ( index)
print ( x[ index] )
x = np. array( [ 1 , 2 , 6 , 4 , 2 , 3 , 2 ] )
u, index = np. unique( x, return_inverse= True )
print ( u)
print ( index)
print ( u[ index] )
u, count = np. unique( x, return_counts= True )
print ( u)
print ( count)
2.布尔运算
numpy.in1d(ar1, ar2, assume_unique=False, invert=False)
前面的数组是否包含于后面的数组,返回布尔值。
test = np. array( [ 0 , 1 , 2 , 5 , 0 ] )
states = [ 0 , 2 ]
mask = np. in1d( test, states)
print ( mask)
print ( test[ mask] )
mask = np. in1d( test, states, invert= True )
print ( mask)
print ( test[ mask] )
3.求两个集合的交集、并集和差集
交集 numpy.intersect1d(ar1, ar2)
x = np. intersect1d( [ 1 , 3 , 4 , 3 ] , [ 3 , 1 , 2 , 1 ] )
print ( x)
x = np. array( [ 1 , 1 , 2 , 3 , 4 ] )
y = np. array( [ 2 , 1 , 4 , 6 ] )
xy, x_ind, y_ind = np. intersect1d( x, y, return_indices= True )
print ( x_ind)
print ( y_ind)
print ( xy)
并集 numpy.union1d(ar1, ar2)
x = np. union1d( [ - 1 , 0 , 1 ] , [ - 2 , 0 , 2 , 5 ] )
print ( x)
差集 numpy.setdiff1d(ar1, ar2)
, 集合的差,即元素存在于第一个函数不存在于第二个函数中。
a = np. array( [ 1 , 2 , 3 , 2 , 4 , 1 ] )
b = np. array( [ 3 , 4 , 5 , 6 ] )
x = np. setdiff1d( a, b)
print ( x)
4.求两个集合的异或
setxor1d(ar1, ar2)
,异或,即两个数组中各自独自拥有的元素的集合。
a = np. array( [ 1 , 2 , 3 , 2 , 4 , 1 ] )
b = np. array( [ 3 , 4 , 5 , 6 ] )
x = np. setxor1d( a, b)
print ( x)