Fit vs. Transform in SciKit libraries for Machine LearningData Science by Sunny Srinidhi - November 7, 2019November 7, 20190 We have seen methods such as fit(), transform(), and fit_transform() in a lot of SciKit’s libraries. And almost all tutorials, including the ones I’ve written, only tell you to just use one of these methods. The obvious question that arises here is, what do those methods mean? What do you mean by fit something and transform something? The transform() method makes some sense, it just transforms the data, but what about fit()? In this post, we’ll try to understand the difference between the two.To better understand the meaning of these methods, we’ll take the Imputer class as an example, because the Imputer class has these methods. But before we get started, keep in mind that fitting something like an imputer is different from fitting a whole model. You use an Imputer to handle missing data in your dataset. Imputer gives you easy methods to replace NaNs and blanks with something like the mean of the column or even median. But before it can replace these values, it has to calculate the value that will be used to replace blanks. If you tell the Imputer that you want the mean of all the values in the column to be used to replace all the NaNs in that column, the Imputer has to calculate the mean first. This step of calculating that value is called the fit() method.Next, the transform() method will just replace the NaNs in the column with the newly calculated value, and return the new dataset. That’s pretty simple. The fit_transform() method will do both the things internally and makes it easy for us by just exposing one single method. But there are instances where you want to call only the fit() method and only the transform() method.When you are training a model, you will use the training dataset. On this dataset, you’ll use the Imputer, calculate the value, and replace the blanks. But when you fit this trained model on the test dataset, you don’t calculate the mean or median again. You’ll use the same value that you used on you training dataset. For this, you’ll use the fit() method on your training dataset to only calculate the value and keep it internally in the Imputer. Then, you’ll call the transform() method on the test dataset with the same Inputer object. This way, the value calculate for the training set, which was saved internally in the object, will be used on the test dataset as well.To put it simply, you can use the fit_transform() method on the training set, as you’ll need to both fit and transform the data, and you can use the fit() method on the training dataset to get the value, and later transform() test data with it. Let me know if you have any comments or are not able to understand it. Share this:TwitterFacebookLike this:Like Loading...Related