close

[Solved] ValueError: x and y must be the same size

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: x and y must be the same size in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: x and y must be the same size Error Occurs?

Today I get the following error ValueError: x and y must be the same size in python.

How To Solve ValueError: x and y must be the same size Error ?

  1. How To Solve ValueError: x and y must be the same size Error ?

    To Solve ValueError: x and y must be the same size Error Print X_train shape. What do you see? I'd bet X_train is 2d (matrix with a single column), while y_train 1d (vector). In turn you get different sizes.

  2. ValueError: x and y must be the same size

    To Solve ValueError: x and y must be the same size Error Print X_train shape. What do you see? I'd bet X_train is 2d (matrix with a single column), while y_train 1d (vector). In turn you get different sizes.

Solution 1

Print X_train shape. What do you see? I’d bet X_train is 2d (matrix with a single column), while y_train 1d (vector). In turn you get different sizes.

I think using X_train[:,0] for plotting (which is from where the error originates) should solve the problem

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read