Nishaglobal Education logo

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.

Lesson 40 of 4420 min readQuiz: 5 questions

🎯 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
  1. When do you need these frameworks?
  2. scikit-learn: everyday machine learning
  3. PyTorch: flexible deep learning
  4. TensorFlow and Keras: deep learning for production
  5. Hugging Face: thousands of ready-made models
  6. Which one should you use?
  7. What this means for your role
  8. Key takeaways
  9. Quiz

When do you need these frameworks?

What is your data?Tables of numbersscikit-learnXGBoost, LightGBMText, images, audioHugging Facepre-trained modelsCustom neural netsPyTorchTensorFlow / KerasWriting & chatLLM APIno training neededStart with the simplest tool that fits your data and task
Pick the framework by your data: tables suit scikit-learn, ready-made text and image models suit Hugging Face, custom neural networks suit PyTorch or Keras, and writing tasks suit an LLM API.

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 questionsThe task is a prediction from structured data (numbers, categories)
You have few or no labelled examplesYou have thousands of labelled historical examples
Flexibility matters more than cost per callYou need very cheap, very fast predictions at high volume
Outputs are text for people to readOutputs 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 customers

Ten 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 little

Guess, 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 you

Keras 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?

FrameworkBest forExample useBeginner-friendly?
scikit-learnPredictions from tables of dataChurn, credit risk, demand forecasting, spam filtersYes, very
XGBoost / LightGBMTop accuracy on large tablesFraud detection, pricing, rankingYes
PyTorchCustom deep learning and researchImage recognition, custom neural networks, training LLMsMedium
TensorFlow / KerasDeep learning in production and on devicesMobile image classification, large-scale trainingKeras: yes
Hugging Face TransformersUsing and fine-tuning pre-trained modelsSentiment, translation, classification, open LLMsYes

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

  1. Open a free Google Colab notebook (colab.research.google.com).
  2. Run the Hugging Face sentiment example with three of your own sentences.
  3. 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. 1. You want to predict which customers will cancel, using 50,000 rows of historical account data. What fits best?

  2. 2. What is PyTorch mainly used for?

  3. 3. Why keep a test set aside?

  4. 4. What does a Hugging Face pipeline let you do?

  5. 5. What is LiteRT?

Answer all 5 questions to check.

Practise more