ValueError: x and y must be the same size

后端 未结 4 1671
抹茶落季
抹茶落季 2020-12-11 03:16
import numpy as np
import pandas as pd
import matplotlib.pyplot as pt

data1 = pd.read_csv(\'stage1_labels.csv\')

X = data1.iloc[:, :-1].values
y = data1.iloc[:, 1]         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 03:49

    Slicing with [:, :-1] will give you a 2-dimensional array (including all rows and all columns excluding the last column).

    Slicing with [:, 1] will give you a 1-dimensional array (including all rows from the second column). To make this array also 2-dimensional use [:, 1:2] or [:, 1].reshape(-1, 1) or [:, 1][:, None] instead of [:, 1]. This will make x and y comparable.


    An alternative to making both arrays 2-dimensional is making them both one dimensional. For this one would do [:, 0] (instead of [:, :1]) for selecting the first column and [:, 1] for selecting the second column.

提交回复
热议问题