1 基本操作
1.1数组创建
import numpy as np
l = [ 1 , 2 , 3 , 4 , 5 ]
nd1 = np. array( l)
print ( nd1)
display( nd1)
[1 2 3 4 5]
array([1, 2, 3, 4, 5])
nd2 = np. zeros( shape = ( 3 , 4 ) , dtype = np. int16)
nd2
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int16)
nd3 = np. ones( shape = ( 3 , 5 ) , dtype= np. float32)
nd3
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], dtype=float32)
nd4 = np. full( shape = ( 3 , 4 , 5 ) , fill_value= 3.1415926 )
nd4
array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],
[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],
[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])
nd5 = np. random. randint( 0 , 100 , size = 20 )
nd5
array([88, 68, 55, 25, 95, 45, 53, 52, 94, 48, 38, 41, 12, 66, 18, 38, 15,
53, 15, 86])
nd6 = np. random. rand( 3 , 5 )
nd6
array([[0.1805577 , 0.97917756, 0.09591097, 0.07785931, 0.48850737],
[0.89135262, 0.37393773, 0.30382903, 0.75894887, 0.27816675],
[0.75702185, 0.22346079, 0.68415817, 0.40806391, 0.01608321]])
nd7 = np. random. randn( 3 , 5 )
display( nd7)
array([[ 0.45444319, 0.91608409, -0.32166309, -1.03094546, -0.46488153],
[-1.31852167, 0.13591764, 0.7220875 , 1.40014984, 0.18295841],
[-0.22589557, -0.94451934, 1.25952318, -1.72418753, 0.65093392]])
nd8 = np. random. normal( loc = 175 , scale = 10 , size = ( 3 , 5 ) )
print ( nd8)
[[158.01782738 178.97018664 180.98631405 172.14661692 175.47228488]
[174.02104608 182.14529853 146.28615046 183.56698055 173.92432236]
[185.93646469 180.11427498 188.36019552 183.88039966 182.85760538]]
nd9 = np. arange( 1 , 100 , step = 10 )
nd9
array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
nd10 = np. linspace( 1 , 100 , num = 19 )
nd10
array([ 1. , 6.5, 12. , 17.5, 23. , 28.5, 34. , 39.5, 45. ,
50.5, 56. , 61.5, 67. , 72.5, 78. , 83.5, 89. , 94.5,
100. ])
查看数组属性
import numpy as np
nd = np. random. randn( 5 , 3 )
nd
array([[ 0.12642702, 0.74807666, 2.42032545],
[-0.27449595, -0.68078987, 0.27759778],
[ 1.15223642, -0.63332573, 0.28521052],
[ 0.39521598, 0.6202027 , 0.90567908],
[ 0.31221798, -0.19330004, -0.42587603]])
nd. shape
(5, 3)
nd. dtype
dtype('float64')
nd. size
15
nd. ndim
2
nd. itemsize
8
文件读写
nd1 = np. random. randint( 0 , 100 , size = ( 3 , 5 ) )
nd2 = np. random. randn( 3 , 5 )
display( nd1, nd2)
array([[22, 32, 86, 19, 24],
[58, 80, 2, 38, 96],
[82, 47, 28, 32, 96]])
array([[-0.04839046, 0.20762731, 1.70801699, -0.41054106, 1.33676572],
[-1.25124635, 0.59099474, -0.74312281, 1.09247369, -0.29597477],
[ 0.90854543, 0.96096231, -1.20553854, 0.9696578 , -0.12783873]])
np. save( './data' , nd1)
np. load( './data.npy' )
array([[22, 32, 86, 19, 24],
[58, 80, 2, 38, 96],
[82, 47, 28, 32, 96]])
np. savez( './data.npz' , a = nd1, abc = nd2)
data = np. load( './data.npz' )
data
<numpy.lib.npyio.NpzFile at 0x7fe3d0869910>
data[ 'a' ]
array([[22, 32, 86, 19, 24],
[58, 80, 2, 38, 96],
[82, 47, 28, 32, 96]])
data[ 'abc' ]
array([[-0.04839046, 0.20762731, 1.70801699, -0.41054106, 1.33676572],
[-1.25124635, 0.59099474, -0.74312281, 1.09247369, -0.29597477],
[ 0.90854543, 0.96096231, -1.20553854, 0.9696578 , -0.12783873]])
data[ 'www' ]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-15-a7937d03f88c> in <module>
----> 1 data['www']
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numpy/lib/npyio.py in __getitem__(self, key)
257 return self.zip.read(key)
258 else:
--> 259 raise KeyError("%s is not a file in the archive" % key)
260
261
KeyError: 'www is not a file in the archive'
np. savez_compressed( './data2.npz' , x = nd1, y = nd2)
np. load( './data2.npz' ) [ 'x' ]
array([[22, 32, 86, 19, 24],
[58, 80, 2, 38, 96],
[82, 47, 28, 32, 96]])
np. savetxt( fname = './data.txt' ,
X = nd1,
fmt= '%0.2f' ,
delimiter= ',' )
np. savetxt( fname = './data.cvs' ,
X = nd1,
fmt= '%d' ,
delimiter= ';' )
np. loadtxt( './data.cvs' , delimiter= ';' )
array([[22., 32., 86., 19., 24.],
[58., 80., 2., 38., 96.],
[82., 47., 28., 32., 96.]])
np. loadtxt( './data.txt' , delimiter= ',' )
array([[22., 32., 86., 19., 24.],
[58., 80., 2., 38., 96.],
[82., 47., 28., 32., 96.]])
数据类型
np. array( [ 2 , 4 , 7 ] , dtype = np. int8)
array([2, 4, 7], dtype=int8)
np. array( [ - 3 , - 7 , 255 , 108 , 0 , 256 ] , dtype = np. uint8)
array([253, 249, 255, 108, 0, 0], dtype=uint8)
np. random. randint( 0 , 100 , size = 10 , dtype = 'int64' )
array([59, 9, 31, 47, 90, 84, 89, 10, 85, 13])
nd = np. random. rand( 10 , 2 )
nd
array([[0.08697582, 0.01856716],
[0.28368212, 0.01316251],
[0.20035117, 0.6809994 ],
[0.31315228, 0.40969755],
[0.79286082, 0.24707183],
[0.60774558, 0.32297674],
[0.54115188, 0.89123677],
[0.99941816, 0.15384657],
[0.52934283, 0.61635984],
[0.34174069, 0.30613281]])
nd. dtype
dtype('float64')
np. asarray( nd, dtype = 'float16' )
array([[0.087 , 0.01857],
[0.2837 , 0.01316],
[0.2003 , 0.681 ],
[0.3132 , 0.4097 ],
[0.793 , 0.2471 ],
[0.608 , 0.323 ],
[0.541 , 0.891 ],
[0.9995 , 0.1538 ],
[0.5293 , 0.616 ],
[0.3418 , 0.3062 ]], dtype=float16)
nd. astype( dtype = np. float16)
array([[0.087 , 0.01857],
[0.2837 , 0.01316],
[0.2003 , 0.681 ],
[0.3132 , 0.4097 ],
[0.793 , 0.2471 ],
[0.608 , 0.323 ],
[0.541 , 0.891 ],
[0.9995 , 0.1538 ],
[0.5293 , 0.616 ],
[0.3418 , 0.3062 ]], dtype=float16)
nd = np. random. randn( 1000 , 3 )
np. save( './data1' , nd)
np. save( './data2' , nd. astype( 'float16' ) )
nd2 = np. array( list ( 'abcdefghi' ) )
nd2
array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='<U1')
nd2. dtype
dtype('<U1')
数组运算
基本运算
nd1 = np. random. randint( 0 , 10 , size =