numpy筆記

經過下標範圍獲取的新的數組是原始數組的一個視圖。它與原始數組共享同一塊數據空間,會一塊兒修改
>>> b = a[3:7] # 經過下標範圍產生一個新的數組b,b和a共享同一塊數據空間
>>> b
array([101, 4, 5, 6])
>>> b[2] = -10 # 將b的第2個元素修改成-10
>>> b
array([101, 4, -10, 6])
>>> a # a的第5個元素也被修改成10
array([ 0, 1, 100, 101, 4, -10, 6, 7, 8, 9])
當使用整數序列對數組元素進行存取時,將使用整數序列中的每一個元素做爲下標,整數序列能夠是列
表或者數組。使用整數序列做爲下標得到的數組不和原始數組共享數據空間。
>>> x = np.arange(10,1,-1)
>>> x
array([10, 9, 8, 7, 6, 5, 4, 3, 2])
>>> x[[3, 3, 1, 8]] # 獲取x中的下標爲3, 3, 1, 8的4個元素,組成一個新的數組
array([7, 7, 9, 2])
>>> b = x[np.array([3,3,-3,8])] #下標能夠是負數
>>> b[2] = 100
>>> b
array([7, 7, 100, 2])
>>> x # 因爲b和x不共享數據空間,所以x中的值並無改變
array([10, 9, 8, 7, 6, 5, 4, 3, 2])
>>> x[[3,5,1]] = -1, -2, -3 # 整數序列下標也能夠用來修改元素的值
>>> x
array([10, -3, 8, -1, 6, -2, 4, 3, 2])
當使用布爾數組b做爲下標存取數組x中的元素時,將收集數組x中全部在數組b中對應下標爲True的
元素。使用布爾數組做爲下標得到的數組不和原始數組共享數據空間
#多維數組
a=np.arange(0, 60, 10).reshape(-1, 1) + np.arange(0, 6)
array([[ 0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55]])
>>> a[3:,[3,5]]
array([[33, 35],
       [43, 45],
       [53, 55]])
#結構體
>>> persontype = np.dtype({
'names':['name', 'age', 'weight'],
'formats':['S32','i', 'f']})
# S32 : 32個字節的字符串類型,因爲結構中的每一個元素的大小必須固定,所以須要指定字符串的長度
# i : 32bit的整數類型,至關於np.int32
# f : 32bit的單精度浮點數類型,至關於np.float32
>>> persontype
dtype([('name', 'S32'), ('age', '<i4'), ('weight', '<f4')])
>>> a = np.array([("Zhang",32,75.5),("Wang",24,65.2)],
dtype=persontype)
>>> a
array([('Zhang', 32, 75.5), ('Wang', 24, 65.19999694824219)],
      dtype=[('name', 'S32'), ('age', '<i4'), ('weight', '<f4')])
>>> a["name"]
array(['Zhang', 'Wang'],
      dtype='|S32')
>>> a[['name','age']]
array([('Zhang', 32), ('Wang', 24)],
      dtype=[('name', 'S32'), ('age', '<i4')])
>>> a['age']+200
array([232, 224])
>>> a['name'][0]='cao'
>>> a
array([('cao', 32, 75.5), ('Wang', 24, 65.19999694824219)],
      dtype=[('name', 'S32'), ('age', '<i4'), ('weight', '<f4')])
#ufunc運算
x = np.linspace(0, 2*np.pi, 10) # 等差數列
>>> np.logspace(0, 2, 20) # 等比數列產生1(10^0)到100(10^2)、有20個元素的等比數列:
array([ 1. , 1.27427499, 1.62377674, 2.06913808,
2.6366509 , 3.35981829, 4.2813324 , 5.45559478,
6.95192796, 8.8586679 , 11.28837892, 14.38449888,
18.32980711, 23.35721469, 29.76351442, 37.92690191,
48.32930239, 61.58482111, 78.47599704, 100. ])

>>> x = np.linspace(0, 20, 11)
>>> x
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.])
>>> len(x)
11
>>> y=np.sin(x)
>>> z=np.sqrt(x)
>>> y
array([ 0. , 0.90929743, -0.7568025 , -0.2794155 , 0.98935825,
       -0.54402111, -0.53657292, 0.99060736, -0.28790332, -0.75098725,
        0.91294525])
>>> z
array([ 0. , 1.41421356, 2. , 2.44948974, 2.82842712,
        3.16227766, 3.46410162, 3.74165739, 4. , 4.24264069,
        4.47213595])
