解決Python報錯Valueerror: Expected 2d Array Got 1d Array Instead
如您所知,每種編程語言都會遇到很多錯誤,有些是在運(yùn)行時,有些是在編譯時。 Python 在使用 numpy 庫時有時會遇到數(shù)組錯誤。
當(dāng)我們在 numpy 中傳遞一維數(shù)組而不是二維數(shù)組時,會發(fā)生錯誤 ValueError: Expected 2D array, got 1D array instead 。
Python 中的 Numpy 數(shù)組
Numpy 是一個處理數(shù)組和數(shù)學(xué)運(yùn)算的開源庫。 在 Python 中,列表向我們提供了數(shù)組的用途,但 numpy 的創(chuàng)建者聲稱他們證明數(shù)組比列表快 50 倍。
這是使用 numpy 數(shù)組的核心目的之一。
在 Python 中創(chuàng)建一個 Numpy 數(shù)組
numpy 數(shù)組的語法很簡單。 我們必須將 numpy 庫導(dǎo)入您的程序并相應(yīng)地使用。
import numpy as np # creating a numpy array array1 = np.array([2,4,6]) print(array1)
輸出:
[2 4 6]
Python 中錯誤 ValueError: Expected 2D array, got 1D array instead 的原因
當(dāng)您在函數(shù)中傳遞一維數(shù)組時會發(fā)生此錯誤。 但是,該函數(shù)需要一個二維數(shù)組,因此您傳遞的不是一個二維數(shù)組,而是一個單一維度的數(shù)組。
它主要發(fā)生在 predict() 方法中使用機(jī)器學(xué)習(xí)算法。
現(xiàn)在讓我們來看看這個場景。
import numpy as np from sklearn import svm X = np.array([[2,1], [4,5], [2.6,3.5], [6,6], [0.8,1], [7,10]]) y = [1,0,1,0,1,0] classifier = svm.SVC(kernel="linear", C = 1.0) classifier.fit(X,y) print(classifier.predict([0.7,1.10]))
輸出:

修復(fù) Python 中錯誤 ValueError: Expected 2D array, got 1D array instead
對數(shù)據(jù)使用雙方括號
下面我們已經(jīng)解決了前面例子中的錯誤。 修復(fù)錯誤的最簡單方法是將維數(shù)組轉(zhuǎn)換為二維數(shù)組。
我們可以將 [0.7,1.10] 括在另一個方括號中,以便在將其傳遞給 predict() 方法時將其轉(zhuǎn)換為二維數(shù)組。
示例代碼:
import numpy as np from sklearn import svm X = np.array([[2,1], [4,5], [2.6,3.5], [6,6], [0.8,1], [7,10]]) y = [1,0,1,0,1,0] classifier = svm.SVC(kernel="linear", C = 1.0) classifier.fit(X,y) print(classifier.predict([[0.7,1.10]]))
輸出:
[1]
使用 reshape() 重塑數(shù)組
將一維數(shù)組轉(zhuǎn)換為二維數(shù)組的另一種方法是使用 reshape() 方法重塑數(shù)組。 您可以使用 reshape() 方法在 Python 中重塑數(shù)組。
每個維度中元素的數(shù)量決定了數(shù)組的形狀。 您可以使用重塑來添加或刪除數(shù)組維度。
在下面的代碼中,您可以看到使用 reshape() 方法前后 numpy 數(shù)組的維度。
示例代碼:
import numpy as np
from sklearn import svm
X = np.array([[2,1],
[4,5],
[2.6,3.5],
[6,6],
[0.8,1],
[7,10]])
y = [1,0,1,0,1,0]
classifier = svm.SVC(kernel="linear", C = 1.0)
classifier.fit(X,y)
test=np.array([0.7,1.10])
print("Dimension before:", test.ndim)
test=test.reshape(1, -1)
print("Dimension now:", test.ndim)
print("Classifier Result:", classifier.predict(test))輸出:
Dimension before: 1
Dimension now: 2
Classifier Result: [1]
到此這篇關(guān)于解決Python報錯Valueerror: Expected 2d Array Got 1d Array Instead的文章就介紹到這了,更多相關(guān)Python報錯Valueerror內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python ValueError: invalid literal for int() with base 10 實(shí)用解決方法
- Python異常?ValueError的問題
- 解決Python報錯:ValueError:operands?could?not?be?broadcast?together?with?shapes
- Python中ValueError報錯的原因和解決辦法
- Python報錯ValueError: cannot reindex from a duplicate axis的解決方法
- Python報錯ValueError:?cannot?convert?float?NaN?to?integer的解決方法
- 解決Python報錯ValueError list.remove(x) x not in list問題
- Python中異常類型ValueError使用方法與場景
- Python ValueError: all input arrays must have the same shap的問題解決
相關(guān)文章
python實(shí)現(xiàn)指定字符串補(bǔ)全空格的方法
這篇文章主要介紹了python實(shí)現(xiàn)指定字符串補(bǔ)全空格的方法,涉及Python中rjust,ljust和center方法的使用技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
Python實(shí)現(xiàn)爬取亞馬遜數(shù)據(jù)并打印出Excel文件操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)爬取亞馬遜數(shù)據(jù)并打印出Excel文件操作,結(jié)合實(shí)例形式分析了Python針對亞馬遜圖書數(shù)據(jù)的爬取操作,以及數(shù)據(jù)打印輸出Excel相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05
Python中操作MySQL數(shù)據(jù)庫時如何處理數(shù)據(jù)類型轉(zhuǎn)換問題
日常工作中,我們難免會遇到要直連數(shù)據(jù)庫來進(jìn)行數(shù)據(jù)的讀取與處理,然而會有幾種比較有特點(diǎn)的數(shù)據(jù)需要進(jìn)行轉(zhuǎn)換,這篇文章主要介紹了Python中操作MySQL數(shù)據(jù)庫時如何處理數(shù)據(jù)類型轉(zhuǎn)換問題的相關(guān)資料,需要的朋友可以參考下2026-01-01
python使用matplotlib畫出的圖怎樣放到word中
這篇文章主要介紹了python使用matplotlib畫出的圖怎樣放到word中問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

