Python Forum
Receiving ValueError("bad input shape {0}".format(shape)) error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receiving ValueError("bad input shape {0}".format(shape)) error
#1
Hi,

I'm trying to run the below code but receiving "ValueError("bad input shape {0}".format(shape))" error. Can someone please help me resolve this issue? Note: The dataset has 5 rows.https://github.com/SuryaCitizen/Data


#Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#Importing the dataset
dataset = pd.read_csv('Lingard.csv')
X = dataset.iloc[:, :-2].values
y = dataset.iloc[:, 7:9].values
print(dataset.head())

#Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

print(X_train)
print(X_test)
print(y_train)
print(y_test)

#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X_train = sc_X.fit_transform(X_train)
y_train = sc_y.fit_transform(y_train)

#Training the SVR model on the Training set
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X_train, y_train)

#Predicting the Test set results
y_pred = sc_y.inverse_transform(regressor.predict(sc_X.transform(X_test)))
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),2), y_test.reshape(len(y_test),2)),1))

#Evaluating the Model Performance
from sklearn.metrics import r2_score
r2_score(y_test, y_pred)
Reply
#2
Please find below complete traceback.


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#Importing the dataset
dataset = pd.read_csv('Lingard.csv')
X = dataset.iloc[:, :-2].values
y = dataset.iloc[:, 7:9].values
print(dataset.head())

#Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

print(X_train)
print(X_test)
print(y_train)
print(y_test)

#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X_train = sc_X.fit_transform(X_train)
y_train = sc_y.fit_transform(y_train)

#Training the SVR model on the Training set
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X_train, y_train)

#Predicting the Test set results
y_pred = sc_y.inverse_transform(regressor.predict(sc_X.transform(X_test)))
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),2), y_test.reshape(len(y_test),2)),1))

#Evaluating the Model Performance
from sklearn.metrics import r2_score
r2_score(y_test, y_pred)
Year Matches Shots On target ... Passes passes completed Goals Assists
0 2019 19 24 14 ... 29 86 4 3
1 2018 20 41 23 ... 29 88 8 5
2 2017 18 25 13 ... 30 88 1 2
3 2016 19 23 13 ... 30 85 4 1
4 2015 7 14 8 ... 44 85 2 1

[5 rows x 9 columns]
[[2018 20 41 23 18 29 88]
[2016 19 23 13 10 30 85]
[2015 7 14 8 6 44 85]]
[[2017 18 25 13 12 30 88]
[2019 19 24 14 10 29 86]]
[[8 5]
[4 1]
[2 1]]
[[1 2]
[4 3]]
Traceback (most recent call last):

File "<ipython-input-2-54cb4f9ef960>", line 30, in <module>
regressor.fit(X_train, y_train)

File "C:\Users\Surya\Anaconda3\lib\site-packages\sklearn\svm\base.py", line 146, in fit
accept_large_sparse=False)

File "C:\Users\Surya\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 724, in check_X_y
y = column_or_1d(y, warn=True)

File "C:\Users\Surya\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 760, in column_or_1d
raise ValueError("bad input shape {0}".format(shape))

ValueError: bad input shape (3, 2)
Reply
#3
Please use proper input/error/output tags while posting a thread - see BBCode to know more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output shape problem with np.arange alan6690 5 610 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 1,911 Aug-13-2023, 12:16 AM
Last Post: cubangt
  simplekml change shape&color issac_n 2 2,772 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,407 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Invalid format specifier Error Led_Zeppelin 2 7,597 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Error in Using INPUT statement gunwaba 1 2,016 Jul-03-2022, 10:22 PM
Last Post: deanhystad
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,433 Jun-16-2022, 06:59 PM
Last Post: saoko
  Date format error getting weekday value Aggie64 2 1,378 May-29-2022, 07:04 PM
Last Post: Aggie64
  Receiving snmp traps with more than one Community String ilknurg 0 2,152 Jan-19-2022, 09:02 AM
Last Post: ilknurg

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020