一、numpy模块的综合使用方法
import numpy as np
array = np. array( [ [ 1 , 2 , 3 ] , [ 2 , 3 , 4 ] ] , np. uint8)
print ( array)
print ( array. shape)
print ( array. ndim)
print ( array. size)
a = np. array( [ 2 , 3 , 4 ] , dtype = np. int64)
a1 = np. array( [ 2 , 3 , 4 ] , dtype = np. int16)
b = np. array( [ 2 , 3 , 4 ] , dtype = np. int32)
c = np. array( [ 2 , 3 , 4 ] , dtype = np. float64)
d = np. array( [ 2 , 3 , 4 ] , dtype = np. float32)
print ( a)
print ( a1)
print ( b)
print ( c)
print ( d)
a = np. array( [ [ 1 , 2 , 3 ] , [ 2 , 3 , 4 ] ] )
print ( a)
a = np. zeros( ( 3 , 4 ) , dtype = np. int16)
b = np. ones( ( 3 , 4 ) , dtype = np. int16)
c = np. empty( ( 3 , 4 ) )
d = np. arange( 10 , 20 , 2 )
e = np. arange( 12 ) . reshape( ( 3 , 4 ) )
f = np. linspace( 1 , 10 , 6 ) . reshape( 2 , 3 )
print ( a)
print ( b)
print ( c)
print ( d)
print ( e)
print ( f)
a = np. array( [ 10 , 20 , 30 , 40 ] )
b= np. arange( 4 )
c = a- b
d = a+ b
e = 10 * np. sin( a)
print ( a, b)
print ( c)
print ( d)
print ( b** 2 )
print ( e)
print ( b)
print ( b< 3 )
print ( b== 3 )
a = np. array( [ [ 1 , 1 ] , [ 2 , 3 ] ] , np. int16)
b = np. arange( 4 ) . reshape( ( 2 , 2 ) )
c = a* b
d = np. dot( a, b)
e = a. dot( b)
print ( c)
print ( d)
print ( e)
a = np. random. random( ( 2 , 4 ) )
print ( a)
print ( np. sum ( a, axis= 1 ) )
print ( np. min ( a, axis= 0 ) )
print ( np. max ( a, axis= 1 ) )
A = np. arange( 2 , 14 ) . reshape( ( 3 , 4 ) )
print ( A)
print ( A. argmin( ) )
print ( np. argmin( A) )
print ( A. argmax( ) )
print ( np. argmax( A) )
print ( np. mean( A, axis = 0 ) )
print ( np. average( A, axis = 1 ) )
print ( A. mean( ) )
print ( np. median( A) )
print ( np. cumsum( A) )
print ( np. diff( A) )
print ( np. nonzero( A) )
A = np. arange( 14 , 2 , - 1 ) . reshape( 3 , 4 )
print ( np. sort( A) )
print ( np. transpose( A) )
print ( A. T)
print ( A. dot( A. T) )
print ( np. clip( A, 5 , 9 ) )
A = np. arange( 3 , 15 )
print ( A)
print ( A[ 3 ] )
A = np. arange( 3 , 15 ) . reshape( 3 , 4 )
print ( A) print ( A[ 2 ] )
print ( A[ 1 ] [ 1 ] )
print ( A[ 1 , 1 ] )
print ( A[ 2 , : ] )
print ( A[ : , 2 ] )
print ( A)
print ( A[ 1 , 1 : 3 ] )
for row in A: print ( row)
for column in A. T: print ( column)
print ( A)
print ( A. flat)
print ( A. flatten( ) )
for item in A. flat: print ( item)
A = np. array( [ 1 , 1 , 1 ] )
print ( A. T)
print ( A. shape)
A = np. matrix( [ 1 , 1 , 1 ] )
B = np. matrix( [ 2 , 2 , 2 ] )
print ( A. shape)
print ( B. shape)
print ( A. T)
print ( B. T)
print ( np. vstack( ( A, B) ) )
print ( np. hstack( ( A, B) ) )
print ( np. vstack( ( A, B, B, A) ) )
print ( np. vstack( ( A, B, B, A) ) . shape)
print ( np. hstack( ( A, B, B, A) ) )
print ( np. hstack( ( A, B, B, A) ) . shape)
print ( np. concatenate( ( A, B, B, A) , axis= 0 ) )
print ( np. concatenate( ( A, B, B, A) , axis= 1 ) )
A = np. arange( 12 ) . reshape( ( 3 , 4 ) )
print ( A)
print ( np. split( A, 2 , axis = 1 ) )
print ( np. split( A, 3 , axis = 0 ) )
print ( np. array_split( A, 3 , axis = 1 ) )
print ( np. vsplit( A, 3 ) )
print ( np. hsplit( A, 2 ) )
A = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
B = A
C = B
A[ 0 ] [ 1 ] = 9
print ( A)
print ( B)
print ( C)
A[ 1 , 1 : 3 ] = [ 22 , 33 ]
print ( A)
print ( B)
print ( C)
D = A. copy( )
A[ 1 ] [ 2 ] = 3
print ( A)
print ( D)