ValueError: Found array with dim 4. Estimator expected和ValueError: Expected 2D array, got 1D array i
python3中對(duì)numpy數(shù)組進(jìn)行降維或升維
解決報(bào)錯(cuò)如:
1.ValueError: Found array with dim 4. Estimator expected
2.ValueError: Expected 2D array, got 1D array instead:
報(bào)錯(cuò)1ValueError: Found array with dim 4. Estimator expected——解決方式:
使用 np.concatenate
函數(shù)模型:concatenate((a1, a2, …), axis=0)
Parameters參數(shù)說(shuō)明:
? 傳入的參數(shù)(a1,a2,a3,…)必須是一個(gè)多個(gè)數(shù)組的元組或者列表
另外需要指定拼接的方向,默認(rèn)axis = 0,也就是說(shuō)對(duì)數(shù)組中0軸(X軸/或者說(shuō)行)的對(duì)象進(jìn)行拼接得到一個(gè)縱向組合的數(shù)組,(axis=1則是相反);注:一般axis = 0,就是對(duì)該軸向的數(shù)組進(jìn)行操作,操作方向是另外一個(gè)軸,即axis=1。
輸出結(jié)果:(這樣就可以將對(duì)數(shù)組進(jìn)行降維了(剝除一組中括號(hào)[ ]))
[[1 2][2 3][4 5][3 4]][1 2 2 3]參考鏈接:https://blog.csdn.net/brucewong0516/article/details/79158758
報(bào)錯(cuò)2函數(shù)fit時(shí)出現(xiàn)ValueError: Expected 2D array, got 1D array instead:——解決方式:
這里我將函數(shù)報(bào)錯(cuò)時(shí)的代碼片段截取出來(lái),具體函數(shù)的數(shù)據(jù)就不截取了
方法1:使用中括號(hào)[ ]:
原代碼:
會(huì)報(bào)錯(cuò):
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中,所有的數(shù)據(jù)都應(yīng)該是二維矩陣,即np.array()中應(yīng)該至少是包含兩對(duì)中括號(hào)[ ]
參考鏈接:https://www.jianshu.com/p/60596270e94e
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的ValueError: Found array with dim 4. Estimator expected和ValueError: Expected 2D array, got 1D array i的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenCV_02 图像的基本操作:图像
- 下一篇: 智慧交通day03-车道线检测实现07: