簡體   English   中英

訓練集中的訓練數據

[英]Training data in a training set

上午,嘗試使用scikit-learn在我的訓練集中訓練模型,但出現此錯誤:

 ValueError: Expected 2D array, got 1D array instead: array=[90.  4.].
 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.

步驟 1:將 x 和 y 拆分為訓練和測試集

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.4, random_state = 4)

檢查新分割的 x 值的形狀(訓練和測試)

X_train = X_train.shape
X_test = X_test.shape
print(X_train)
print(X_test)

檢查新分割的 y 值的形狀(訓練和測試)

y_train = y_train.shape
y_test = y_test.shape
print(y_train)
print(y_test)

第 2 步:在訓練集上訓練我們的模型(使用邏輯回歸)

logR = LogisticRegression()
logR = logR.fit(X_train, y_train)

運行此代碼我收到錯誤

看來您正在用它們的形狀替換數據點:

X_train = X_train.shape
X_test = X_test.shape
y_train = y_train.shape
y_test = y_test.shape

刪除這些行並重新運行。

您做得很好,但您做錯了一件事:您將訓練和測試數據替換為一維形狀,這就是您面臨此錯誤的原因

 #replace these line

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.4, random_state = 4)

print( X_train.shape)
print( X_test.shape)
print(y_train.shape)
print(y_test.shape)

logR = LogisticRegression()
logR = logR.fit(X_train, y_train)

# Now it work fine

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM