1. 程式人生 > >python 科學計算庫Numpy

python 科學計算庫Numpy

科學計算庫Numpy

1、讀取檔案

numpy.gerfromtxt()用於讀取檔案,其中傳入的引數依次是:

  1、需要讀取txt檔案位置,此處檔案與程式位於同一目錄下

  2、delimiter 分割的標記

  3、dtype 轉換型別,如果檔案中既有文字型別也有數字型別,就先轉成文字型別

help(numpy.genformtxt)用於檢視幫助文件

1 import numpy
2  
3 world_alcohol = numpy.genfromtxt("world_alcohol.txt
", delimiter=",",dtype=str) 4 print(type(world_alcohol)) 5 print(world_alcohol) 6 print(help(numpy.genfromtxt))
View Code

2、構造ndarray

numpy.array()中傳入陣列引數,可以是一維的也可以是二維三維的,numpy會將其轉變成ndayyay結構。

 1 import numpy
 2 vector = numpy.array([1,2,3,4])
 3
matrix = numpy.array([[1,2,3],[4,5,6]]) 4 print(vector) 5 print("----------------------") 6 print(matrix) 7 # [1 2 3 4] 8 # ---------------------- 9 # [[1 2 3] 10 # [4 5 6]]
View Code

注意:傳入的引數必須是同一結構,不是同一結構將會發生轉換

1 import numpy
2 vector1 = numpy.array([1,2,3,4.])
3 vector2 = numpy.array([1,2,3,4]) 4 vector3 = numpy.array([1,2,'3',4]) 5 print(vector1) # [1. 2. 3. 4.] 6 print(vector2) # [1,2,3,4] 7 print(vector3) # ['1' '2' '3' '4']
View Code

利用.shape檢視結構

1 import numpy
2 vector = numpy.array([1,2,3,4])
3 matrix = numpy.array([[1,2,3],[4,5,6]])
4 print(vector.shape)
5 print(matrix.shape)
6 
7 # (4,)
8 # (2, 3)
View Code

利用.dtype檢視型別

1 import numpy
2 vector = numpy.array([1,2,3,4])
3 print(vector.dtype)# int32
View Code

利用.ndim檢視維度

1 import numpy
2 vector = numpy.array([1,2,3,4])
3 matrix = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
4 print(vector.ndim)# 1
5 print(matrix.ndim)# 2
View Code

利用.size檢視元素的數量

print(matrix.size)  # 9

3、獲取與計算

切片索引取值

vector = numpy.array([5, 10, 15, 20])

print(vector[0:3])  # 切片索引取值# [ 5 10 15]

利用返回值獲取元素

import numpy
vector = numpy.array([5, 10, 15, 20])
print(vector == 10)   # [False  True False False]
# 利用返回值獲取元素
print(vector[vector == 10])# [10]

將整型型別進行轉換

vector = vector.astype(str)
print(vector.dtype)# <U11

求和

matrix = numpy.array([[1,2,3],
                      [4,5,6],
                     [7,8,9]])
print(matrix.sum())# 45
print(matrix.sum(1))# 按每行求和 # [ 6 15 24]
print(matrix.sum(0))# 按每列求和 # [12 15 18]

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

4、常用函式

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

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

zero:生成指定結構的預設為0.的array

1 np.zeros ((3,4))
2  
3 #array([[ 0.,  0.,  0.,  0.],
4 #       [ 0.,  0.,  0.,  0.],
5 #       [ 0.,  0.,  0.,  0.]])
View Code

ones:生成一個三維的array,通過dtype指定型別

1 np.ones( (2,3,4), dtype=np.int32 )
2  
3 # array([[[1, 1, 1, 1],
4 #         [1, 1, 1, 1],
5 #         [1, 1, 1, 1]],
6  
7 #        [[1, 1, 1, 1],
8 #         [1, 1, 1, 1],
9 #         [1, 1, 1, 1]]])
View Code

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

1 np.arange(0,10,2)
2  
3 # array([0, 2, 4, 6, 8])
View Code

random隨機數:生成指定結構的隨機數,可以用於生成隨機權重

1 np.random.random((2,3))
2  
3 # array([[ 0.86166627,  0.37756207,  0.94265883],
4 #        [ 0.9768257 ,  0.96915312,  0.33495431]])
View Code

ndarray運算:矩陣之間的相加、相減、開根號、e平方

 1 a = np.array([10,20,30,40])
 2 b = np.array(4)
 3  
 4 print(a - b)    # array([ 6, 16, 26, 36])
 5 print(a**2) # array([ 100,  400,  900, 1600])
 6 print(np.sqrt(B))   # array([[ 1.41421356,  0.        ],
 7                         # [ 1.73205081,  2.        ]])
 8 
 9 print(np.exp(B))    # array([[  7.3890561 ,   1.        ],
10                         # [ 20.08553692,  54.59815003]])
View Code

向下取整np.floor()和向下取整np.ceil()

 1 import numpy as np
 2 a = np.floor(10*np.random.random((2,2)))
 3 print(a)
 4 a = np.ceil(np.random.random((2,2)))
 5 print(a)
 6 
 7 # [[2. 8.]
 8 #  [5. 0.]]
 9 #######################
10 # [[1. 1.]
11 #  [1. 1.]]
View Code

轉置(行列變換)a.T

1 import numpy as np
2 a = np.array([[1,2],[3,4]])
3 print(a)
4 print(a.T)# 轉置
5 # [[1 2]
6 #  [3 4]]
7 ################
8 # [[1 3]
9 #  [2 4]]
View Code

變換結構a.resize(1,4)

1 import numpy as np
2 a = np.array([[1,2],[3,4]])
3 a.resize(1,4)
4 a# array([[1, 2, 3, 4]])
View Code

矩陣運算

A*B

A.dot(B)

np.dot(A,B)

 1 import numpy as np
 2 A = np.array( [[1,1],
 3                [0,1]] )
 4 B = np.array( [[2,0],
 5                [3,4]] )
 6 print(A*B)
 7 
 8 print (A.dot(B))# A*B
 9 print(np.dot(A,B))# A*B
10 # [[2 0]
11 #  [0 4]]
View Code

橫向相加 np.hstack(a,b)

 1 a = np.floor(10*np.random.random((2,2)))
 2 b = np.floor(10*np.random.random((2,2)))
 3  
 4 print(a)
 5 print(b)
 6 print(np.hstack((a,b)))
 7  
 8 # [[ 2.  3.]
 9 # [ 9.  3.]]
10 # [[ 8.  1.]
11 # [ 0.  0.]]
12 # [[ 2.  3.  8.  1.]
13 # [ 9.  3.  0.  0.]]
View Code

縱向相加 np.vstack(a,b)

1 print(np.vstack((a,b)))
2  
3 # [[ 2.  3.]
4 # [ 9.  3.]
5 # [ 8.  1.]
6 # [ 0.  0.]]
View Code

矩陣縱向切割np.hsplit(a,3)  # 把a豎切成3分

 1 a = np.floor(10*np.random.random((2,12)))
 2 print(a)
 3 print(np.hsplit(a,3))
 4 
 5 # [[1. 4. 9. 1. 7. 2. 6. 3. 5. 4. 1. 8.]
 6 #  [0. 0. 4. 4. 7. 9. 1. 6. 7. 3. 9. 2.]]
 7 
 8 
 9 #  [array([[1., 4., 9., 1.],
10 #        [0., 0., 4., 4.]]), 
11 #  array([[7., 2., 6., 3.],
12 #        [7., 9., 1., 6.]]), 
13 #  array([[5., 4., 1., 8.],
14 #        [7., 3., 9., 2.]])]
View Code

矩陣橫向切割 np.vsplit(a,3)  # 把a橫的切成3分

 1 b = np.floor(10*np.random.random((12,2)))
 2 print(b)
 3 print(np.vsplit(b,3))
 4 
 5 # [[8. 0.]
 6 #  [9. 1.]
 7 #  [9. 1.]
 8 #  [7. 7.]
 9 #  [7. 1.]
10 #  [9. 0.]
11 #  [8. 0.]
12 #  [7. 4.]
13 #  [8. 8.]
14 #  [7. 5.]
15 #  [9. 8.]
16 #  [1. 5.]]
17 
18 [array([[8., 0.],
19        [9., 1.],
20        [9., 1.],
21        [7., 7.]]), 
22 array([[7., 1.],
23        [9., 0.],
24        [8., 0.],
25        [7., 4.]]), 
26 array([[8., 8.],
27        [7., 5.],
28        [9., 8.],
29        [1., 5.]])]
View Code

複製的區別

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

 1 a = np.arange(12)
 2 b = a
 3 print(a is b)
 4  
 5 print(a.shape)
 6 print(b.shape)
 7 b.shape = (3,4)
 8 print(a.shape)
 9 print(b.shape)
10  
11 # True
12 # (12,)
13 # (12,)
14 # (3, 4)
15 # (3, 4)
View Code

複製值:通過 a.view() 僅複製值,當對 c 值進行改變會改變 a 的對應的值,而改變 c 的 shape 不改變 a 的 shape,只有值會變,矩陣結構不變

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

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

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