Machine Learning Getting Started
Learn what machine learning is, explore common workflows, and prepare your Python environment for modeling.
What Is Machine Learning?
Machine learning (ML) builds predictive or descriptive models from data. Instead of hard-coding rules, algorithms learn patterns and make decisions based on experience.
- Supervised learning: Predict labels from labeled training data.
- Unsupervised learning: Discover structure in unlabeled data.
- Reinforcement learning: Optimize actions based on rewards.
Machine Learning Workflow
- Define the problem and success metrics.
- Collect and clean data using Pandas and NumPy.
- Engineer features to represent the problem effectively.
- Select and train models using scikit-learn or other libraries.
- Evaluate with train/test splits, cross-validation, and metrics.
- Deploy and monitor in production environments.
Environment Setup
Install essential ML libraries within a virtual environment registered for your team at info.studygrid@gmail.com.
pip install numpy pandas scikit-learn matplotlib seabornUse Jupyter notebooks or IDEs like VS Code to experiment with data interactively.
Data Preparation Checklist
- Fix missing values with imputation or removal.
- Normalize or standardize numerical features.
- Encode categorical variables (one-hot, target encoding).
- Split data into training, validation, and test sets.
- Document assumptions and data sources for reproducibility.
Common Tools
- Pandas for data wrangling.
- NumPy for numerical operations.
- scikit-learn for modeling, pipelines, and evaluation.
- matplotlib / seaborn for visual diagnostics.
Next Steps
Continue to the mean/median/mode lesson to understand descriptive statistics used in exploratory data analysis.
Supervised vs Unsupervised
| Type | Data | Goal | Examples |
|---|---|---|---|
| Supervised | Labelled | Predict a label/value | Regression, classification |
| Unsupervised | Unlabelled | Find structure | Clustering, dimensionality reduction |
The typical workflow: collect data → clean & scale → split train/test → train → evaluate → tune → deploy. The following lessons build up each of these steps.
Try It Yourself
Exercise: Classify each task as supervised or unsupervised: (a) predict house price, (b) group customers by behaviour, (c) detect spam email.
Show solution
(a) supervised regression, (b) unsupervised clustering, (c) supervised classification.
Key Takeaways
- ML learns patterns from data instead of explicit rules.
- Supervised learning uses labels; unsupervised finds hidden structure.
- Always evaluate on data the model has not seen.
📘 Real-World Deep Dive
scikit-learn packages the canonical supervised, unsupervised, and ensemble-learning algorithms into one consistent API. <code>fit / predict / transform / score</code> — that loop is most of what you'll do day-to-day.
Real-Life Scenario
A realistic mini-ML pipeline: load Iris, split into train/test, fit a small pipeline that scales + fits a logistic regression, and report accuracy on held-out data.
Real-Life Example
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
X, y = load_iris(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, random_state=0, stratify=y)
pipe = make_pipeline(
StandardScaler(),
LogisticRegression(max_iter=200),
)
pipe.fit(Xtr, ytr)
yhat = pipe.predict(Xte)
print("accuracy :", round(accuracy_score(yte, yhat), 3))
print(classification_report(yte, yhat, target_names=["setosa", "versicolor", "virginica"]))Expected Output
accuracy : 0.947
precision recall f1-score support
setosa 1.00 1.00 1.00 12
versicolor 0.92 0.92 0.92 12
virginica 0.92 0.92 0.92 13
accuracy 0.95 37Common mistakes
predict_probaisn't supported by every classifier — checksklearn.utils.all_estimatorsfor capabilities.- Calling
fit_transformon the test set leaks information — use.transforminstead. cross_val_scoredefaults to accuracy for classification but squared error for regression; pickscoring=deliberately.
🚀 Performance & Best Practices
- Always wrap preprocessing + estimator in a
Pipeline— never train a model on raw features in production. - For > 100 k rows, use
HistGradientBoostingRegressor/Classifier— orders of magnitude faster. - Enable
n_jobs=-1for embarrassingly-parallel trainers likeRandomForest.
🧪 Try It Yourself
- Replace the pipeline with
RandomForestClassifierand compare accuracy. - Add a
GridSearchCVover C and a new feature transformation (polynomial degree). - Persist the best pipeline with
joblib.dumpand reload it for inference.