陷阱一:數(shù)據(jù)結(jié)構(gòu)混亂
array 和 matrix 都可以用來表示多維矩陣
In [98]: a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [99]: a
Out[99]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [100]: A = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [101]: A
Out[101]:
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [102]: a.shape
Out[102]: (3, 3)
In [103]: A.shape
Out[103]: (3, 3)
看起來效果不錯。假設(shè)我們要對數(shù)據(jù)進(jìn)行篩選深滚,取第 1 列的第 1 行和第 3 行數(shù)據(jù)構(gòu)成一個 2 x 1 的列向量奕谭。先看對 array 的做法
In [99]: a
Out[99]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [100]: y
Out[100]:
matrix([[1],
[0],
[1]])
In [101]: a[:, 0]
Out[101]: array([1, 4, 7])
In [102]: a[:, 0].shape
Out[102]: (3,)
In [110]: a[:, 0][y == 1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-110-f32ed63aa2a8> in <module>()
----> 1 a[:, 0][y == 1]
IndexError: too many indices for array
In [111]: a[:, 0].reshape(3, 1)[y == 1]
Out[111]: array([1, 7])
從 Out[101] 可以看到一個陷阱涣觉,a[:, 0]
過濾完應(yīng)該是一個 3 x 1 的列向量痴荐,可是它變成了行向量。其實也不是真正意義上的行向量官册,因為行向量 shape 應(yīng)該是 3 x 1生兆,可是他的 shape 是 (3,) ,這其實已經(jīng)退化為一個數(shù)組了膝宁。所以鸦难,導(dǎo)致最后 In [110] 出錯。只有像 In [111] 那樣 reshape 一下才可以员淫。我不知道大家暈了沒有合蔽,我是已經(jīng)快暈了。
相比之下介返,matrix 可以確保運(yùn)算結(jié)果全部是二維的拴事,結(jié)果相對好一點。為什么只是相對好一點呢圣蝎?呆會兒我們再來吐吐 matrix 的槽點刃宵。
In [101]: A
Out[101]:
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [112]: y
Out[112]:
matrix([[1],
[0],
[1]])
In [113]: A[:,0]
Out[113]:
matrix([[1],
[4],
[7]])
In [102]: A[:, 0].shape
Out[102]: (3,1)
In [114]: A[:,0][y == 1]
Out[114]: matrix([[1, 7]])
In [114]: A[:,0][y == 1].shape
Out[114]: (1,2)
看起來還不錯。不過槽點就來了徘公。Out [114] 我們預(yù)期的輸入結(jié)果應(yīng)該是一個 2 x 1 的列向量牲证,可是這里變成了 1 x 2 的行向量!
為什么我會在意行向量和列向量关面?在矩陣運(yùn)算里坦袍,行向量和列向量是不同的十厢。比如一個 m x 3 的矩陣可以和 3 x 1 的列向量叉乘,結(jié)果是 m x 1 的列向量键闺。而如果一個 m x 3 的矩陣和 1 x 3 的行向量叉乘是會報錯的寿烟。
陷阱二:數(shù)據(jù)處理能力不足,語言效率低
我們再看個例子辛燥。假設(shè) X 是 5 x 2 的矩陣筛武,Y 是 5 X 1 的 bool 矩陣,我們想用 Y 來過濾 X 挎塌,即取出 Y 值為 True 的項的索引徘六,拿這些索引去 X 里找出對應(yīng)的行,再組合成一個新矩陣榴都。
In [79]: X
Out[79]:
matrix([[ 34.62365962, 78.02469282],
[ 30.28671077, 43.89499752],
[ 35.84740877, 72.90219803],
[ 60.18259939, 86.3085521 ],
[ 79.03273605, 75.34437644]])
In [80]: Y
Out[80]:
matrix([[ True],
[False],
[ True],
[ True],
[False]], dtype=bool)
In [81]: X[Y == True]
Out[81]: matrix([[ 34.62365962, 35.84740877, 60.18259939]])
In [85]: X[Y == True, :]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-85-2aeabbc2bcc5> in <module>()
----> 1 X[Y == True, :]
C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.pyc in __getitem__(self, index)
314
315 try:
--> 316 out = N.ndarray.__getitem__(self, index)
317 finally:
318 self._getitem = False
IndexError: too many indices for array
In [86]: X[:, 0][Y == True]
Out[86]: matrix([[ 34.62365962, 35.84740877, 60.18259939]])
In [87]: X[:, 1][Y == True]
Out[87]: matrix([[ 78.02469282, 72.90219803, 86.3085521 ]])
In [88]: np.column_stack((x[:, 0][y == True].reshape(3,1), x[:, 1][y == True].reshape(3,1)))
Out[88]:
matrix([[ 34.62365962, 78.02469282],
[ 35.84740877, 72.90219803],
[ 60.18259939, 86.3085521 ]])
我們預(yù)期 X 過濾完是 3 x 2 列的矩陣待锈,但不幸的是從 Out[81] 來看 numpy 這樣過濾完只會保留第一列的數(shù)據(jù),且把它轉(zhuǎn)化成了行向量嘴高,即變成了 1 x 3 的行向量竿音。不知道你有沒有抓狂的感覺。如果按照 In [85] 的寫法拴驮,還會報錯春瞬。如果要正確地過濾不同的列,需要寫成 In [86] 和 In [87] 的形式套啤。但是即使寫成 In [86] 和 In [87] 的樣式宽气,還是一樣把列向量轉(zhuǎn)化成了行向量。所以潜沦,要實現(xiàn)這個目的萄涯,得復(fù)雜到按照 In [88] 那樣才能達(dá)到目的。實際上唆鸡,這個還達(dá)不到目的涝影,因為那里面寫了好多硬編碼的數(shù)字,要處理通用的過濾情況争占,還需要寫個函數(shù)來實現(xiàn)燃逻。而這個任務(wù)在 matlab/octave 里只需要寫成 X(Y==1, :)
即可完美達(dá)成目的。
陷阱三:數(shù)值運(yùn)算句法混亂
在機(jī)器學(xué)習(xí)算法里燃乍,經(jīng)常要做一些矩陣運(yùn)算唆樊。有時候要做叉乘,有時候要做點乘刻蟹。我們看一下 numpy 是如何滿足這個需求的逗旁。
假設(shè) x, y, theta 的值如下,我們要先讓 x 和 y 點乘,再讓結(jié)果與 theta 叉乘片效,最后的結(jié)果我們期望的是一個 5 x 1 的列向量红伦。
In [22]: x
Out[22]:
matrix([[ 1. , 34.62365962, 78.02469282],
[ 1. , 30.28671077, 43.89499752],
[ 1. , 35.84740877, 72.90219803],
[ 1. , 60.18259939, 86.3085521 ],
[ 1. , 79.03273605, 75.34437644]])
In [23]: y
Out[23]:
matrix([[1],
[2],
[3],
[2],
[2]])
In [24]: theta
Out[24]:
matrix([[2],
[2],
[2]])
直觀地講,我們應(yīng)該會想這樣做:(x 點乘 y) 叉乘 theta淀衣。但很不幸昙读,當(dāng)你輸入 x * y
時妥妥地報錯。那好吧膨桥,我們這樣做總行了吧蛮浑,x[:, 0] * y
這樣兩個列向量就可以點乘了吧,不幸的還是不行只嚣,因為 numpy 認(rèn)為這是 matrix沮稚,所以執(zhí)行的是矩陣相乘(叉乘),要做點乘册舞,必須轉(zhuǎn)為 array 蕴掏。
In [37]: x * y
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-ae1a0a4af750> in <module>()
----> 1 x * y
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.pyc in __mul__(self, other)
339 if isinstance(other, (N.ndarray, list, tuple)) :
340 # This promotes 1-D vectors to row vectors
--> 341 return N.dot(self, asmatrix(other))
342 if isscalar(other) or not hasattr(other, '__rmul__') :
343 return N.dot(self, other)
ValueError: matrices are not aligned
In [38]: x[:, 0] * y
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-38-d55ad841fa29> in <module>()
----> 1 x[:, 0] * y
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.pyc in __mul__(self, other)
339 if isinstance(other, (N.ndarray, list, tuple)) :
340 # This promotes 1-D vectors to row vectors
--> 341 return N.dot(self, asmatrix(other))
342 if isscalar(other) or not hasattr(other, '__rmul__') :
343 return N.dot(self, other)
ValueError: matrices are not aligned
In [39]: sp.array(x[:,0]) * sp.array(y)
Out[39]:
array([[ 1.],
[ 2.],
[ 3.],
[ 2.],
[ 2.]])
In [42]: xy = sp.column_stack(((sp.array(x[:,0]) * sp.array(y)), (sp.array(x[:,1]) * sp.array(y)), (sp.array(x[:,2]) * sp.array(y))))
In [43]: xy
Out[43]:
array([[ 1. , 34.62365962, 78.02469282],
[ 2. , 60.57342154, 87.78999504],
[ 3. , 107.54222631, 218.70659409],
[ 2. , 120.36519878, 172.6171042 ],
[ 2. , 158.0654721 , 150.68875288]])
所以,我們需要象 In [39] 那樣一列列轉(zhuǎn)為 array 和 y 執(zhí)行點乘调鲸,然后再組合回 5 x 3 的矩陣盛杰。好不容易算出了 x 和 y 的點乘了,終于可以和 theta 叉乘了藐石。
In [44]: xy * theta
Out[44]:
matrix([[ 227.29670488],
[ 300.72683316],
[ 658.4976408 ],
[ 589.96460596],
[ 621.50844996]])
看起來結(jié)果還不錯即供,但實際上這里面也是陷阱重重。
In [45]: xy * sp.array(theta)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-45-5ea2f7324fbe> in <module>()
----> 1 xy * sp.array(theta)
ValueError: operands could not be broadcast together with shapes (5,3) (3,1)
In [46]: sp.dot(xy, sp.array(theta))
Out[46]:
array([[ 227.29670488],
[ 300.72683316],
[ 658.4976408 ],
[ 589.96460596],
[ 621.50844996]])
In [45] 會報錯贯钩,因為在 array 里 *
運(yùn)算符是點乘募狂,而在 matrix 里 *
運(yùn)算符是叉乘办素。如果要在 array 里算叉乘角雷,需要用 dot
方法⌒源看起來提供了靈活性勺三,實際上增加了使用者的大腦負(fù)擔(dān)。而我們的需求在 matlab/octave 里只需要寫成 x .* y * theta
需曾,直觀優(yōu)雅吗坚。
陷阱四:語法復(fù)雜,不自然
比如呆万,我們要在一個 5 x 2 的矩陣的前面加一列全部是 1 的數(shù)據(jù)商源,變成一個 5 x 3 的矩陣,我們必須這樣寫
In [11]: x
Out[11]:
matrix([[ 34.62365962, 78.02469282],
[ 30.28671077, 43.89499752],
[ 35.84740877, 72.90219803],
[ 60.18259939, 86.3085521 ],
[ 79.03273605, 75.34437644]])
In [18]: sp.column_stack(((sp.ones((5,1)), x)))
Out[18]:
matrix([[ 1. , 34.62365962, 78.02469282],
[ 1. , 30.28671077, 43.89499752],
[ 1. , 35.84740877, 72.90219803],
[ 1. , 60.18259939, 86.3085521 ],
[ 1. , 79.03273605, 75.34437644]])
有興趣的人可以數(shù)數(shù) In [18] 里有多少個括號谋减,還別不服牡彻,括號寫少了妥妥地報錯。而這個需求在 matlab/octave 里面只需要寫成 [ones(5,1) x]
出爹,瞬間腦袋不短路了庄吼,直觀優(yōu)雅又回來了缎除。
結(jié)論
有人說 python 是機(jī)器學(xué)習(xí)和數(shù)據(jù)分析的新貴,但和專門的領(lǐng)域語言 matlab/octave 相比总寻,用起來確實還是比較別扭的器罐。當(dāng)然有些槽點是因為語言本身的限制,比如 python 不支持自定義操作符渐行,導(dǎo)致 numpy 的一些設(shè)計不夠優(yōu)雅和直觀轰坊,但默認(rèn)把列向量轉(zhuǎn)化為行向量的做法只能說是 numpy 本身的設(shè)計問題了。這或許就是 Andrew Ng 在他的 Machine Learning 課程里用 matlab/octave 祟印,而不用 python 或其他的語言的原因吧衰倦。