Fit vs Fit_transform

Have you ever wondered whats the difference between fit() and fit_transform(). you must have came across these 2 functions somewhere while preprocessing your data. So, lets learn the difference between fit and fit_transform. we are going to understand this using an example

whenever you want to perform standardization which is an essential preprocessing step, you typically need to calculate various parameters of the data like mean, min, max, variance. fit_transform calculates these parameters and applies to the dataset, where as fit calculates these parameters but doesn’t apply to the dataset.

Lets assume this small array of data
data = [[1,2,3],[4,5,6],[7,8,9]]

when you apply standard scaler and use fit and transform seperately:

from sklearn.preprocessing import StandardScaler

# step-1
Scaler = StandardScaler()

# step-2
scaled_data = Scaler.fit(data) # no scaling of data takes place here ,just the mean and std deviation are calculated. 

# step-3
scaled_data = Scaler.transform() # now the scaled data contains the data after performing standardization. 

Enter fullscreen mode Exit fullscreen mode

when you apply fit_transform instead of fit and transform seperately.

from sklearn.preprocessing import StandardScaler

# step-1
Scaler = StandardScaler()

# step-2
scaled_data = Scaler.fit_transform(data) # scaled_data contains the data after performing standardization. 

Enter fullscreen mode Exit fullscreen mode

we can observe that by using fit_transform() we are essentially reducing an extra step

which one to use purely depends upon your usecase. If you want to learn parameters for once and then apply transformations to multiple datasets like training set and testing set, using fit and transform seperately is preferred. but if you want to apply transformation to a single dataset, use fit_transform() which makes the preprocessing pipeline concise.

原文链接:Fit vs Fit_transform

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容