机器学习实战:TypeError: unhashable type: 'matrix'
生活随笔
收集整理的這篇文章主要介紹了
机器学习实战:TypeError: unhashable type: 'matrix'
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在《機器學習實戰(zhàn)》的第9章樹回歸中的源代碼錯誤:
既是:
TypeError: unhashable type: 'matrix'matrix類型不能被hash
解決過程:
In [22]: testMat Out[22]: matrix([[ 1., 0., 0., 0.],[ 0., 1., 0., 0.],[ 0., 0., 1., 0.],[ 0., 0., 0., 1.]])In [23]: testMat[:,1] Out[23]: matrix([[ 0.],[ 1.],[ 0.],[ 0.]])In [24]: set(testMat[:,1]) Traceback (most recent call last):File "<ipython-input-24-34e910607d48>", line 1, in <module>set(testMat[:,1])TypeError: unhashable type: 'matrix'In [25]: testMat[:,1].T.tolist() Out[25]: [[0.0, 1.0, 0.0, 0.0]]In [26]: testMat[:,1].T.A.tolist() Out[26]: [[0.0, 1.0, 0.0, 0.0]]In [27]: testMat[:,1].T.A.tolist()[0] Out[27]: [0.0, 1.0, 0.0, 0.0]In [28]: testMat[:,1].T.tolist()[0] Out[28]: [0.0, 1.0, 0.0, 0.0]In [29]: set(testMat[:,1].T.tolist()[0]) Out[29]: {0.0, 1.0}即必須轉(zhuǎn)換為行向量,然后再轉(zhuǎn)換為列表,最后集合處理得到所有不同的元素值。
我這里用的是windows下的python 2.7 版本,其它的系統(tǒng)和版本暫不清楚。
總結
以上是生活随笔為你收集整理的机器学习实战:TypeError: unhashable type: 'matrix'的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSDN Markdown使用
- 下一篇: 哈利波特5迅雷下载地址(哈利波特5迅雷下