學習參考鏈接:
Numpy的介紹和安裝和性能對比_嗶哩嗶哩_bilibili
Numpy相對List的優勢和特點
1、Numpy的數據結構是array數組
2、相較List的性能更好,并且包含大量的便捷的函數,以及數組中元數據的信息
3、array的數據類型必須一致,為int或者float,因此性能更高
1、創建數組
import numpy as np#創建一維數組
x=np.array([1,2,3,4,5,6])#創建二維數組
y=np.array([[1,2,3],[4,5,6]
])
print("x=",x)
print("y=",y)
x= [1 2 3 4 5 6]
y= [[1 2 3]
?[4 5 6]]?
2、array的屬性
import numpy as np#創建二維數組
y=np.array([[1,2,3],[4,5,6]
])# 數組形狀
print("y.shape",y.shape)# 數組維度
print("y.ndim",y.ndim)# 所有數據的數目
print("y.size",y.size)# 數據的類型
print("y.dtype",y.dtype)
y.shape (2, 3)
y.ndim 2
y.size 6
y.dtype int32
3、創建array的便捷函數
?3.1 np.arange
import numpy as np#使用arrange創建數組序列
#arange(start,stop,step,dtype=None),左閉右開x=np.arange(12)
y=np.arange(2,10,2)
print("x=",x)
print("y=",y)
x= [ 0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 11]
y= [2 4 6 8]
?3.2 np.ones
import numpy as np#使用ones創建全為1的數組序列
#ones(shape,dtype=None,order='C')x=np.ones(12)
y=np.ones((2,3))
print("x=",x)
print("y=",y)
x= [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
y= [[1. 1. 1.]
?[1. 1. 1.]]
?3.3 np.ones_like
import numpy as np#使用ones_like創建全為0的形狀相同的數組序列
#ones_like(x,dtype=None)
x=np.arange(12).reshape(3,4)y=np.ones_like(x)
print("x=",x)
print("y=",y)
x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[1 1 1 1]
?[1 1 1 1]
?[1 1 1 1]]
?3.4 np.empty
empty的數據是未初始化的數據,里面的值是隨機值不能直接使用
import numpy as np#使用empty創建全為0的數組序列
#empty(shape,dtype=None,order='C')
x=np.empty(10)
y=np.empty((2,5))print("x=",x)
print("y=",y)
x= [6.23042070e-307 1.89146896e-307 1.37961302e-306 6.23053614e-307
?1.60218627e-306 6.23061763e-307 6.23059386e-307 6.23058028e-307
?8.90100844e-307 2.39277352e-203]
y= [[-5.74929044e-186 -3.41963150e+202 ?8.38577217e-253 -4.80176796e+207
? -8.30082508e+049]
?[ 5.69602577e-219 ?2.08987787e-290 ?5.64286538e+290 ?2.34477072e+222
? -1.62584672e+000]]Process finished with exit code 0
?
?3.5 np.empty_like
import numpy as np#使用empty_like創建全為1的形狀相同的數組序列
#empty_like(x,dtype=None)
x=np.arange(12).reshape(3,4)y=np.empty_like(x)
print("x=",x)
print("y=",y)
x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[0 0 0 0]
?[0 0 0 0]
?[0 0 0 0]]
?3.6 np.full
import numpy as np#使用full創建指定值的數組序列
#full(shape,fill_vaule,dtype=None,order='C')
x=np.full(5,10)
y=np.full((2,5),10)print("x=",x)
print("y=",y)
x= [10 10 10 10 10]
y= [[10 10 10 10 10]
?[10 10 10 10 10]]
?3.7?np.full_like
import numpy as np#使用full_like創建填充指定值的形狀相同的數組序列
#full_like(x,fill_vaule,dtype=None)
x=np.arange(12).reshape(3,4)y=np.full_like(x,666)
print("x=",x)
print("y=",y)
x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[666 666 666 666]
?[666 666 666 666]
?[666 666 666 666]]
?3.8 np.random.randn
import numpy as np#使用rand創建隨機數組序列
#randn(d0,d1,d2...)
x=np.random.randn(4)
y=np.random.randn(4,3)
z=np.random.randn(4,3,2)print("x=",x)
print("y=",y)
print("z=",z)
x= [-0.6814137 ? 2.58645822 -0.32107098 -1.16734569]
y= [[-0.12104364 -0.60353561 -0.70747402]
?[-0.78172178 ?0.56876222 -0.89172026]
?[-0.35139011 -0.53920652 ?0.66813472]
?[ 0.6570334 ?-0.59396372 -2.36066397]]
z= [[[ 0.18886269 ?0.40312599]
? [-0.15651529 -0.28551847]
? [ 0.41414574 ?1.76023497]]?[[-0.79363865 ?0.62441301]
? [ 2.09764425 ?2.08595285]
? [-0.77915632 ?0.8259647 ]]?[[ 0.31214999 ?1.54711407]
? [ 0.90644711 ?0.76644429]
? [-1.59445626 ?1.29589803]]?[[ 0.97082177 -1.52600754]
? [ 1.4742554 ? 0.07321613]
? [-0.1599697 ? 0.19982069]]]
?4、array用于操作及函數
4.1 shape?
import numpy as npx=np.arange(12).reshape(3,4)
print(x.shape)
?(3, 4)
?4.2 數組+1
與list的區別在于是對數組內的所有的數字同時操作,而list對所有的數字同時操作則需要用for循環實現。?
import numpy as np#array+1是對數組內的所有的元素進行加1
x=np.arange(12).reshape(3,4)
y=x+1
print("x=",x)
print("y=",y)
x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[ 1 ?2 ?3 ?4]
?[ 5 ?6 ?7 ?8]
?[ 9 10 11 12]]
??4.3 數組*3
import numpy as np#array*3是對數組內的所有的元素進行*3
x=np.arange(12).reshape(3,4)
y=x*3
print("x=",x)
print("y=",y)
x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[ 0 ?3 ?6 ?9]
?[12 15 18 21]
?[24 27 30 33]]
??4.4 sin(數組)
import numpy as np#np.sin(array)是對數組內的所有的元素進行sin
x=np.arange(12).reshape(3,4)
y=np.sin(x)
print("x=",x)
print("y=",y)
?x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[ 0. ? ? ? ? ?0.84147098 ?0.90929743 ?0.14112001]
?[-0.7568025 ?-0.95892427 -0.2794155 ? 0.6569866 ]
?[ 0.98935825 ?0.41211849 -0.54402111 -0.99999021]]
?4.5 array間的加減操作
import numpy as np#np.sin(array)是對數組內的所有的元素進行sin
x=np.arange(12).reshape(3,4)
y=np.random.randint(1,10,(3,4))print("x=",x)
print("y=",y)z=x+y
w=x-y
print("z=",z)
print("w=",w)
x= [[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
y= [[3 6 2 1]
?[8 2 7 1]
?[6 7 5 9]]
z= [[ 3 ?7 ?4 ?4]
?[12 ?7 13 ?8]
?[14 16 15 20]]
w= [[-3 -5 ?0 ?2]
?[-4 ?3 -1 ?6]
?[ 2 ?2 ?5 ?2]]