Python ML Environment Setup
A professional ML environment uses virtual environments to isolate project dependencies, Jupyter notebooks for exploration, and version pinning to ensure reproducibility. The four essential libraries: NumPy (arrays), Pandas (tables), Matplotlib/Seaborn (plots), Scikit-learn (models). Additional libraries for advanced work: XGBoost, LightGBM, Optuna, MLflow.
Environment Setup and Core Imports
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# ENVIRONMENT SETUP (run in terminal)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Create isolated environment
# python -m venv ml_env
# source ml_env/bin/activate # Linux/Mac
# ml_env\Scripts\activate # Windows
# Install all required libraries
# pip install numpy pandas matplotlib seaborn scikit-learn
# pip install xgboost lightgbm optuna mlflow joblib
# pip install jupyter notebook ipykernel
# pip install imbalanced-learn shap
# Pin versions for reproducibility
# pip freeze > requirements.txt
# pip install -r requirements.txt # reproduce exact environment later
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# STANDARD IMPORTS -- every ML notebook starts here
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Core sklearn
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score, classification_report, mean_squared_error, r2_score
# Models
from sklearn.linear_model import LinearRegression, LogisticRegression, Ridge, Lasso
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
# Print versions
import sklearn
print(f"NumPy: {np.__version__}")
print(f"Pandas: {pd.__version__}")
print(f"Scikit-learn: {sklearn.__version__}")
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# RECOMMENDED PROJECT STRUCTURE
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
project_structure = """
ml-project/
data/
raw/ <- original, never modified
processed/ <- cleaned, ready for modeling
notebooks/
01_eda.ipynb
02_preprocessing.ipynb
03_modeling.ipynb
src/
data_prep.py
train.py
predict.py
models/ <- saved model files (.joblib)
tests/
requirements.txt
README.md
"""
print(project_structure)Tip
Tip
Practice Python ML Environment Setup in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
uv = fastest. Poetry for projects.
Practice Task
Note
Practice Task — (1) Write a working example of Python ML Environment Setup from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Python ML Environment Setup is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready ml code.