零. 常見
TypeError: 'range' object doesn't support item deletion
- 注:3.x中range()要改為list(rang())棋恼,因為python3中range不返回數(shù)組對象剩膘,而是返回range對象
AttributeError: 'dict' object has no attribute 'iteritems'
- iteritems()要改為items()
二. kNN
- 報錯:
NameError: name 'reload' is not defined
- 在前面加入命令(個人推薦直接寫在mian函數(shù)里面簡單快捷)
from imp import reload
四. 樸素貝葉斯
- 報錯:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
- 那是因為書上的下面這兩行代碼有點問題:
wordList = textParse(open('email/spam/%d.txt' % i).read()
wordList = textParse(open('email/ham/%d.txt' % i).read()
需要將上面的代碼更為下面這兩行:
wordList = textParse(open('email/spam/%d.txt' % i, "rb").read().decode('GBK','ignore') )
wordList = textParse(open('email/ham/%d.txt' % i, "rb").read().decode('GBK','ignore') )
因為有可能文件中存在類似“?”非法字符僧著。
- 報錯:
TypeError: 'range' object doesn't support item deletion
AttributeError: 'dict' object has no attribute 'iteritems'
- 參考常見錯誤
五. Logistic回歸
- 報錯:
TypeError: 'numpy.float64' object cannot be interpreted as an integer
這里是因為numpy版本問題既绩,更改版本解決
pip install -U numpy==1.11.0
- 報錯:
TypeError: 'range' object doesn't support item deletion
參考常見錯誤
報錯:
AttributeError: 'numpy.ndarray' object has no attribute 'getA'
- 注釋掉plotBestFit()的矩陣轉為數(shù)組,因為在輸入時已經轉換為數(shù)組了
plotBestFit(weights)
'''
# 矩陣變?yōu)閿?shù)組,使用gradAscent時加入
weights = wei.getA()
'''