1. 程式人生 > >成功解決Python的Reshape your data either using array.reshape(-1, 1) if your data has a single feature or

成功解決Python的Reshape your data either using array.reshape(-1, 1) if your data has a single feature or

今天寫關於決策樹的一些演算法的時候,臥槽,mmp,竟然出現了紅色的警示錯誤,


ValueError: Expected 2D array, got 1D array instead:
array=[ 1.  0.  0.  0.  1.  0.  0.  1.  0.  1.  0.].

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我特麼就有點木訥了,不應該呀,啊啊啊。於是百度谷歌各大論壇,卻找不到類似的錯誤,不過在Stack Overflow

上歪果仁的回答倒是提醒了我,哈哈,還是謝謝那個歪果仁啦,我仔細檢查了幾遍,才發現,這特麼明明是一個so easy的問題,加個中括號啊,弄的我好embarrassed。因為輸出的格式不對,必須是陣列矩陣的形式,哈哈,分享出來啦,希望大家不要犯類似的錯誤啦!

predictedY = clf.predict(newRowX)

print("predictedY: " + str(predictedY))