54
votes

Could you please explain what the "fit" method in scikit-learn does? Why is it useful?

1
Question is broad, but I'll bite. Plenty of models have fit methods in scikit-learn. When you call fit method it estimates the best representative function for the the data points (could be a line, polynomial or discrete borders around). With that representation, you can calculate new data points. Take linear_regression for example: when you call fit on a dataset of points, it'll give you a function that represents a line that is best fits all the points. With that line function you can estimate other results. I'd advise you to read on line fitting for simple understanding. - umutto
Hi "umutto"Many Thank . Can you provide me the link to read about "line fitting" ? - Pearapon Joe
Sure, you can google for line fitting, or linear regression and it should give you an insight. Better yet, if you want to follow an intuitive introduction to machine learning I'd advise you to follow Andrew Ng's coursera. It is great for an easy to understand start to machine learning. Here is the line fitting, estimation part from his course. - umutto
This is something you can easily google or read about in any intro data science tutorial. - DYZ
I think this is a great question, as long as you do something to focus it. Consider focusing it to using fit WITH a specific use case, as in machine learning model, preprocessing step, etc. - Kevin Glynn

1 Answers

78
votes

In a nutshell: fitting is equal to training. Then, after it is trained, the model can be used to make predictions, usually with a .predict() method call.

To elaborate: Fitting your model to (i.e. using the .fit() method on) the training data is essentially the training part of the modeling process. It finds the coefficients for the equation specified via the algorithm being used (take for example umutto's linear regression example, above).

Then, for a classifier, you can classify incoming data points (from a test set, or otherwise) using the predict method. Or, in the case of regression, your model will interpolate/extrapolate when predict is used on incoming data points.

It also should be noted that sometimes the "fit" nomenclature is used for non-machine-learning methods, such as scalers and other preprocessing steps. In this case, you are merely "applying" the specified function to your data, as in the case with a min-max scaler, TF-IDF, or other transformation.

Note: here are a couple of references...