paython(numpy常用函式)
阿新 • • 發佈:2018-12-05
小弟峰迴路轉,開始學習python,介紹一下numpy的常用函式,如覺得不全,下方留言!(圖是小弟辛勤所畫,希望對我們有用)
import numpy as np world_alcohol = np.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1) print(world_alcohol) print("~~~~~~~~~~~~~~~~~") print(type(world_alcohol)) numbers = np.array([1, 2, 3, 4]) numbers.dtype vector = np.array([5, 10, 15, 20]) equal_to_ten = (vector == 10) print (equal_to_ten) print("~~~~~~~~~~~~~~~~~") print(vector[equal_to_ten]) vector = np.array(["1", "2", "3"]) print(vector.dtype) print("~~~~~~~~~~~~~~~~~") print(vector) print("~~~~~~~~~~~~~~~~~") vector = vector.astype(float) print(vector.dtype) print("~~~~~~~~~~~~~~~~~") print(vector) matrix = np.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix.sum(axis=1)) print("~~~~~~~~~~~~~~~~~") print(matrix.sum(axis=0)) a = np.arange(15).reshape(3, 5) print(a) print("~~~~~~~~~~~~~~~~~") print(a.shape) print("~~~~~~~~~~~~~~~~~") print(a.ndim) #顯示這個矩陣的維度 print(a.dtype) print(a.size) print("~~~~~~~~~~~~~~~~~") b = np.zeros((3,4)) print(b) c = np.ones((2,3,4),dtype=np.int32 ) #產生了一個三維的單位矩陣 print("~~~~~~~~~~~~~~~~~") print(c) print(np.arange(10,30,5)) #建立一個矩陣,以10為起點,以5為間隔,30為終點 print("~~~~~~~~~~~~~~~~~") from numpy import pi #這裡要用到pi,要從numpy中匯入,可在這裡匯入的時候居然不能用它的小名np print(np.linspace(0,2*pi,10 )) #從0到2*pi之間等間距的插入10個數 print("~~~~~~~~~~~~~~~~~") print(np.sin(np.linspace(0,2*pi,10))) #矩陣的點乘和叉乘 A = np.array([[1,1],[0,1]]) B = np.array([[2,0],[3,4]]) print(A) print("~~~~~~~~~~~~~~~~~") print(B) print("~~~~~~~~~~~~~~~~~") print(A*B) print("~~~~~~~~~~~~~~~~~") print(A.dot(B)) # A.dot(B)這個寫法等價於np.dot(A, B) C = np.arange(3) print(C) print("~~~~~~~~~~~~~~~~~") print(np.exp(C)) print("~~~~~~~~~~~~~~~~~") print(np.sqrt(C)) a = np.floor(10*np.random.random((3,4))) print(a) print("~~~~~~~~~") print(a.shape) print("~~~~~~~~~") b = a.ravel() #將矩陣拉成一個向量 print(b) print("~~~~~~~~~") a.shape = (2,6) print(a) print("~~~~~~~~~") print(a.T) #轉置 print("~~~~~~~~~") print(a.resize(6,2)) #和上面的a.shape = (2,6)的功能一樣 print("~~~~~~~~~") print(a) a = np.floor(10*np.random.random((2,2))) b = np.floor(10*np.random.random((2,2))) print(a) print("~~~~~~~~~") print(b) print("~~~~~~~~~") print(np.hstack((a,b))) #將兩個矩陣橫著拼在一起 print("~~~~~~~~~") print(np.vstack((a,b))) #將兩個矩陣豎著拼在一起 a = np.floor(10*np.random.random((2,12))) print(a) print("~~~~~~~~~") b = np.hsplit(a,3) # 將a分割成3個矩陣,並將這三個矩陣拼接組成一個新矩陣,該操作不會影響a矩陣本身,它是產生了一個新的矩陣 print(b) print("~~~~~~~~~") print(b[1]) print("~~~~~~~~~") print(a) print("~~~~~~~~~") c = np.hsplit(a,(3,4))# 將a從第3列分割開,從第4列分割開,然後就產成了3個矩陣, print(c) print("~~~~~~~~~") print(c[2]) a = np.arange(12) b = a print(b is a) print("~~~~~~~~") b.shape = (3,4) #(3,4)和3,4效果一樣都表示元組 print(a) print("~~~~~~~~") print(b) print(a.shape) print("~~~~~~~~") print(id(a)) print(id(b)) a = np.arange(12) c = a.view() # 這樣做a和c是不同的記憶體,但它們公共資料,不共用其形式 print(c is a) print("~~~~~~~~") c.shape = (2,6) print(a) print("~~~~~~~~") print(c) print("~~~~~~~~") c[0,4] = 124 print(a) print("~~~~~~~~") print(c) a = np.arange(12) d = a.copy() print(a is d) # is判斷a和d是否共用同一個地址,即是否為同一個東西 d[0] = 9999 print(d) print("~~~~~~~~") print(a) data = np.sin(np.arange(20)).reshape(5,4) print(np.arange(20)) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(data) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") ind = data.argmax(axis = 0) # 找出每列中最大元素的位置 print(ind) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") data_max = data[ind,range(data.shape[1])] print(data_max) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(data.shape) # 返回矩陣的大小(行,列),返回元祖 print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(data.shape[1]) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") a = range(2) print(a) print(a[1]) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(data.max(axis=0)) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(data_max == data.max(axis=0)) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(all(data_max == data.max(axis=0))) a = np.arange(0,40,10) print(a) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") b = np.tile(a,(3,5)) #title 對a進行一個擴充套件 print(b) print(b.shape) a = np.array([[4,3,5],[1,2,1]]) print(a) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") b = np.sort(a,axis=1) #按行內由小到大重新排列 print(b) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(a) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") a.sort(axis=1) #上面那種排序方式,對a進行排序後給了b,但未對a本身進行排序。這種方法,是對a本身進行了排序 print(a) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") c = np.array([4,3,1,2]) print(c) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") j = np.argsort(c) #找出c中由小到大的元素索引 print(j) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~") print(c[j])