簡體   English   中英

Python SKLearn:預測序列時出現“輸入形狀錯誤”錯誤

[英]Python SKLearn: 'Bad input shape' error when predicting a sequence

我有一個Excel文件,該文件在每個列中存儲一個序列(從頂部單元格到底部單元格讀取),並且該序列的趨勢類似於上一列。 因此,我想預測此數據集中第n列的順序。

我的數據集樣本:

樣本數據

看到每個列都有一組值/序列,並且隨着我們向右移動它們會有所進展,因此我想預測例如Z列中的值。

到目前為止,這是我的代碼:

import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

# Read the Excel file in rows
df = pd.read_excel(open('vec_sol2.xlsx', 'rb'),
                header=None, sheet_name='Sheet1')
print(type(df))
length = len(df.columns)
# Get the sequence for each row

x_train, x_test, y_train, y_test = train_test_split(
    np.reshape(range(0, length - 1), (-1, 1)), df, test_size=0.25, random_state=0)

print("y_train shape: ", y_train.shape)

pred_model = LogisticRegression()
pred_model.fit(x_train, y_train)
print(pred_model)

我將盡可能解釋邏輯:

  • x_trainx_test將只是與序列關聯的索引/列號。
  • y_train是序列的數組。
  • 總共有51列,因此將其拆分為25%的測試數據可得到37個訓練序列和13個測試序列。

我設法在調試時獲取每個var的形狀,它們是:

  • x_train :( x_train
  • x_test :( x_test
  • y_train :( y_train
  • y_test :( y_test

但是現在,運行程序給我這個錯誤:

ValueError: bad input shape (37, 51)

我這是什么錯

我不明白您為什么使用這個:

x_train, x_test, y_train, y_test = train_test_split(
np.reshape(range(0, length - 1), (-1, 1)), df, test_size=0.25, random_state=0)

您在df有數據。 從中提取Xy ,然后將其拆分以進行訓練和測試。

嘗試這個:

X = df.iloc[:,:-1]
y = df.iloc[:, -1:]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0)

否則,您共享的統計信息表明您正試圖從一項功能中獲得51列輸出,如果考慮一下,這是很奇怪的。

暫無
暫無

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

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