ValueError: Found array with dim 4. Estimator expected和ValueError: Expected 2D array, got 1D array i
python3中對numpy數組進行降維或升維
解決報錯如:
1.ValueError: Found array with dim 4. Estimator expected
2.ValueError: Expected 2D array, got 1D array instead:
報錯1ValueError: Found array with dim 4. Estimator expected——解決方式:
使用 np.concatenate
函數模型:concatenate((a1, a2, …), axis=0)
Parameters參數說明:
? 傳入的參數(a1,a2,a3,…)必須是一個多個數組的元組或者列表
另外需要指定拼接的方向,默認axis = 0,也就是說對數組中0軸(X軸/或者說行)的對象進行拼接得到一個縱向組合的數組,(axis=1則是相反);注:一般axis = 0,就是對該軸向的數組進行操作,操作方向是另外一個軸,即axis=1。
輸出結果:(這樣就可以將對數組進行降維了(剝除一組中括號[ ]))
[[1 2][2 3][4 5][3 4]][1 2 2 3]參考鏈接:https://blog.csdn.net/brucewong0516/article/details/79158758
報錯2函數fit時出現ValueError: Expected 2D array, got 1D array instead:——解決方式:
這里我將函數報錯時的代碼片段截取出來,具體函數的數據就不截取了
方法1:使用中括號[ ]:
原代碼:
會報錯:
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.使用np.array.reshape修改:
x_new = np.array([50000,8,1.2]).reshape(1,-1)方法2.使用 np.array.reshape(1,-1):
x_new = np.array([[50000,8,1.2]])python3新版的sklearn中,所有的數據都應該是二維矩陣,即np.array()中應該至少是包含兩對中括號[ ]
參考鏈接:https://www.jianshu.com/p/60596270e94e
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ValueError: Found array with dim 4. Estimator expected和ValueError: Expected 2D array, got 1D array i的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV_02 图像的基本操作:图像
- 下一篇: python notebooks_Jup