1、NumPy包含的內容
1、ndarrray,高效的多維數組,提供了基于數組的便捷算術操作以及靈活的廣播功能;
2、對所有數組對象進行快速的矩陣計算,而無需編寫循環;
3、提供對硬盤中的數據的讀寫工具,并對內存映射文件進行操作;
4、可實現線性變換、隨機數生成以及傅里葉變換功能;
特點:NumPy在內部將數據存儲在連續的內存塊上,這與其他的Python內建序列是不同的,這使得NumPy數組對象的計算速度比其他同內容同操作的對象快(快10—100倍)。
?
2、每個ndarray對象具備的屬性
1、shape屬性用來描述數組的維數;
2、dtype屬性描述數組的數據類型,它包含了ndarray需要為某一種類型數據所申明的內存塊信息,是NumPy能夠與其他系統數據靈活交互的原因;
1 import numpy as np 2 data = np.random.randn(2,3) 3 data.shape 4 Out[14]: 5 》(2, 3) 6 data.dtype 7 Out[15]: 8 》dtype('float64')
?
3、生成ndarray
1、array函數,array函數接受任意的序列型對象(包括數組),生成一個包含傳遞數據的NumPy數組;接受嵌套序列時,自動轉換成多維數組。
1 data1 = [1,2,7.9,0,1] 2 arr1 = np.array(data1) 3 arr1 4 Out[18]: 5 array([1. , 2. , 7.9, 0. , 1. ]) 6 data2 = [[1,2,3],[1,2,3]] 7 np.array(data2) # 接受嵌套序列,自動轉成二維數組 8 Out[20]: 9 》array([[1, 2, 3], 10 [1, 2, 3]])
2、zeros()、ones()、empty()、full()等函數,注意zeros()、ones()、empty()等函數接受的參數只有一個,如果要生成多維數組,則需要為shape傳遞一個元組指定維數;
3、ones_like()根據所給的數據數組生成一個形狀一摸一樣的全1數組;zeros_like(),empty_like()、full_like()也類似;
4、arange()生成python內建函數range的數組版,返回一個數組。
?
4、ndarray數據類型
1、python數據數據類型眾多,可以使用astype方法顯示地轉換數組地數據類型,注意:浮點型轉整型時,小數點后部分將被消除。使用astype時,返回一個新的數組,而原數組不變。
1 arr = np.array([1, 2, 3, 4, 5]) 2 print(arr.dtype) 3 float_arr = arr.astype(np.float64) 4 print(float_arr.dtype) 5 》》int32 6 》》float64
2、如果一個數組里面地元素都是表達數字含義地字符串,也可以將字符串轉換成數字;
1 numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_) 2 numeric_strings 3 Out[30]: 4 >>array([b'1.25', b'-9.6', b'42'], dtype='|S4') 5 numeric_strings.astype(np.float) 6 Out[31]: 7 >>array([ 1.25, -9.6 , 42. ])
?
5、NumPy數組算術
數組之間可以直接進行批量操作而無需任何for循環;
任何在兩個等尺寸數組之間的算術操作都運用了逐元素操作的方式;
帶有標量的算術操作,會把計算參數傳遞給每一個元素;
同尺寸數組之間的比較,會產生一個布爾類型的數組;
?
6、數組索引與切片
1、數組的索引與列表的索引類似,但不相同。區別于python數組的內建列表,數組的切片是原數組的視圖,即數組并不是被復制了,任何對于視圖的修改都會反應到原數組上。
1 arr = np.arange(10) 2 arr[5:8] = 12 3 arr_slice = arr[5:8] 4 print(arr_slice) 5 arr_slice[1] = 12345 # 對視圖的修改后,原數組也會被改變 6 arr 7 [12 12 12] 8 Out[38]: 9 array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 10 9])
2、如果相對數組進行復制而不是得到一份視圖,應該使用copy方法,eg:arr[5:8].copy()
3、多維數組的索引方法,以二維數組為例,數組名[index][index]或者數組名[index,index]的方式。
?
7、布爾索引
1、在索引數組時,可以傳入布爾值數組,返回的是True對應的內容。
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe']) data = np.random.randn(7, 4) names == 'Bob' Out[41]: array([ True, False, False, True, False, False, False]) data[names == 'Bob'] Out[42]: array([[-0.71491699, 0.40102409, -0.42140722, -1.50136196],[ 0.21920979, -0.13960939, 1.60586575, 0.0712131 ]])
2、基于常識來設置布爾數組的值也是可行的。
1 data = np.random.randn(7, 4) 2 data 3 Out[44]: 4 >>array([[-0.83434737, -0.67205305, 0.17626815, -0.60448911], 5 [ 0.30011278, -0.98530314, -0.58816207, 2.40943742], 6 [-0.94236761, 1.12991724, -0.4361433 , 0.75806253], 7 [-0.7228912 , 1.1955933 , -0.75127874, -0.73905711], 8 [-0.4128283 , -0.15726642, 0.86381129, -1.2467569 ], 9 [-0.3290692 , -1.03838623, 0.68320058, -0.58237692], 10 [ 1.40461917, 0.55720836, 0.39822819, 0.64182056]]) 11 data[data < 0] = 0 12 data 13 Out[46]: 14 >>array([[0. , 0. , 0.17626815, 0. ], 15 [0.30011278, 0. , 0. , 2.40943742], 16 [0. , 1.12991724, 0. , 0.75806253], 17 [0. , 1.1955933 , 0. , 0. ], 18 [0. , 0. , 0.86381129, 0. ], 19 [0. , 0. , 0.68320058, 0. ], 20 [1.40461917, 0.55720836, 0.39822819, 0.64182056]])
8、reshape函數,格式 reshape(a, newshape, order='C')
Parameters
----------
a : array_like
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {'C', 'F', 'A'}, optional
1 a = np.arange(6) 2 a 3 Out[49]: 4 array([0, 1, 2, 3, 4, 5]) 5 a.reshape(3,2) 6 Out[50]: 7 array([[0, 1], 8 [2, 3], 9 [4, 5]])
?
9、數組轉置和換軸
1、轉置是一種特殊的數據重組形式,也可以返回底層數據的視圖而不需要復制任何內容,數組擁有transpose方法,也擁有T屬性。當數組是一維或者二維時,數組名.T的形式直接返回轉置后的數組(原來的數組不會修改),
1 arr = np.arange(15).reshape((3, 5))2 arr3 Out[54]: 4 array([[ 0, 1, 2, 3, 4],5 [ 5, 6, 7, 8, 9],6 [10, 11, 12, 13, 14]]) 7 arr.T 8 Out[55]: 9 array([[ 0, 5, 10], 10 [ 1, 6, 11], 11 [ 2, 7, 12], 12 [ 3, 8, 13], 13 [ 4, 9, 14]])
2、對于更高維的數組,轉置使用transpose和swapxes方法。