Akanksha Gavhane’s Post

🚀 Day 6 of 7: Learning Machine Learning from O’Reilly’s Introduction to Machine Learning with Python Today’s concept: Method Chaining 🔗 If you’ve ever seen lines like this in Python 👇 df.dropna().groupby('category').mean().reset_index() and wondered “What’s going on here?”, that’s method chaining in action! 🚀 📌 What it means: Method chaining is when you call multiple methods sequentially on the same object — without creating temporary variables each time. Each method returns an object, allowing the next method to be called directly. ⚙️ Without chaining (same logic): cleaned = df.dropna() grouped = cleaned.groupby('category')['value'].mean() result = grouped.reset_index() Both work — but chaining feels smoother and more elegant 💫 💡Reference from the book: Introduction to Machine Learning with Python (pg-68) Common application of method chaining inscikit-learn is to fit and predict in one line: logreg = LogisticRegression() y_pred = logreg.fit(X_train, y_train).predict(X_test) Finally, you can even do model instantiation, fitting, and predicting in one line: y_pred = LogisticRegression().fit(X_train, y_train).predict(X_test) 👉 Note: This very short variant is not ideal, though. A lot is happening in a single line, which might make the code hard to read. Additionally, the fitted logistic regression model isn’t stored in any variable, so we can’t inspect it or use it to predict on any other data. #MachineLearning #DataScience #Python #scikitLearn #OReilly #LearningJourney #AI

To view or add a comment, sign in

Explore content categories