Numpy是利用python來進行數據分析中必需要掌握的基礎。是高性能科學計算和數據分析的基礎包。利用numpy能對整組數據無需循環就能進行快速的標準數學函數計算,同時能進行線性代數,隨機數,以及傅里葉變換等等功能,而對於數據分析來講,比較重要的用途就是數據的清理,過濾,子集構造,轉換,排序,描述統計等等。python
1.利用array來生成基本數組,如:算法
>>> import numpy as np數組
>>> a=np.array([1,2,3,4])dom
>>> b=np.array([[1,2,3],[4,5,6]])函數
2.利用shape來查看數組維度,如:性能
>>> a.shapespa
(4,)orm
>>> b.shape排序
(2, 3)數據分析
a是4行的數組,b是一個2行3列的數組。
3,利用zeros,ones,empty來建立全0,全1,沒有任何具體值的數組,如:
>>> np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.empty(8)
array([ 1.42988904e-307, 1.42990941e-307, 1.42987885e-307,
1.42991960e-307, 1.42988904e-307, 1.42992978e-307,
1.42991960e-307, 1.42946125e-307])
>>> np.zeros((3,2))
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
4,使用eye來建立對角矩陣,如:
>>> np.eye(4)
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
5,利用arange來建立數組,如:
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(32).reshape((8,4))
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
數組與標量之間的運算,也會將變量值傳播到各個原始,如:
>>> a
array([1, 2, 3, 4])
>>> a*2
array([2, 4, 6, 8])
>>> a**2
array([ 1, 4, 9, 16])
>>> a**0.5
array([ 1. , 1.41421356, 1.73205081, 2. ])
1,下標從0開始,如:
>>> a
array([1, 2, 3, 4])
>>> a[0]
1
2,使用冒號來控制下標範圍,冒號座標是起始下標,後邊是結束下標,若是左邊沒有,默認從0開始,若是右邊沒有,默認到最後一個,如:
>>> a[1:3]
array([2, 3])
>>> a[:]
array([1, 2, 3, 4])
>>> c
array([[1, 2],
[3, 4],
[5, 6]])
>>> c[0,1]
2
>>> c[:,:]
array([[1, 2],
[3, 4],
[5, 6]])
>>> c[1:,:]
array([[3, 4],
[5, 6]])
1.一元運算符,接受一個或者多個標量值,返回一個或者多個變量值,如:
>>> np.sqrt(a)
array([ 1. , 1.41421356, 1.73205081, 2. ])
>>> np.exp(a)
array([ 2.71828183, 7.3890561 , 20.08553692, 54.59815003])
>>> np.log(a)
array([ 0. , 0.69314718, 1.09861229, 1.38629436])
>>> np.square(a)
array([ 1, 4, 9, 16])
2.數學以及統計方法
能夠經過數組上的一組(如一行,或者一列,或者全部元素)數據進行統計計算,如sum求和,mean平均值,std標準差等:
>>> np.mean(c)
3.5
>>> np.sum(c)
21
>>> np.std(c)
1.707825127659933
或者接受某一行,某一列的運算,經過參數axis=1(行)或者axis=0(列)來控制,如:
>>> c.mean(1)
array([ 1.5, 3.5, 5.5])
>>> c.mean(0)
array([ 3., 4.])
1.利用dox進行矩陣相乘,如
>>> a=np.array([[5,7,2],[1,4,3]])
>>> a
array([[5, 7, 2],
[1, 4, 3]])
>>> b=np.ones(3)
>>> b
array([ 1., 1., 1.])
>>> a.dot(b)
array([ 14., 8.])
或者:
>>> np.dot(a,b)
array([ 14., 8.])
a是2*3數組,b是3*1數組,那麼a.dot(b)顯然就是2*1數組
2.其餘常見運算,如:
Diag:以一維數組的形式返回方陣對角線元素,如:
>>> np.diag(a)
array([5, 4])
trace:計算對角線之和,如
>>> np.trace(a)
9
Eig:計算方陣的特徵值以及特徵向量(在進行求解PCA主成分分析的時候,有很大做用)
Svd:計算奇異值分解(SVD)
1.normal產生正態(高斯)分佈樣本,如:
>>> nor= np.random.normal(size=(4,4))
>>> nor
array([[ 1.82509434, -0.08174943, -0.03192186, -1.32022539],
[ 0.5635118 , -0.01755259, -0.6218383 , -0.47245589],
[ 0.65491108, -0.07561601, -0.77738699, -1.0271891 ],
[ 0.00750912, -0.28588276, 0.04140614, -0.0730934 ]])
2. rand產生均勻分佈樣本,如:
>>> ran=np.random.rand(10)
>>> ran
array([ 0.05615543, 0.30253678, 0.05719663, 0.93391993, 0.56396041,
0.88799492, 0.90171215, 0.99980605, 0.4308874 , 0.75317069])
或者建立4*4矩陣
>>> np.random.rand(4,4)
array([[ 0.6606665 , 0.61180694, 0.80557148, 0.29191235],
[ 0.45824131, 0.71035683, 0.64597049, 0.53813232],
[ 0.19844871, 0.99582822, 0.66510914, 0.38786658],
[ 0.22661631, 0.24502371, 0.29560581, 0.65864835]])
3.uniform產生[0,1)的均勻分佈值,如:
>>> np.random.uniform(size=(4*4))
array([ 0.08978688, 0.69810777, 0.60858528, 0.88008121, 0.42380056,
0.6660461 , 0.38487761, 0.89294656, 0.8344627 , 0.33255587,
0.15196568, 0.38325999, 0.76401535, 0.30862096, 0.83909417,
0.88435482])
Numpy函數庫中存在兩種不一樣的數據類型,矩陣matrix以及數組array,均可以用於處理行列表示的數字元素。可是在這兩個數據類型上執行相同的數學運算可能獲得不一樣的結果,通常來講若是說須要矩陣之間的一些運算,如求逆,矩陣相乘,轉置等,能夠先轉換爲mat矩陣在進行。
1.使用mat()能夠把array轉換爲矩陣,如:
>>> np.mat(a)
matrix([[5, 7, 2],
[1, 4, 3]])
2.轉換爲mat以後,兩個矩陣能夠直接運算,如:
相乘*
>>> mat_a=np.mat(a)
>>> mat_a
matrix([[5, 7, 2],
[1, 4, 3]])
>>> mat_b=np.mat(b)
>>> mat_b
matrix([[ 1., 1., 1.]])
>>> mat_b.T
matrix([[ 1.],
[ 1.],
[ 1.]])
>>> mat_b=mat_b.I
>>> mat_a*mat_b
matrix([[ 14.],
[ 8.]])
3,通用函數在array以及在mat上面返回的結果都是同樣的,如:
>>> mat_a
matrix([[5, 7, 2],
[1, 4, 3]])
>>> mat_a.sum()
22
>>> mat_a.sum(1)
matrix([[14],
[ 8]])
>>> mat_a.mean(0)
matrix([[ 3. , 5.5, 2.5]])
>>> mat_a.mean(0).shape
(1, 3)
>>> mat_a.sum(1).shape
(2, 1)
1.求一個向量inx與原始矩陣dataSet全部行的歐式距離(knn算法)
>>> dataSet=np.array([[2,3,4,7,6], [4,3,4,5,7], [4,6,6,8,9], [2,3,6,1,6]])
>>> inx=np.array([2,3,4,5,6])
>>> rowSize=dataSet.shape[0] #求出行數
>>> diffMat=tile(inx,(rowSize,1)) #利用tile函數,把inx擴充爲與dataset同樣的維度,目的是爲了矩陣相減
>>> diffMat2=diffMat-dataSet
>>> diffMat3=diffMat**2 #求歐式距離
>>> diffMat4=diffMat3**0.5
2.在Numpy中實現PCA主成分分析
已知一個矩陣dataSet
>>> dataSet
array([[2, 3, 4, 7, 6],
[4, 3, 4, 5, 7],
[4, 6, 6, 8, 9],
[2, 3, 6, 1, 6]])
求出它的PCA,分爲以下幾個步驟:
1)先求出每一列,也就是每一個特徵的平均值,axis=0表示列,axis=1表示行
>>> meanVals=np.mean(dataSet,axis=0)
2)原始矩陣去平均化
>>> meanRemove = dataSet-meanVals
3)求出去平均化後的矩陣的協方差矩陣
>>> covMat=np.cov(meanRemove, rowvar=0)
4)求出協方差矩陣的特徵值以及特徵向量,利用numpy庫裏面的eig函數
>>> eigVals,eigVects=np.linalg.eig(np.mat(covMat))
>>> eigVals
array([ 1.20374494e+01, 3.44539806e+00, 1.01715252e+00,
-1.59662646e-16, 1.21625562e-16])
>>> eigVects
matrix([[ 0.20502268, 0.21893499, -0.80686681, 0.45018645, -0.41478476],
[ 0.32626948, 0.5145318 , 0.23557446, -0.33467377, -0.45542834],
[-0.03039502, 0.57264251, 0.43491946, 0.64725395, -0.0675817 ],
[ 0.86497081, -0.39326712, 0.20883181, 0.21575132, -0.02252723],
[ 0.32002433, 0.4524887 , -0.24638376, -0.46887027, 0.78451505]])
5)求出topN(假設爲3)大的特徵值以及對應的特徵向量
>>> eigValInd=np.argsort(eigVals)
>>> eigValInd
array([3, 4, 2, 1, 0])
>>> eigValInd=eigValInd[:-(3+1):-1]
>>> eigValInd
array([0, 1, 2]) #TOPN的值對應的下標爲0,1,2
>>> redEigVects=eigVects[:,eigValInd] #TOPN特徵值對應的特徵向量組成的矩陣
>>> redEigVects
matrix([[ 0.20502268, 0.21893499, -0.80686681],
[ 0.32626948, 0.5145318 , 0.23557446],
[-0.03039502, 0.57264251, 0.43491946],
[ 0.86497081, -0.39326712, 0.20883181],
[ 0.32002433, 0.4524887 , -0.24638376]])
上面生成的redEigVect矩陣就是咱們須要的PCA,也就是把5個特徵變成了3個特徵,實現了降維。假設原先的特徵爲x1,x2,x3,x4,x5,那麼進行PCA轉換後,新的三個變量分別爲:
Y1=0.20502268*X1+ 0.32626948*x2+-0.03039502*x3…..
Y2=0.21893499*X1+…..
Y3= -0.80686681*X1+…….