Back to Study Material Python

Scikit-learn

Scikit-learn (sklearn) is the most widely used machine learning library in Python. It provides consistent, well-documented implementations of supervised an...

Scikit-learn

Machine Learning with Scikit-learn

Scikit-learn (sklearn) is the most widely used machine learning library in Python. It provides consistent, well-documented implementations of supervised and unsupervised learning algorithms, along with tools for model selection, preprocessing, and evaluation.

The three pillars of Scikit-learn are estimators (models), transformers (preprocessing), and pipelines (combining both).

Supervised learning algorithms available in Scikit-learn: - Classification: LogisticRegression, DecisionTreeClassifier, RandomForestClassifier, SVC, KNeighborsClassifier, GradientBoostingClassifier - Regression: LinearRegression, Ridge, Lasso, ElasticNet, SVR, RandomForestRegressor

Unsupervised learning: KMeans, DBSCAN, PCA, IsolationForest, AgglomerativeClustering.

Classification example with evaluation: from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import classification_report, confusion_matrix

rf = RandomForestClassifier(n_estimators=100, random_state=42) rf.fit(X_train, y_train) y_pred = rf.predict(X_test) print(classification_report(y_test, y_pred)) print(confusion_matrix(y_test, y_pred))

The classification_report prints precision, recall, F1-score, and support for each class. This is more informative than accuracy alone, especially for imbalanced datasets.

Feature importance from tree-based models reveals which variables drive predictions: importances = rf.feature_importances_ feat_names = X.columns for name, imp in sorted(zip(feat_names, importances), key=lambda x: -x[1]): print(f'{name}: {imp:.4f}')

Dimensionality reduction with PCA: from sklearn.decomposition import PCA pca = PCA(n_components=2) X_reduced = pca.fit_transform(X_scaled) print(pca.explained_variance_ratio_) # how much variance each component captures

Practical tip: Use RandomForestClassifier as a strong baseline before trying more complex models. It handles missing features poorly but is robust to outliers and requires minimal tuning.

Exam tip: Know the difference between predict() (returns class labels) and predict_proba() (returns probability per class). Many interview questions ask which to use when building a ranking system or calibrating a threshold.