numpy快速初始化
np. array( [ 1 , 2 , 3 , 4 , 5 ] )
>> > array( [ 1 , 2 , 3 , 4 , 5 ] )
np. arange( 15 )
>> > array( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ] )
np. arange( 1 , 13 , 2 )
>> > array( [ 1 , 3 , 5 , 7 , 9 , 11 ] )
np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
>> > array( [ [ 1 , 2 , 3 ] ,
[ 4 , 5 , 6 ] ] )
np. arange( 15 ) . reshape( 3 , 5 )
>> > array( [ [ 0 , 1 , 2 , 3 , 4 ] ,
[ 5 , 6 , 7 , 8 , 9 ] ,
[ 10 , 11 , 12 , 13 , 14 ] ] )
np. linspace( 1 , 13 , 20 )
>> > array( [ 1 . , 1.63157895 , 2.26315789 , 2.89473684 ,
3.52631579 , 4.15789474 , 4.78947368 , 5.42105263 ,
6.05263158 , 6.68421053 , 7.31578947 , 7.94736842 ,
8.57894737 , 9.21052632 , 9.84210526 , 10.47368421 ,
11.10526316 , 11.73684211 , 12.36842105 , 13 . ] )
l = [ i for i in range ( 8 ) ]
np. array( l)
>> > array( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] )
numpy普通运算
a= np. array( [ 1 , 2 , 3 , 4 , 5 ] )
b= np. array( [ 2 , 2 , 2 , 2 , 2 ] )
a* 3 >> > array( [ 3 , 6 , 9 , 12 , 15 ] )
a+ b >> > array( [ 3 , 4 , 5 , 6 , 7 ] )
a* b >> > array( [ 2 , 4 , 6 , 8 , 10 ] )
a> b >> > array( [ False , False , True , True , True ] , dtype= bool )
c= array( [ False , False , True , True , True ] )
d= array( [ False , True , True , True , False ] )
c& d >> > array( [ False , False , True , True , False ] , dtype= bool )
c| d >> > array( [ False , True , True , True , True ] , dtype= bool )
~ d >> > array( [ True , False , False , False , True ] , dtype= bool )
numpy函数运算
c= array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
np. abs ( c)
np. sum ( c) >> > 21
np. sqrt( c) >> > array( [ [ 1 . , 1.41421356 , 1.73205081 ] ,
[ 2 . , 2.23606798 , 2.44948974 ] ] )
np. max ( c) >> > 6
c= np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
d= np. array( [ [ 2 , 2 , 2 ] , [ 6 , 6 , 6 ] ] )
np. maximum( c, d) >> > array( [ [ 2 , 2 , 3 ] , [ 6 , 6 , 6 ] ] )
np. mean( c) >> > 3.5
a= np. array( [ np. nan, 2 , 3 , 4 ] ) >> > array( [ nan, 2 . , 3 . , 4 . ] )
np. isnan( a) >> > array( [ True , False , False , False ] , dtype= bool )
a= np. array( [ np. inf, 2 , 3 , 4 ] ) >> > array( [ inf, 2 . , 3 . , 4 . ] )
np. isinf( a) >> > array( [ True , False , False , False ] , dtype= bool )
numpy的形状和大小
a= np. arange( 15 ) . reshape( 3 , 5 )
a. size >> > 15
a. shape >> > ( 3 , 5 )
a. dim >> > 2
numpy遍历
a= np. array( [ 1 , 2 , 3 , 4 , 5 ] )
for i in a:
print ( i)
b= np. arange( 14 ) . reshape( 2 , 7 )
for i in b:
print ( i)
>> > [ 0 1 2 3 4 5 6 ]
[ 7 8 9 10 11 12 13 ]
b= np. arange( 14 ) . reshape( 2 , 7 )
for i in b. flat:
print ( i)
>> > 1
2
3
. . .
numpy索引
a= np. arange( 14 )
a[ 3 ]
>> > 3
a= np. arange( 14 ) . reshape( 2 , 7 )
a >> > array( [ [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ,
[ 7 , 8 , 9 , 10 , 11 , 12 , 13 ] ] )
a[ 0 ] >> > array( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] )
a[ 0 ] [ 1 ] >> > 1
a[ 0 , 1 ] >> > 1
a[ 0 : 2 , 0 : 3 ] >> > array( [ [ 0 , 1 , 2 ] ,
[ 7 , 8 , 9 ] ] )
a> 3 >> > array( [ [ False , False , False , False , True , True , True ] ,
[ True , True , True , True , True , True , True ] ] )
a[ a> 3 ] >> > array( [ 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 ] )
a[ ( a> 5 ) & ( a% 2 == 0 ) ] >> > array( [ 6 , 8 , 10 , 12 ] )
a[ 0 : 2 , 1 : 4 ] >> > array( [ [ 1 , 2 , 3 ] ,
[ 8 , 9 , 10 ] ] )
a[ 0 , [ 1 , 2 , 3 ] ] >> > array( [ 1 , 2 , 3 ] )
a[ 0 , a[ 0 ] > 3 ] >> > array( [ 4 , 5 , 6 ] )