>>> x
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.])
>>> np.sin(x,x) #將sin函數所計算的結果直接覆蓋到數組x上去的話,能夠將要被覆蓋的數組做爲第二個參數傳遞給ufunc函數。
array([ 0. , 0.90929743, -0.7568025 , -0.2794155 , 0.98935825,
       -0.54402111, -0.53657292, 0.99060736, -0.28790332, -0.75098725,
        0.91294525])
>>> x
array([ 0. , 0.90929743, -0.7568025 , -0.2794155 , 0.98935825,
       -0.54402111, -0.53657292, 0.99060736, -0.28790332, -0.75098725,
        0.91294525])
>>> np.abs(x)
array([ 0. , 0.90929743, 0.7568025 , 0.2794155 , 0.98935825,
        0.54402111, 0.53657292, 0.99060736, 0.28790332, 0.75098725,
        0.91294525])
>>> x
array([ 0. , 0.90929743, -0.7568025 , -0.2794155 , 0.98935825,
       -0.54402111, -0.53657292, 0.99060736, -0.28790332, -0.75098725,
        0.91294525])
>>> np.abs(x,x) #同理,覆蓋。
array([ 0. , 0.90929743, 0.7568025 , 0.2794155 , 0.98935825,
        0.54402111, 0.53657292, 0.99060736, 0.28790332, 0.75098725,
        0.91294525])
>>> x
array([ 0. , 0.90929743, 0.7568025 , 0.2794155 , 0.98935825,
        0.54402111, 0.53657292, 0.99060736, 0.28790332, 0.75098725,
        0.91294525])
numpy的ufunc能夠對數組直接進行計算,因此np.sin()比for ... math.sin()要快
然而,單個值計算時,np.sin(0.5) 比 math.sin(0.5) 慢。能夠把np當作批量操做。
>>> a = np.arange(0,4)
>>> b = np.arange(1,5)
>>> a+b
array([1, 3, 5, 7])
>>> np.add(a,b)
array([1, 3, 5, 7])
>>> np.add(a,b,c)

Traceback (most recent call last):
  File "<pyshell#139>", line 1, in <module>
    np.add(a,b,c)
ValueError: operands could not be broadcast together with shapes (4) (4) (100)
>>> np.add(a,b,a) #覆蓋a
array([1, 3, 5, 7])
>>> a
array([1, 3, 5, 7])
>>> a=[1,2,3,4]
>>> b=[2,3,4,5]
>>> a+b #python自帶數組的+
[1, 2, 3, 4, 2, 3, 4, 5]
>>> np.add(a,b)
array([3, 5, 7, 9])
#運算符
y = x1 + x2: add(x1, x2 [, y])
y = x1 - x2: subtract(x1, x2 [, y])
y = x1 * x2: multiply (x1, x2 [, y])
y = x1 / x2: divide (x1, x2 [, y]), 若是兩個數組的元素爲整數,那麼用整數除法
y = x1 / x2: true divide (x1, x2 [, y]), 老是返回精確的商
y = x1 // x2: floor divide (x1, x2 [, y]), 老是對返回值取整
y = -x: negative(x [,y])
y = x1**x2: power(x1, x2 [, y])
y = x1 % x2: remainder(x1, x2 [, y]), mod(x1, x2, [, y])

2.2.1 廣播
當咱們使用ufunc函數對兩個數組進行計算時,ufunc函數會對這兩個數組的對應元素進行計算,因
此它要求這兩個數組有相同的大小(shape相同)。若是兩個數組的shape不一樣的話,會進行以下的廣播
(broadcasting)處理:
1. 讓全部輸入數組都向其中shape最長的數組看齊,shape中不足的部分都經過在前面加1補齊
2. 輸出數組的shape是輸入數組shape的各個軸上的最大值
3. 若是輸入數組的某個軸和輸出數組的對應軸的長度相同或者其長度爲1時,這個數組可以用來計
算,不然出錯
4. 當輸入數組的某個軸的長度爲1時,沿着此軸運算時都用此軸上的第一組值
>>> a = np.arange(0, 60, 10).reshape(-1, 1)
>>> a
array([[ 0], [10], [20], [30], [40], [50]])
>>> a.shape
(6, 1)
>>> b = np.arange(0, 5)
>>> b
array([0, 1, 2, 3, 4])
>>> b.shape
(5,)
>>> c = a + b
>>> c
array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[20, 21, 22, 23, 24],
[30, 31, 32, 33, 34],
[40, 41, 42, 43, 44],
[50, 51, 52, 53, 54]])
>>> c.shape
(6, 5)

#矩陣
>>> a = np.matrix([[1,2,3],[5,5,6],[7,9,9]])
>>> a**-1 #逆矩陣
matrix([[-0.6 , 0.6 , -0.2 ],
        [-0.2 , -0.8 , 0.6 ],
        [ 0.66666667, 0.33333333, -0.33333333]])
