一,從現有的數據創建數組
1,使用arange創建
import numpy as np
temp1 = np.arange(12,dtype=np.float32)
temp2 = np.arange(3,12,dtype=np.float32)
temp3 = temp1.reshape(3,4)
print(temp1,temp1.dtype)
print(temp3,temp3.dtype)
# -----------output-----------------
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.] float32
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]] float32
2,使用array創建
import numpy as np
temp1 = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype=float)
temp2 = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype='float32')
temp3 = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype=np.float32)
temp4 = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype='i4')
temp5 = np.array([[1,1,0,0],[1,1,0,0],[1,1,0,0]],dtype=bool)
temp6 = np.array(range(1,6))
print(temp1.dtype)
print(temp2.dtype)
print(temp3.dtype)
print(temp4.dtype)
print(temp5.dtype)
print(temp6,temp6.dtype)
# -----------output-----------------
float64
float32
float32
int32
bool
[1 2 3 4 5] int32
3,使用asarray創建
temp1 = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype=np.float32)
temp3 = np.asarray(temp1 )
print(temp3)
4,使用copy創建
temp1 = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype=np.float32)
temp3 = temp1.copy()
print(temp3)
5,array和asarray的區別
array和asarray都可以將結構數據轉化爲ndarray,但是主要區別就是當數據源是ndarray時,array仍然會copy出一個副本,佔用新的內存,但asarray不會。
temp = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,0]],dtype=np.float32)
temp1 = np.array(temp) # 創建了一個新的數組
temp2 = np.asarray(temp) # 還是引用原來的數組
print(temp1,temp2)
二,創建固定範圍的數組
1,創建一個全0的數組
import numpy as np
# temp = np.arange(30).reshape(5,6)
temp = np.zeros((2,3))
print(temp)
# -----------output-----------------
[[0. 0. 0.]
[0. 0. 0.]]
2,創建一個全1的數組
import numpy as np
# temp = np.arange(30).reshape(5,6)
temp = np.ones((2,3))
print(temp)
# -----------output-----------------
[[1. 1. 1.]
[1. 1. 1.]]
3,創建一個對角線爲1的正方形數組:
np.eye(number)
import numpy as np
print(np.eye(4))
# -----------output-----------------
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
4,使用linspace創建數組
常用於創建等差數列
np.linspace (start, stop, num, endpoint, retstep, dtype)
生成等間隔的序列
start 序列的起始值
stop 序列的終止值,如果endpoint爲true,該值包含於序列中
num 要生成的等間隔樣例數量,默認爲50
endpoint 序列中是否包含stop值,默認爲ture
retstep 如果爲true,返回樣例,以及連續數字之間的步長
dtype 輸出ndarray的數據類型
import numpy as np
temp = np.linspace(0,100,11,True,True)
print(temp)
# -----------output-----------------
(array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.]), 10.0)
5,類似的還有:
numpy.arange(start, stop, step, dtype)
numpy.logspace(start, stop, num, endpoint, base, dtype)
常用於創建等比數列
三,創建隨機的數組
1,numpy生成均勻分佈隨機數
import numpy as np
# 給定隨機種子
np.random.seed(10)
# 創建維度爲(3,1)的0~1的隨機數列
t1 = np.random.rand(3,1)
# 創建維度爲(2,2)的(0~100)的小數隨機數列
t2 = np.random.uniform(0,100,(2,2))
# 創建維度爲(2,2)的(0~100)的整數隨機數列
t3 = np.random.randint(0,20,(2,2))
for i in [t1,t2,t3]:
print(i)
# -----------output-----------------
[[0.77132064]
[0.02075195]
[0.63364823]]
[[74.88038825 49.85070123]
[22.47966455 19.80628648]]
[[ 8 9]
[ 0 10]]
2,numpy生成正態分佈隨機數
import numpy as np
# 給定均值、標準差、維度的正態分佈
t1 = np.random.normal(0,1,(2,2))
# 標準正太分佈。定均值爲0、標準差爲1的正太分佈
t2 = np.random.standard_normal(size=(2,2))
for i in [t1,t2]:
print(i)
# -----------output-----------------
[[-0.290396 1.32423901]
[ 0.59322204 -2.37736497]]
[[-1.31577873 0.99945344]
[ 1.55544037 0.8770521 ]]
3,什麼是正太分佈
率密度函數爲正態分佈的期望值μ決定了其位置,其標準差σ決定了分佈的幅度。
當μ = 0,σ = 1時的正態分佈是標準正態分佈。
方差用來衡量數據的離散程度:
標準差用數據公式代替表示爲: