一,numpy中数值的修改
import numpy as np
temp = np. arange( 30 ) . reshape( 6 , 5 )
print ( temp)
temp[ : , 1 ] = 0
print ( temp)
[ [ 0 1 2 3 4 ]
[ 5 6 7 8 9 ]
[ 10 11 12 13 14 ]
[ 15 16 17 18 19 ]
[ 20 21 22 23 24 ]
[ 25 26 27 28 29 ] ]
[ [ 0 0 2 3 4 ]
[ 5 0 7 8 9 ]
[ 10 0 12 13 14 ]
[ 15 0 17 18 19 ]
[ 20 0 22 23 24 ]
[ 25 0 27 28 29 ] ]
二,布尔索引
import numpy as np
temp = np. arange( 30 ) . reshape( 6 , 5 )
print ( temp)
temp[ temp< 10 ] = 3
print ( temp)
temp[ np. logical_or( temp< 3 , temp> 15 ) ] = 0
print ( temp)
temp[ np. logical_and( temp> 3 , temp< 15 ) ] = 0
print ( temp)
[ [ 0 1 2 3 4 ]
[ 5 6 7 8 9 ]
[ 10 11 12 13 14 ]
[ 15 16 17 18 19 ]
[ 20 21 22 23 24 ]
[ 25 26 27 28 29 ] ]
[ [ 3 3 3 3 3 ]
[ 3 3 3 3 3 ]
[ 10 11 12 13 14 ]
[ 15 16 17 18 19 ]
[ 20 21 22 23 24 ]
[ 25 26 27 28 29 ] ]
[ [ 3 3 3 3 3 ]
[ 3 3 3 3 3 ]
[ 10 11 12 13 14 ]
[ 15 0 0 0 0 ]
[ 0 0 0 0 0 ]
[ 0 0 0 0 0 ] ]
[ [ 3 3 3 3 3 ]
[ 3 3 3 3 3 ]
[ 0 0 0 0 0 ]
[ 15 0 0 0 0 ]
[ 0 0 0 0 0 ]
[ 0 0 0 0 0 ] ]
三,三元运算符where
1,where语句
import numpy as np
temp = np. arange( 30 ) . reshape( 6 , 5 )
print ( temp)
temp = np. where( temp< 10 , 0 , 11 )
print ( temp)
[ [ 0 1 2 3 4 ]
[ 5 6 7 8 9 ]
[ 10 11 12 13 14 ]
[ 15 16 17 18 19 ]
[ 20 21 22 23 24 ]
[ 25 26 27 28 29 ] ]
[ [ 0 0 0 0 0 ]
[ 0 0 0 0 0 ]
[ 11 11 11 11 11 ]
[ 11 11 11 11 11 ]
[ 11 11 11 11 11 ]
[ 11 11 11 11 11 ] ]
2,python的语法中的三元运算符
a = 3 if 3 > 4 else 2
print ( a)
3,logical_and和np.logical_or
符合逻辑需要结合np. logical_and和np. logical_or使用
import numpy as np
temp = np. arange( 15 ) . reshape( 3 , 5 )
print ( temp)
print ( np. where( temp< 6 , 1 , 0 ) )
print ( np. where( np. logical_and( temp > 3 , temp < 6 ) , 1 , 0 ) )
print ( np. where( np. logical_or( temp < 3 , temp > 6 ) , 1 , 0 ) )
[ [ 0 1 2 3 4 ]
[ 5 6 7 8 9 ]
[ 10 11 12 13 14 ] ]
[ [ 1 1 1 1 1 ]
[ 1 0 0 0 0 ]
[ 0 0 0 0 0 ] ]
[ [ 0 0 0 0 1 ]
[ 1 0 0 0 0 ]
[ 0 0 0 0 0 ] ]
[ [ 1 1 1 0 0 ]
[ 0 0 1 1 1 ]
[ 1 1 1 1 1 ] ]
四,通用判断函数
1,numpy.all和numpy.any
import numpy as np
t = np. arange( 30 ) . reshape( 6 , 5 )
print ( t[ 0 : 2 , 0 : 3 ] )
print ( '-' * 50 )
print ( np. all ( t[ 0 : 2 , 0 : 3 ] ) )
print ( np. any ( t[ 0 : 2 , 0 : 3 ] ) )
print ( '-' * 50 )
print ( np. all ( t[ 0 : 2 , 0 : 3 ] > 3 ) )
print ( np. any ( t[ 0 : 2 , 0 : 3 ] > 3 ) )
[ [ 0 1 2 ]
[ 5 6 7 ] ]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
False
True
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
False
True
2,numpy.unique
import numpy as np
t = np. arange( 16 ) . clip( 5 , 8 ) . reshape( 4 , 4 )
print ( t)
print ( '-' * 50 )
change_int = t. astype( int )
print ( np. unique( change_int ) )
[ [ 5 5 5 5 ]
[ 5 5 6 7 ]
[ 8 8 8 8 ]
[ 8 8 8 8 ] ]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ 5 6 7 8 ]
五,clip裁剪
1,将数组中的数据设置为np.nan
import numpy as np
temp = np. arange( 30 ) . reshape( 6 , 5 ) . astype( float )
print ( temp)
temp[ 3 , 2 ] = np. nan
print ( temp)
2,将数组进行裁剪
import numpy as np
temp = np. arange( 30 ) . reshape( 6 , 5 ) . astype( float )
temp[ 0 , 3 ] = np. nan
print ( temp)
temp = temp. clip( 10 , 18 )
print ( temp)
[ [ 0 . 1 . 2 . nan 4 . ]
[ 5 . 6 . 7 . 8 . 9 . ]
[ 10 . 11 . 12 . 13 . 14 . ]
[ 15 . 16 . 17 . 18 . 19 . ]
[ 20 . 21 . 22 . 23 . 24 . ]
[ 25 . 26 . 27 . 28 . 29 . ] ]
[ [ 10 . 10 . 10 . nan 10 . ]
[ 10 . 10 . 10 . 10 . 10 . ]
[ 10 . 11 . 12 . 13 . 14 . ]
[ 15 . 16 . 17 . 18 . 18 . ]
[ 18 . 18 . 18 . 18 . 18 . ]
[ 18 . 18 . 18 . 18 . 18 . ] ]