AI & ML6 min read

Machine Learning Fundamentals

Build ML models with scikit-learn - supervised learning, feature engineering, cross-validation, hyperparameter tuning, and avoiding overfitting.

Machine Learningscikit-learnModel EvaluationFeature EngineeringCross-Validation

skill: Machine Learning Fundamentals category: AI & ML tags: [Machine Learning, scikit-learn, Model Evaluation, Feature Engineering, Cross-Validation] readingTime: 6

Most ML projects fail not because of wrong algorithms but because of bad data, wrong evaluation metrics, or data leakage. The algorithm matters far less than data quality, proper train/test splitting, and choosing the right metric for the actual business goal. Start simple (linear models) and add complexity only when simpler models fail.

Workflow

  1. Explore and prepare dataGarbage in, garbage out. EDA reveals distribution issues, outliers, class imbalance, and missing values before they corrupt model training.
  2. Engineer featuresRaw data rarely has the right representation for ML models. Feature engineering creates informative signals that models can learn from.
  3. Train and evaluate modelsDifferent algorithms have different bias-variance tradeoffs. Start with simple baselines; add complexity only when simpler models underfit.
  4. Tune hyperparametersDefault hyperparameters are rarely optimal. Systematic search finds better settings without manual guessing.
  5. Interpret model predictionsModel interpretability builds trust, helps debug, and is often required for regulatory compliance. Feature importance + SHAP values explain why the model made each prediction.

Output Format

ML: train_test_split with stratify, ColumnTransformer pipeline, cross_val_score 5-fold, RandomizedSearchCV, and SHAP feature importance.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
Pipeline([("preprocessor", ColumnTransformer(...)), ("model", RF())])
cross_val_score(pipe, X_train, y_train, cv=5, scoring="roc_auc")
RandomizedSearchCV(pipe, param_dist, n_iter=50, cv=5)
shap.TreeExplainer + summary_plot

Examples

Input

Output

Edge Cases & Gotchas

  • Class imbalance - With 95% negative / 5% positive, a model predicting "always negative" gets 95% accuracy. Use class_weight="balanced", SMOTE oversampling, or ROC-AUC instead of accuracy.
  • Target leakage - Features that encode the outcome (e.g., "refund_amount" in a churn model) artificially inflate validation scores but fail in production. Review each feature: could this be known before the outcome occurs?
  • Cross-validation with time series - Random k-fold CV leaks future data into training for time series. Use TimeSeriesSplit instead - training sets only contain data before the test fold.
  • Pipeline and GridSearch compatibility - When tuning pipeline hyperparameters in GridSearchCV, prefix parameter names with the step name + "__": "model__n_estimators", not "n_estimators".

When to Break These Rules

  • Use neural networks instead of scikit-learn for raw text, images, or audio - these modalities need deep learning feature extraction.
  • Use XGBoost or LightGBM directly instead of sklearn GradientBoosting for tabular data at scale - they're 10-100× faster with better defaults.

Reference Files

  • https://scikit-learn.org/stable/user_guide.html
  • https://shap.readthedocs.io/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.