Python科學計算庫-Numpy

NumPy 是 Python 語言的一個擴充程序庫。支持高級大量的維度數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫,也是學習 python 必學的一個庫。python

1. 讀取文件

numpy.genfromtxt() 用於讀取 txt 文件,其中傳入的參數依次爲:數組

  1. 須要讀取的 txt 文件位置,此處文件與程序位於同一目錄下
  2. 分割的標記
  3. 轉換類型,若是文件中既有文本類型也有數字類型,就先轉成文本類型

help(numpy.genfromtxt)用於查看幫助文檔:
若是不想看 API 能夠啓動一個程序用 help 查看指令的詳細用法dom

import numpy world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str) print(type(world_alcohol)) print(world_alcohol) print(help(numpy.genfromtxt)) 

2. 構造 ndarray

numpy.array()構造 ndarray

numpy.array()中傳入數組參數,能夠是一維的也能夠是二維三維的。numpy 會將其轉變成 ndarray 的結構。函數

vector = numpy.array([1,2,3,4]) matrix = numpy.array([[1,2,3],[4,5,6]]) 

傳入的參數必須是同一結構,不是同一結構將發生轉換。學習

vector = numpy.array([1,2,3,4]) array([1, 2, 3, 4]) 

均爲 int 類型spa

vector = numpy.array([1,2,3,4.0]) array([ 1., 2., 3., 4.]) 

轉爲浮點數類型debug

vector = numpy.array([1,2,'3',4]) array(['1', '2', '3', '4'],dtype='<U21') 

轉爲字符類型code

利用 .shape 查看結構

可以瞭解 array 的結構,debug 時經過查看結構可以更好地瞭解程序運行的過程。文檔

print(vector.shape)
print(matrix.shape)
(4,) (2, 3) 

利用 dtype 查看類型

vector = numpy.array([1,2,3,4]) vector.dtype dtype('int64') 

ndim 查看維度

一維數學

vector = numpy.array([1,2,3,4]) vector.ndim 1 

二維

matrix = numpy.array([[1,2,3], [4,5,6], [7,8,9]]) matrix.ndim 2 

size 查看元素數量

matrix.size
9 

3. 獲取與計算

numpy 能使用切片獲取數據

matrix = numpy.array([[1,2,3], [4,5,6], [7,8,9]]) 

根據條件獲取

numpy 可以依次比較 vector 和元素之間是否相同

vector = numpy.array([5, 10, 15, 20]) vector == 10 array([False, True, False, False], dtype=bool) 

根據返回值獲取元素

vector = numpy.array([5, 10, 15, 20]) equal_to_ten = (vector == 10) print(equal_to_ten) print(vector[equal_to_ten]) [False True False False] [10] 

進行運算以後獲取

vector = numpy.array([5, 10, 15, 20]) equal_to_ten_and_five = (vector == 10) & (vector == 5) 
vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5) 

類型轉換

將總體類型進行轉換

vector = numpy.array([5, 10, 15, 20]) print(vector.dtype) vector = vector.astype(str) print(vector.dtype) int64 <U21 

求和

sum() 可以對 ndarray 進行各類求和操做,好比分別按行按列進行求和

matrix = numpy.array([[1,2,3], [4,5,6], [7,8,9]]) print(matrix.sum()) print(matrix.sum(1)) print(matrix.sum(0)) 45 [ 6 15 24] [12 15 18] 

sum(1) 是 sum(axis=1)) 的縮寫,1表示按照 x軸方向求和,0表示按照y軸方向求和

4. 經常使用函數

reshape

生成從 0-14 的 15 個數字,使用 reshape(3,5) 將其構形成一個三行五列的 array。

import numpy as np arr = np.arange(15).reshape(3, 5) arr array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) 

zeros

生成指定結構的默認爲 0. 的 array

np.zeros ((3,4)) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) 

ones

生成一個三維的 array,經過 dtype 指定類型

np.ones( (2,3,4), dtype=np.int32 ) array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]]) 

range

指定範圍和數值間的間隔生成 array,注意範圍包左不包右

np.arange(0,10,2) array([0, 2, 4, 6, 8]) 

random 隨機數

生成指定結構的隨機數,能夠用於生成隨機權重

np.random.random((2,3)) array([[ 0.86166627, 0.37756207, 0.94265883], [ 0.9768257 , 0.96915312, 0.33495431]]) 

5. ndarray 運算

元素之間依次相減相減

a = np.array([10,20,30,40]) b = np.array(4) a - b array([ 6, 16, 26, 36]) 

乘方

a**2 array([ 100, 400, 900, 1600]) 

開根號

np.sqrt(B)

array([[ 1.41421356, 0. ], [ 1.73205081, 2. ]]) 

e 求方

np.exp(B)

array([[  7.3890561 , 1. ], [ 20.08553692, 54.59815003]]) 

向下取整

a = np.floor(10*np.random.random((2,2))) a array([[ 0., 0.], [ 3., 6.]]) 

行列變換

a.T

array([[ 0., 3.], [ 0., 6.]]) 

變換結構

a.resize(1,4) a array([[ 0., 0., 3., 6.]]) 

6. 矩陣運算

矩陣之間的運算

A = np.array( [[1,1], [0,1]] ) B = np.array( [[2,0], [3,4]] ) 

對應位置一次相乘

A*B

array([[2, 0], [0, 4]]) 

矩陣乘法

print (A.dot(B)) print(np.dot(A,B)) [[5 4] [3 4]] 

橫向相加

a = np.floor(10*np.random.random((2,2))) b = np.floor(10*np.random.random((2,2))) print(a) print(b) print(np.hstack((a,b))) [[ 2. 3.] [ 9. 3.]] [[ 8. 1.] [ 0. 0.]] [[ 2. 3. 8. 1.] [ 9. 3. 0. 0.]] 

縱向相加

print(np.vstack((a,b)))

[[ 2. 3.] [ 9. 3.] [ 8. 1.] [ 0. 0.]] 

矩陣分割

#橫向分割 print( np.hsplit(a,3)) #縱向風格 print(np.vsplit(a,3)) 

7. 複製的區別

地址複製

經過 b = a 複製 a 的值,b 與 a 指向同一地址,改變 b 同時也改變 a。

a = np.arange(12) b = a print(a is b) print(a.shape) print(b.shape) b.shape = (3,4) print(a.shape) print(b.shape) True (12,) (12,) (3, 4) (3, 4) 

複製值

經過 a.view() 僅複製值,當對 c 值進行改變會改變 a 的對應的值,而改變 c 的 shape 不改變 a 的 shape

a = np.arange(12) c = a.view() print(c is a) c.shape = 2,6 c[0,0] = 9999 print(a) print(c) False [9999 1 2 3 4 5 6 7 8 9 10 11] [[9999 1 2 3 4 5] [ 6 7 8 9 10 11]] 

完整拷貝

a.copy() 進行的完整的拷貝,產生一份徹底相同的獨立的複製

a = np.arange(12) c = a.copy() print(c is a) c.shape = 2,6 c[0,0] = 9999 print(a) print(c) False [ 0 1 2 3 4 5 6 7 8 9 10 11] [[9999 1 2 3 4 5] [ 6 7 8 9 10 11]]
相關文章
相關標籤/搜索