Deep Dive Into Supervised Learning with Scikit-Learn
If you’re building intelligent systems that learn from labeled data, supervised learning is your bread and butter. Scikit-learn provides a rich, extensible toolbox of models to tackle a wide range of supervised learning problems: from simple linear regression to sophisticated ensemble models. In this article, we explore a curated set of supervised learning tools that go beyond the basics.
1. Linear Models in Practice
1.1 Ordinary Least Squares (OLS)
The default method for linear regression, OLS minimizes the residual sum of squares between the observed and predicted values. It’s efficient, interpretable, and fast — but prone to overfitting in high dimensions.
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(X_train, y_train)
1.2 Ridge, Lasso, ElasticNet
- Ridge Regression: Adds L2 penalty to reduce coefficient magnitudes.
- Lasso: Adds L1 penalty, enabling sparse solutions by zeroing out weak features.
- ElasticNet: Combines both penalties. Useful when features are correlated.
from sklearn.linear_model import Ridge, Lasso, ElasticNet
model = Lasso(alpha=0.1).fit(X_train, y_train)
1.3 Logistic Regression
Used for classification tasks — yes, despite the name. Models the probability of classes using a sigmoid function.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit(X_train, y_train)
2. Discriminant Analysis
2.1 Linear Discriminant Analysis (LDA)
Projects features into a lower-dimensional space that maximizes class separability.
2.2 Quadratic Discriminant Analysis (QDA)
Assumes each class has its own covariance structure. More flexible, but needs more data.
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis().fit(X, y)
3. Support Vector Machines (SVMs)
Powerful classifiers for high-dimensional spaces. Scikit-learn includes support for both classification and regression:
from sklearn.svm import SVC
svc = SVC(kernel='rbf').fit(X_train, y_train)
Use kernels (linear, RBF, polynomial) to adapt to nonlinear patterns. For large datasets, try SGDClassifier(loss='hinge')
as a linear approximation.
4. Naive Bayes Classifiers
These are probabilistic models based on Bayes’ theorem. Scikit-learn supports several variants:
- GaussianNB: For continuous features
- MultinomialNB: For counts (e.g., text data)
- BernoulliNB: For binary/boolean features
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB().fit(X_train, y_train)
5. Decision Trees and Ensemble Models
5.1 Decision Trees
Simple, interpretable models that split data based on feature thresholds. Prone to overfitting unless pruned.
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=4).fit(X_train, y_train)
5.2 Random Forests and Gradient Boosting
Ensemble models average multiple trees to improve generalization.
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100).fit(X_train, y_train)
Other ensemble models include:
- GradientBoostingClassifier: Boosting trees sequentially.
- BaggingClassifier: Random sampling with replacement.
- VotingClassifier: Combine different model types.
- StackingClassifier: Meta-learning over base classifiers.
6. Neural Networks (MLP)
Scikit-learn offers feedforward neural networks via MLPClassifier
and MLPRegressor
. These models use backpropagation and support multiple layers, activation functions, and L2 regularization.
from sklearn.neural_network import MLPClassifier
model = MLPClassifier(hidden_layer_sizes=(100,)).fit(X_train, y_train)
7. Feature Selection & Dimensionality Reduction
Eliminate noise and reduce overfitting:
SelectKBest
andSelectPercentile
RFE
(Recursive Feature Elimination)SelectFromModel
with any estimator
from sklearn.feature_selection import SelectKBest, f_classif
X_new = SelectKBest(f_classif, k=10).fit_transform(X, y)
8. Final Words
This blog only scratches the surface of what scikit-learn offers in the supervised learning space. The library continues to evolve, adding more flexible estimators, robust pipelines, and tuning capabilities. Whether you’re a beginner or building production systems, scikit-learn offers an excellent foundation.
For more detail, check the official scikit-learn documentation.
Want to see a hands-on example? Stay tuned for the next post where we’ll implement a real-world supervised learning pipeline from scratch.
Leave a Reply