目錄
(2)通過改變數組的shape屬性,在保持數組元素個數不變的情況下,改變數組每個軸的長度
6.ufunc運算:Numpy裏面內置的許多ufunc函數都是在C語言級別實現的,因此它們的運算速度非常快
一.Numpy的安裝
pip install numpy
二.Numpy的基本使用
1.導入Numpy庫,命令如下:
import numpy as np #底層是用C語言實現的,效率高
2.數組的創建
需要創建數組才能進行其他操作。array函數傳遞Python的序列對象創建數組
(1)例如,下面分別創建一維數組和二維數組:
a = np.array([1, 2, 3, 4])
b = np.array((5, 6, 7, 8))
c = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[7, 8, 9, 10]])
數組的大小可以通過shape屬性獲得:
print(a.shape)#(4,)
print(b.shape)#(4,)
print(c.shape)#(3, 4)
即a、b爲4行一列,c爲三行四列
(2)通過改變數組的shape屬性,在保持數組元素個數不變的情況下,改變數組每個軸的長度
c.shape = 2, -1
print(c)#[[ 1 2 3 4 5 6]
#[ 7 8 7 8 9 10]]
(3)reshape
本身不變,得到一個轉化之後的值,創建一個改變之後的新數組.
d = c.reshape(3, 4)
print(c)
# [[ 1 2 3 4 5 6]
# [ 7 8 7 8 9 10]]
print(d)
#[[ 1 2 3 4]
#[ 5 6 7 8]
#[ 7 8 9 10]]
e = a.reshape(2,2)
print(a)#[1 2 3 4]
print(e)
#[[1 2]
#[3 4]]
數組a和e共享數據存儲內存區域,因此修改其中任意一個數的元組都會發生改變
a[0] = 10
print(e)
# [[10 2]
# [ 3 4]]
3.數組的類型
print(f.dtype)#float64
可以通過dtype參數在創建時指定元素類型
a = np.arange(0, 1, 0.1)
#最後一個值是取不到的:arrange(開始值,終值,步長)
print(a)#[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
4.數組的其他創建方式
(1)arrange函數
類似於Python的range函數
a = np.arange(0, 1, 0.1)
#最後一個值是取不到的:arrange(開始值,終值,步長)
print(a)#[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
(2)linspace函數
#linspace(開始值,終值,元素個數,[endpoint])
#endpoint指定是否包括終值,默認設置包括終值
#endpoint是否包括終值,默認設置包括終值
b = np.linspace(0, 1, 12)
print(b)
(3)logspace函數
#logspace創建10^0到10^2的 等比數列
c = np.logspace(0, 2, 10)
print(c)
5.數組元素的存取
a = np.arange(10)
print(a[2 : 5])#[2 3 4]
print(a[2 : -1])#[2 3 4 5 6 7 8]
6.ufunc運算:Numpy裏面內置的許多ufunc函數都是在C語言級別實現的,因此它們的運算速度非常快
x = [i * 0.001 for i in range(1000000)]
#(碰到的一個問題)
#start = time.clock() #time.clock是被棄用的方法,在python3.8的時候將被移除
#use time.perf_counter or time.process_time instead
start = time.perf_counter()
#列表得用for循環進行賦值
for i,t in enumerate(x):#enumerate同時列出列表的下標和對應列表的值
x[i] = math.sin(t)
print('math.sin:', time.perf_counter() - start)#math.sin: 0.378968541
x = [i * 0.001 for i in range(1000000)]
x = np.array(x) #將列表轉化爲數組
start = time.perf_counter()
#數組可以直接用np.sin()函數進行調用
np.sin(x)
print('numpy.sin:', time.perf_counter() - start)#numpy.sin: 0.012504123000000034
可以發現,numpy.sin 比math.sin 快得多
7.矩陣的運算
矩陣的乘法要求左邊的列數要等於右邊矩陣的行數
矩陣的乘積可以使用dot函數計算。對於二維數組,它計算的是矩陣的乘積,對於一維數組,它計算的是點積
a = np.arange(12).reshape(4, 3)
print(a)
# [[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]
b = np.arange(12, 24).reshape(3, 4)
print(b)
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
c = np.dot(a, b)
print(c)
# [[ 56 59 62 65]
# [200 212 224 236]
# [344 365 386 407]
# [488 518 548 578]]
8.文件的存取
(1)讀寫一維和二維的數組:
#文件的存取
a = np.arange(0, 12, 0.5).reshape(4, -1)
#默認按照‘%。18e’格式保存數據,以空格分隔
np.savetxt("./a.txt", a)#將a保存成text
b = np.loadtxt("./a.txt")
print(b)
# [[ 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]]
(2)改爲整數保存數據
#fmt = "%d"表示只保存整數部分,delimiter = ","表示用’,’進行分隔
np.savetxt("./a2.txt", a, fmt = "%d", delimiter = ",")
b2 = np.loadtxt("./a2.txt", delimiter=",")
print(b2)
# [[ 0. 0. 1. 1. 2. 2.]
# [ 3. 3. 4. 4. 5. 5.]
# [ 6. 6. 7. 7. 8. 8.]
# [ 9. 9. 10. 10. 11. 11.]]