統計分析
算總數
.sum(ndarray)
ex:
>>> np.sum([0.5, 1.5])
2.0
取最大值
.amax(ndarray)
ex:
>>>a = np.array([1,1,2,2,2,2,3,3,3])
>>>print numpy.amax(a)
3
取最小值
.amin(ndarray)
算出標準差
.std(ndarray)
ex:
>>>a = np.array([1,1,2,2,2,2,3,3,3])
>>>print numpy.std(a)
平均數
.average(ndarray)
ex:
>>>a=[1, 2, 3, 4]
>>> np.average(a)
2.5
將所有元素取小數後n位
.round(ndarray)
ex:
.round(ndarray)
>>>a=[0.11,0.22,0.33]
>>>np.round(a,1)
[0.1,0.2,0.3]
…
將資料切成多份
.histogram( ndarray, bins= < num>)
ex:
將數列分成3等份
hist, bins = np.histogram([1,1,2,2,2,2,3,3,3], bins=3)
print bins
>>> [1 1.66666667 2.33333333 3]
print hist
>>> [2 4 3]
ex: 將數列分為50等份並畫圖
hist, bins = np.histogram(x, bins=50)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()
refer:
http://stackoverflow.com/questions/5328556/histogram-matplotlib
http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
計算元素重複次數
.bincount(ndarray)
ex:
target=[1, 1, 2, 3, 4]
dcount = np.bincount(target)
dindex = np.nonzero(dcount)[0]
print dcount
print np.nonzero(dcount)[0]
print zip(dindex,dcount[dindex])
………………………………………………………………………………………………….
矩陣運算
設a,b如下
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 4, 6])
每個元素*2
>>> a * 2
array([2, 4, 6])
平方
>>> a ** 2
array([1, 4, 9])
向量相加
>>> a+b
array([3, 6, 9])
向量相乘
>>>a*b
array([ 2, 8, 18])
向量相減
>>> a-b
array([-1, -2, -3])
向量相除
>>> a/b
array([ 0.5, 0.5, 0.5])
refer
http://ccckmit.wikidot.com/la:vector
dot
矩陣的乘積
兩數值相乘
ex:
>np.dot(3, 4)
12
對於一維數組,它計算的是其點積/內積
ex:
X=[1,3,-5], Y=[4,-2,-1]
(X^T)*(Y)=1*4+3*-2+-5*-1=3
做法如下
>>> X=[1,3,-5]
>>> Y=[4,-2,-1]
>>> print np.dot(X,Y)
3
對於二維數組,它計算的是矩陣乘積,
ex:
>>> a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1], [2, 2]])
refer
NumPy Basics: Arrays and Vectorized Computation
http://pda.readthedocs.org/en/latest/chp4.html#ndarray