>>> a*a**-1
matrix([[ 1.00000000e+00, 0.00000000e+00, -5.55111512e-17],
        [ 4.44089210e-16, 1.00000000e+00, -1.11022302e-16],
        [ 4.44089210e-16, 0.00000000e+00, 1.00000000e+00]])
#寫文件,讀 。維度會變成一維的
tofile能夠方便地將數組中數據以二進制的格式寫進文件。tofile輸出的數據沒有格
式,所以用numpy.fromfile讀回來的時候須要本身格式化數據:
>>> a = np.arange(0,12)
>>> a.shape = 3,4
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a.tofile("a.bin")
>>> b = np.fromfile("a.bin", dtype=np.float) # 按照float類型讀入數據
>>> b # 讀入的數據是錯誤的
array([ 2.12199579e-314, 6.36598737e-314, 1.06099790e-313,
1.48539705e-313, 1.90979621e-313, 2.33419537e-313])
>>> a.dtype # 查看a的dtype
dtype('int32')
>>> b = np.fromfile("a.bin", dtype=np.int32) # 按照int32類型讀入數據
>>> b # 數據是一維的
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b.shape = 3, 4 # 按照a的shape修改b的shape
>>> b # 此次終於正確了
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a.tofile("d:\\a1.bin",sep='#') #數組將以文本格式輸入輸出。,以#分隔。
>>> np.save("d:\\a.npy", a) #二進制
>>> c = np.load( "d:\\a.npy" )#維度未丟失,且不用設置dtype
>>> c
array([[ 0, 1, 2, 3],
       [ 4, 5, 6, 7],
       [ 8, 9, 10, 11]])
#np.savez() 存儲多個數組
>>> a = np.array([[1,2,3],[4,5,6]])
>>> b = np.arange(0, 1.0, 0.1)
>>> c = np.sin(b)
>>> np.savez("result.npz", a, b, sin_array = c)
>>> r = np.load("result.npz")
>>> r["arr_0"] # 數組a
array([[1, 2, 3],
[4, 5, 6]])
>>> r["arr_1"] # 數組b
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
>>> r["sin_array"] # 數組c
array([ 0. , 0.09983342, 0.19866933, 0.29552021, 0.38941834,
0.47942554, 0.56464247, 0.64421769, 0.71735609, 0.78332691])
若是你用解壓軟件打開result.npz文件的話,會發現其中有三個文件:arr_0.npy, arr_1.npy,
sin_array.npy,其中分別保存着數組a, b, c的內容。
# 讀寫txt文件
使用numpy.savetxt和numpy.loadtxt能夠讀寫1維和2維的數組:
>>> a = np.arange(0,12,0.5).reshape(4,-1)
>>> np.savetxt("a.txt", a) # 缺省按照'%.18e'格式保存數據,以空格分隔
>>> np.loadtxt("a.txt")
array([[ 0. , 0.5, 1. , 1.5, 2. , 2.5],
[ 3. , 3.5, 4. , 4.5, 5. , 5.5],
[ 6. , 6.5, 7. , 7.5, 8. , 8.5],
[ 9. , 9.5, 10. , 10.5, 11. , 11.5]])
>>> np.savetxt("a.txt", a, fmt="%d", delimiter=",") #改成保存爲整數,以逗號分隔
>>> np.loadtxt("a.txt",delimiter=",") # 讀入的時候也須要指定逗號分隔
array([[ 0., 0., 1., 1., 2., 2.],
[ 3., 3., 4., 4., 5., 5.],
[ 6., 6., 7., 7., 8., 8.],
[ 9., 9., 10., 10., 11., 11.]])

本節介紹所舉的例子都是傳遞的文件名,也能夠傳遞已經打開的文件對象,例如對於load和save
函數來講,若是使用文件對象的話,能夠將多個數組儲存到一個npy文件中:
>>> a = np.arange(8)
>>> b = np.add.accumulate(a)
>>> c = a + b
>>> f = file("result.npy", "wb")
>>> np.save(f, a) # 順序將a,b,c保存進文件對象f
>>> np.save(f, b)
>>> np.save(f, c)
>>> f.close()
>>> f = file("result.npy", "rb")
>>> np.load(f) # 順序從文件對象f中讀取內容
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> np.load(f)
array([ 0, 1, 3, 6, 10, 15, 21, 28])
>>> np.load(f)
array([ 0, 2, 5, 9, 14, 20, 27, 35])
相關文章
相關標籤/搜索