1
$\begingroup$

So, I've been trying to split my dataset into a 70-30 ratio using train_test_split in order to work things out with sklearn's PLS. However, I encountered an error that says:

# Read data
df = pd.read_csv(
    filepath_or_buffer='colontumor.csv',
    header=None,
    sep=',')

df.tail()


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

# Split data to train and test on 70-30 ratio
X_train, X_test = train_test_split(
    X, test_size=0.30, random_state=None)

pls = PLSRegression(n_components=2)
pls.fit(X_train, X_test)
Y_pred = pls2.predict(X_train)

and somehow encountered this error:

Line (17): pls.fit(X_train, X_test)
ValueError: Found input variables with inconsistent numbers of samples: [43, 19]

Is there any solution for this? I've been circling around for like hours now.

$\endgroup$
2
  • $\begingroup$ Could you edit your question to show which line throws the error? $\endgroup$
    – bkshi
    Jun 5, 2019 at 18:27
  • $\begingroup$ @bkshi done, i'm not sure if it's the sklearn or the data splitting. $\endgroup$ Jun 5, 2019 at 18:42

2 Answers 2

1
$\begingroup$

The line that gives the error is:

pls.fit(X_train, X_test)

The second argument in fit should be your labels, the value you want your model to be able to predict. Instead you are inputting X_test, which contains the same features as X_train but with another length, causing your error. You want an y_train with your target value and make it look like this:

pls.fit(X_train, y_train)

But to get this value you will need to get your labels from you dataframe. It is hard to know how without knowing the data, but this might give you an idea:

X = df.drop(['name of label column'], axis=1).iloc[:, :-1].values
y = df['name of label column'].iloc[:, :-1].values

# Split data to train and test on 70-30 ratio
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

pls = PLSRegression(n_components=2)
pls.fit(X_train, y_train)
$\endgroup$
0
$\begingroup$

I have not worked with PLSRegression before but the problem lies in your input. From the docs, PLSRegression().fit() expects both the inputs to have the same number of rows, but that is clearly not true in your case. When you apply train_test_split() on X, you just split the dataframe by the number of rows in 70:30 ratio and I'm assuming that's not what you want. (I hope I am making sense.)

Just try printing your dataframes before inputting them to the model and check if that's what you really want.

$\endgroup$
1
  • $\begingroup$ Ah yes, i somehow managed to do it. By changing the ratio from 70:30 to 50:50 the problem is immediately resolved. I honestly thought the PLS concept uses the same method as classifications when it comes to data splitting but i never knew it's supposed to be equally split. Thanks for the help! $\endgroup$ Jun 5, 2019 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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