常用小操作
arr1 = np. array( [ [ 1 , 2 , 3 ] ,
[ 1 , 2 , 3 ] ,
[ 4 , 5 , 6 ] ] )
arr2 = np. array( [ [ 1 , 1 , 2 ] ,
[ 1 , 2 , 3 ] ,
[ 2 , 3 , 3 ] ] )
x = np. dot( arr1, arr2)
y = arr1. dot( arr2)
print ( arr1. T)
np. random. random( size= ( 3 , 2 ) )
np. random. randint( 0 , 10 , size= ( 3 , 2 ) )
np. random. random( ( 3 , 2 ) )
np. sum ( arr1)
np. min ( arr1)
np. max ( arr1)
np. sum ( arr1, axis= 0 )
np. sum ( arr1, axis= 1 )
np. argmin( arr1)
np. argmax( arr1)
np. mean( arr1)
arr1. mean( )
np. abs ( arr1)
np. median( arr1)
np. sqrt( arr1)
np. sort( arr1)
np. argsort( arr1)
a = np. array( [ 2 , 4 , 6 , 8 , 10 ] )
a1 = np. where( a > 5 )
p = [ [ 7 , 0 , 0 , 5 , 8 , 6 , 1 , 5 , 5 , 0 ] ]
a = np. clip( p, 2 , 7 )
a = np. array( [ - 1.7 , - 1.5 , - 0.2 , 0.2 , 1.5 , 1.7 , 2.0 ] )
np. ceil( a)
结果:[ - 1. - 1. - 0. 1. 2. 2. 2. ]
a. std( )
np. exp( x)
arr1. tolist( )
l1 = np. asarray( [ [ 1 , 2 , 3 ] ] )
l2 = np. squeeze( l1)
print ( l1. shape)
print ( l2. shape)
l1 = np. array( [ [ 1 , 2 , - 10 ] ,
[ 4 , 5 , - 6 ] ] )
l2 = np. array( [ [ 1 , 9 , - 10 ] ,
[ 4 , 5 , - 6 ] ] )
b1 = np. maximum( l1, l2)
print ( b1)
b2 = np. maximum( l1, 0 )
print ( b2)
保存与读取np文件
import numpy as np
l1 = np. array( [ [ 1 , 2 , - 10 ] ,
[ 4 , 5 , - 6 ] ] )
l2 = np. array( [ [ 1 , 9 , - 10 ] ,
[ 4 , 5 , - 6 ] ] )
np. savez( './Data.npz' , l1, l2)
np. savez( './Data.npz' , name1= l1, name2= l2)
arr = np. load( './Data.npz' )
a1 = arr[ 'arr_0' ]
a2 = arr[ 'arr_1' ]
print ( a1)
print ( a2)
with arr as f:
print ( f. files)
显示完整数组
import numpy as np
np. set_printoptions( threshold= np. inf)
去重排序(小-大),并变为1维
label_list = np. array( [ [ [ 1 , 2 , 3 ] , [ 6 , 5 , 4 ] ] ] )
print ( np. unique( label_list) )
生成数组
np_ = np. arange( 1 , 8 , 1 )
np. zeros( ( 5 , 8 ) , dtype= 'int32' )
cry = np. ones( ( 5 , 8 ) , dtype= 'int32' )
gry0 = np. zeros_like( cry)
gry1 = np. ones_like( cry)
查看属性
array = np. array( [ [ 1 , 2 , 3 , ] ,
[ 4 , 5 , 6 ] ,
[ 7 , 8 , 9 ] ] )
print ( array. ndim)
print ( array. shape)
print ( array. size)
print ( array. dtype)
print ( array[ 0 , 0 ] )
print ( array[ 0 ] [ 0 ] )
np_ = np. arange( 1 , 8 , 1 )
np_. shape = ( 2 , 4 )
np_1 = np. arange( 1 , 9 )
np_1. shape = ( 2 , 2 , 2 )
a = array. astype( 'float64' )
. astype( np. uint8)
f = np. array( [ '2013-01-01 01:01:01' ] )
f = f. astype( 'datetime64' )
f = f. astype( 'datetime64[Y]' )
版本过高,去除警告的方法
import warnings
warnings. filterwarnings( 'ignore' )
Numpy的内部基本数据类型
类型名 类型表示符 布尔型 bool_ 有符号整数型 int8(-128~127) / int16 / int32 / int64 无符号整数型 uint8(0~255) / uint16 / uint32 / uint64 浮点型 float16 / float32 / float64 复数型 complex64 / complex128 字串型 str_,每个字符用32位Unicode编码表示 日期类型 datetime64 字符串型:1字节=8位 类型字符码
类型 字符码 np.bool_ ? np.int8/16/32/64 i1 / i2 / i4 / i8 np.uint8/16/32/64 u1 / u2 / u4 / u8 np.float/16/32/64 f2 / f4 / f8 np.complex64/128 c8 / c16 np.str_ U np.datetime64 M8[Y] M8[M] M8[D] M8[h] M8[m] M8[s]
自定义复合类型
data = [
( 'zs' , [ 12 , 23 , 34 ] , 54 ) ,
( 'ls' , [ 11 , 24 , 53 ] , 77 ) ,
( 'ww' , [ 12 , 23 , 63 ] , 15 )
]
data = np. array( data, dtype= [ ( 'name' , 'str' , 2 ) ,
( 'score' , 'int32' , 3 ) ,
( 'age' , 'int32' , 1 ) ,
] )
print ( data[ 1 ] [ 'score' ] )
data = np. array( data, dtype= { 'names' : [ 'name' , 'score' , 'age' ] ,
'formats' : [ 'U2' , '3int32' , 'int32' ] } )
print ( data[ 1 ] [ 'score' ] )
视图变维
a = np. arange( 1 , 9 )
b = a. reshape( 2 , 4 )
c = b. reshape( 2 , 2 , 2 )
d = c. ravel( )
a. shape = ( 2 , 4 )
a. resize( 2 , 2 , 2 )
数组掩码
a = np. arange( 1 , 9 )
a1 = a > 3
print ( a1)
res = np. arange( 1 , 101 )
print ( res[ ( res % 3 == 0 ) & ( res % 7 == 0 ) ] )
res = np. array( [ '物品1' , '物品2' , '物品3' ] )
print ( res[ [ 2 , 1 , 0 ] ] )
mask = [ 0 , 0 , 0 , 1 , 2 , 2 , 1 , 1 , 0 , 1 , 0 ]
np. array( mask)
print ( res[ mask] )
合并数组
arr1 = np. array( [ 1 , 2 , 3 ] )
arr2 = np. array( [ 4 , 5 , 6 ] )
arr3 = np. vstack( ( arr1, arr2) )
arr4 = np. hstack( ( arr1, arr2) )
arr5 = np. vstack( ( arr1, arr2, arr3) )
arr = np. concatenate( ( arr1, arr2, arr1) )
arr = np. concatenate( ( arr3, arr5) , axis= 0 )
arr1_3 = np. atleast_2d( arr1)
分割数组
arr1 = np. arange( 12 ) . reshape( 3 , 4 )
arr2, arr3 = np. split( arr1, 2 , axis= 1 )
arr4, arr5, arr6 = np. split( arr1, 3 , axis= 0 )
arr7, arr8, arr9 = np. array_split( arr1, 3 , axis= 1 )
arrv1, arrv2, arrv3 = np. vsplit( arr1, 3 )
arrv4, arrv5 = np. hsplit( arr1, 2 )
np迭代元素
arr2 = np. arange( 2 , 14 ) . reshape( 3 , 4 )
for i in arr2:
print ( i)
for i in arr2. T:
print ( i)
for i in arr2. flat:
print ( i)
基础概率分布
连续数据:在一定范围内 ,任意数据----无限可能
离散数据:在一定选项中,任意某个选项----有限选项
np. random. normal( 172 , 7 , 2000 )
x = np. random. binomial( 10 , 0.3 , 100 )
x1 = x[ x == 1 ] . size / x. size
x = np. random. hypergeometric( 6 , 4 , 3 , 1000 )