adarray:能夠是多維數組,但元素類型必須相同。數組
經常使用屬性:dom
T 數組的轉置(對高維數組而言)spa
dtype 數組元素的,數據類型code
size 數組元素的個數blog
ndim 數組的維數it
shape 數據的維度大小(以元組形式)ast
dtype:class
bool_, int(8,16,32,64), unit(8,16,32,64), float(16,32,64)import
類型轉換:astype()隨機數
建立adarray:
array() 將列表轉換爲數組,可選擇顯式指定dtype
arange() range的numpy版,支持浮點數
linspace() 相似arange(),第三個參數爲數組長度
zeros() 根據指定形狀和dtype,建立全0數組
ones() 根據指定形狀和dtype,建立全i數組
empty() 根據指定形狀和dtype,建立空數組(隨機值)
eye() 根據指定邊長dtype,建立單元矩陣
實例:
linspace(0,10,15) 將0--10之間的數字,分紅15份
導入numpy模塊
import numpy as np
Array(數組)
a = np.array([1,2,3])
#a
#array([1,2,3])
type(a)
#nympy.ndarray
a.shape
#(3,) #一緯數據 看大小
a= a.reshape((1,-1) ) #明確行列,-1=3
a.shape
#(1,3) #1行3列
a = np.array([1,2,3,4,5,6]) a.shape #(6,) a= a.reshape((2,-1)) a.shape #(2,3) a #array( [[1,2,3], []4,5,6] ] )
###
a= a.reshape((-1,2))
a
array([
[1,2],
[3,4],
[5,6]
])
##取5
a[2,0]
## 將5換成55
a[2,0]= 55
zeros
a = zeros((3,3))
a
array([
[0.,0.,0.],
[0.,0.,0.],
[0.,0.,0.],
])
ones
a = np.ones((2,3)) a ## array([ [1.,1.,1.], [1.,1.,1.], ])
full
a = np.full((3,3),0) #3行3列,全部數據都是0
a = np.full((2,3),1) #2行3列,全部數據都是1
eye :單位矩陣
a = np.eye((3)) #左上右下爲1,3行3列 array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
random.random:建立隨機數組,取值在0--1之間
a=np.random.random((3,4)) #3行4列,0-1之間數字組成的 array([[0.31970217, 0.52454361, 0.93528294, 0.59955502], [0.47355245, 0.7775892 , 0.8112688 , 0.58033926], [0.20438656, 0.37185309, 0.89225405, 0.61406772]])