I am a rookie in data science and I am confused about some part of ML.

My problems:

If I use a package of linear-model like LiearRegression() or PolynomialFeatures():

  • Should I to fit my (x_train, y_train) or only is enough when I fit my (x_train)?

  • Should I to transform my (x_train, y_train)?

  • Should I fit my (x_test) too or it will be only transformed? If only transform why?

    from sklearn.linear_model import LinearRegression()
    LinReg.fit(x_train) or LinReg.fit(x_train,y_train) ???? ;-|

2 Answers 2


When you are fitting a supervised learning ML model (such as linear regression) you need to feed it both the features and labels for training. The features are your X_train, and the labels are your y_train. In your case:

from sklearn.linear_model import LinearRegression
LinReg = LinearRegression()
LinReg.fit(X_train, y_train)

If you are performing some unsupervised learning approach, or simply some data transformation (such as PolynomialFeatures) you simply fit on your feature space (X_train) since there are no labels required for such an approach. Like so:

from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(2)
X_train_transformed = poly.transform(X_train)

For your second point - depending on your approach you might need to transform your X_train or your y_train. It's entirely dependent on what you're trying to do.

As for your last point - never ever fit on testing data. It defeats the purpose of a train/test split. Usually what is done is your pipeline step is fit either with X_train and y_train or just X_train alone. This fit transformer can then be applied to your testing data (X_test) using the .transform() method but never use this data for .fit()


What you want to do is teach a model how to predict something using your train set, and test it, like in real conditions, with a test set.

For that you have to provide the model some train data associated with the known result, so the model can learn which patern is usually labeled 1 and which one is usually labaled 0. So you have to fit your model giving X_train (data) and y_train (targets, 1 or 0).

Now you want to test your model, so you want to transform X_test USING THE MODEL YOU TRAINED WITH YOUR TRAINING SET, to test your model in "real conditions", so just transform your X_test. Then, you can compare outputs from the X_test transformed to your known test targets (y_test) to evaluate the model performance.

If you fit the model on part or all your test set, you make a mistake, since the data you use to evaluate the model are the one used to train it, so the model may overperform, and your evaluation will be wrong.

I'd really suggest you following some courses before going in practice, in Data Science, there are many issues that you might not see and that will make you have wrong results.

  • $\begingroup$ Thank u so much for the answer. Honestly, I followed some courses about ML but they were too much theory! But theory and praxis are a little bit different as you know. If you know some practice course about ML, it will be great to recommend it to me. $\endgroup$
    – Jsmoka
    Dec 31, 2020 at 9:48
  • $\begingroup$ I've been taught ML in the University so I won't be able to provide links to courses in the Internet, sorry $\endgroup$
    – Adept
    Jan 4, 2021 at 7:57

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.