1. 程式人生 > >決策樹學習AllElectronics.py碰到的幾個問題

決策樹學習AllElectronics.py碰到的幾個問題

報錯:AttributeError: '_csv.reader' object has no attribute 'next'

原因:python版本問題

解決:將headers = reader.next()改為headers = next(reader)

報錯:_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

原因:python版本問題

解決:將allElectronicsData = open(r'F:/txt/AllElectronics.csv', 'rb')改成allElectronicsData = open(r'F:/txt/AllElectronics.csv', 'rt')

報錯:ValueError: Expected 2D array, got 1D array instead

原因:資料維度不對,將其通過reshape進行轉化

解決:將predictedY = clf.predict(newRowX)改成predictedY = clf.predict(newRowX.reshape(1,-1))