# When should i use fit(x_train) and when should i fit( x_train,y_train)?

I am a rookie in data science and I'm 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_tarin) or only is enough when i fit my (x_train)?
• Should i to transform my (x_train,y_tarin)?
• Should i fit my (x_test) too or it will be only transformed? If only transform why?
from sklearn.linear_model import LinearRegression()
LinReg=LinearRegression()
LinReg.fit(x_train) or LinReg.fit(x_train,y_train) ???? ;-|
LinReg.transforn(x_test)


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)
poly.fit(X_train)
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.

• 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. – Jsmoka Dec 31 '20 at 9:48
• I've been taught ML in the University so I won't be able to provide links to courses in the Internet, sorry – BeamsAdept Jan 4 at 7:57