Machine learning frameworks: scikit-learn, PyTorch, TensorFlow and Hugging Face
The frameworks used to train and run your own machine learning models: scikit-learn for everyday predictions on tables of data, PyTorch and TensorFlow/Keras for deep learning, and Hugging Face for ready-made pre-trained models. Each with a short example and when to use it.
🎯 By the end of this lesson you will be able to:
- Explain when you need a machine learning framework instead of an LLM API
- Recognise what scikit-learn, PyTorch, TensorFlow/Keras and Hugging Face are each best at
- Read a simple training example and understand each step
In this lesson
When do you need these frameworks?
Large language models through an API can do a lot without any training. But many valuable AI tasks are about predicting something from your own data: will this customer leave, how much will this house sell for, is this transaction fraud? For those, you usually train your own model with a machine learning framework. These models are small, cheap to run, fast, and often more accurate on your specific data than a general LLM.
| Use an LLM API when… | Train your own model when… |
|---|---|
| The task is about language: writing, summarising, answering questions | The task is a prediction from structured data (numbers, categories) |
| You have few or no labelled examples | You have thousands of labelled historical examples |
| Flexibility matters more than cost per call | You need very cheap, very fast predictions at high volume |
| Outputs are text for people to read | Outputs are numbers or labels for other systems |
scikit-learn: everyday machine learning
scikit-learn is the most popular Python library for classic machine learning on tables of data. It includes dozens of proven algorithms with the same simple pattern: create a model, fit it on examples, then predict. Start here for most business prediction tasks.
Predicting which customers might cancel (churn)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
data = pd.read_csv("customers.csv") # one row per customer, numeric columns
X = data.drop(columns=["churned"]) # inputs: months as customer, support calls...
y = data["churned"] # what to predict: 1 = left, 0 = stayed
# Keep 20% of customers aside to test on data the model has never seen
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestClassifier()
model.fit(X_train, y_train) # learn from examples
print(accuracy_score(y_test, model.predict(X_test))) # check on unseen customersTen lines take you from a spreadsheet to a working prediction model. The same pattern works for prices, fraud, demand and many other business questions.
Tip: for large tables, the related libraries XGBoost and LightGBM are often even more accurate and use the same fit/predict style.
PyTorch: flexible deep learning
PyTorch (originally from Meta, now run by the PyTorch Foundation) is the most widely used framework for deep learning: neural networks for images, sound, text and custom models. Most AI research and most modern open models, including large language models, are built with PyTorch.
The training loop: how a neural network learns
import torch
from torch import nn
# A small neural network: 10 inputs → 32 hidden units → 1 output
model = nn.Sequential(nn.Linear(10, 32), nn.ReLU(), nn.Linear(32, 1))
loss_fn = nn.BCEWithLogitsLoss() # measures how wrong we are
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for inputs, labels in train_loader: # batches of training examples
predictions = model(inputs) # 1. guess
loss = loss_fn(predictions, labels) # 2. measure the error
optimizer.zero_grad()
loss.backward() # 3. work out how to adjust each weight
optimizer.step() # 4. adjust the weights a littleGuess, measure the error, adjust, repeat: the same loop from Lesson 2, written in code. PyTorch gives you full control over every step.
TensorFlow and Keras: deep learning for production
TensorFlow (from Google) is another major deep learning framework, strong in production deployment, including mobile and edge devices through LiteRT (previously called TensorFlow Lite). Keras is a simpler, high-level way to build neural networks; Keras 3 can run on TensorFlow, PyTorch or JAX underneath.
The same kind of model in Keras
import keras
model = keras.Sequential([
keras.layers.Dense(32, activation="relu"),
keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(X_train, y_train, epochs=10, validation_split=0.2) # Keras runs the loop for youKeras hides the training loop, which makes it friendly for beginners. PyTorch shows the loop, which gives researchers more control.
Hugging Face: thousands of ready-made models
Hugging Face is a platform and set of libraries for sharing and using pre-trained models. Its Transformers library lets you download and use models for text, images and audio in a few lines, and its Hub hosts hundreds of thousands of models, including open models published by Meta, Google, Mistral, Qwen and others.
Sentiment analysis without training anything
from transformers import pipeline
classifier = pipeline("sentiment-analysis") # downloads a small pre-trained model
print(classifier("The delivery was late and the box was damaged."))
# [{'label': 'NEGATIVE', 'score': 0.99}]A pre-trained model does the job immediately. You can also fine-tune these models on your own examples for better accuracy on your data.
Which one should you use?
| Framework | Best for | Example use | Beginner-friendly? |
|---|---|---|---|
| scikit-learn | Predictions from tables of data | Churn, credit risk, demand forecasting, spam filters | Yes, very |
| XGBoost / LightGBM | Top accuracy on large tables | Fraud detection, pricing, ranking | Yes |
| PyTorch | Custom deep learning and research | Image recognition, custom neural networks, training LLMs | Medium |
| TensorFlow / Keras | Deep learning in production and on devices | Mobile image classification, large-scale training | Keras: yes |
| Hugging Face Transformers | Using and fine-tuning pre-trained models | Sentiment, translation, classification, open LLMs | Yes |
What this means for your role
Developer
Reach for scikit-learn for structured predictions before reaching for an LLM; it is cheaper, faster and easier to test. Learn PyTorch when you need deep learning.
Tester / QA
Test trained models on a separate test set and on slices of data (Lesson 11), and watch for data drift after release.
Business Analyst / PO
Spot prediction tasks in your processes (who will churn, which invoices are risky): these often suit a trained model better than an LLM.
Tech Lead / Architect
Decide where trained models and LLMs each fit in your architecture, and plan how models are retrained and versioned.
Project / Delivery Manager
Trained models need historical labelled data and retraining over time; include data preparation and retraining in plans.
Scrum Master / Agile
Machine learning work is experimental; plan it as time-boxed experiments with clear accuracy targets (Lesson 35).
Try it: run your first model
- Open a free Google Colab notebook (colab.research.google.com).
- Run the Hugging Face sentiment example with three of your own sentences.
- Change one sentence to be sarcastic and see whether the model gets it right. Note what that tells you about model limits.
Key takeaways
- Train your own model for predictions from structured data; use LLM APIs for language tasks.
- scikit-learn is the best start for everyday predictions; XGBoost and LightGBM for top accuracy on tables.
- PyTorch is the most widely used deep learning framework; TensorFlow/Keras is strong for production and devices.
- Hugging Face gives you thousands of pre-trained models to use or fine-tune in a few lines.
Key terms
- Deep learning
- Machine learning with neural networks of many layers, used for images, audio and language.
- Pre-trained model
- A model already trained on large data that you can use directly or fine-tune.
- Fine-tuning
- Training a pre-trained model further on your own examples.
- Test set
- Data kept aside during training to check how the model performs on new examples.
Check your understanding
Answer all 5 questions. Score 3 or more to complete the lesson.
1. You want to predict which customers will cancel, using 50,000 rows of historical account data. What fits best?
2. What is PyTorch mainly used for?
3. Why keep a test set aside?
4. What does a Hugging Face pipeline let you do?
5. What is LiteRT?