4
$\begingroup$

I would appreciate if you could let me know how to resolve this error: Code:

X = np.array(pd.read_csv('my_X_table1-1c.csv',header=None).values)
y = np.array(pd.read_csv('my_y_table1-1c.csv',header=None).values.ravel())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=7)

def Ridgecv(alpha):
    return cross_val_score(Ridge(alpha=float(alpha), random_state=2),
                           X_train, y_train, 'mae', cv=5).mean()

The error is related to X_train, y_train:

ValueError: Found input variables with inconsistent numbers of samples: [1052, 1052, 3]

regards,

$\endgroup$
  • $\begingroup$ what's the result for X_train.shape and y_train.shape? $\endgroup$ – oW_ Jan 31 '17 at 20:47
  • $\begingroup$ @oW_ Thanks. They respectively are (1052, 60) and (1052, ). $\endgroup$ – ebrahimi Jan 31 '17 at 21:11
  • $\begingroup$ Try y_train.reshape((1052,1)) $\endgroup$ – oW_ Jan 31 '17 at 21:16
  • $\begingroup$ @oW_ Thanks. I tried y_train.reshape(len(y_train),1) but unfortunately the same error is reported. $\endgroup$ – ebrahimi Feb 1 '17 at 5:53
6
$\begingroup$

It seems that I missed the word "scoring". In fact, the extra 3 was related to the number of characters of 'mae'.

def Ridgecv(alpha):
    return cross_val_score(Ridge(alpha=float(alpha), random_state=2),
                           X_train, y_train, scoring='mae', cv=5).mean()
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Wow had the same bug ... hard one to spot! Thanks for posting this. $\endgroup$ – tdc Sep 4 '18 at 14:39
-1
$\begingroup$

It should be in sequence:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X,Y,random_state=101,test_size=0.3)

and then it should be in fit method(x_train,y train)

|improve this answer|||||
$\endgroup$
  • $\begingroup$ sorry, the error is related to how to define parameters of cross_val_score. $\endgroup$ – ebrahimi Jul 29 '19 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.