Best Machine Learning Libraries

Machine learning libraries in Python for ML, deep learning, GenAI, and data science
Published On
Updated On
Table of Content
up_arrow
Summarize with AI:

The best machine learning libraries in Python depend on the type of data you work with and the task you need to solve. For traditional tabular data, Scikit-learn, XGBoost, and LightGBM are the standard choices for classification, regression, and predictive modelling, while PyTorch and TensorFlow lead deep learning work such as computer vision and large language models. Hugging Face Transformers and LangChain extend this further into modern generative AI and LLM-powered applications.

These tools sit on top of foundational data libraries like NumPy and Pandas, which handle data preparation and numerical computing before any model is trained. Together, they provide ready-to-use algorithms and frameworks to build, train, evaluate, and deploy models without coding every component from scratch. Choosing the right one comes down to your data type, the machine learning task, and how much control you need over the model.

NumPy: The Foundation Everything Else Is Built On

NumPy cube representing numerical computing and array operations on a soft blue abstract background

NumPy is a Python library for numerical computing that provides efficient multidimensional arrays and mathematical operations. Its core ndarray structure makes it easier to store, manipulate, and perform calculations on numerical data across different dimensions.

NumPy is widely used in machine learning for data preparation, feature manipulation, matrix operations, and numerical calculations. Features such as vectorised operations and broadcasting allow calculations to be performed efficiently across large arrays, while universal functions (ufuncs) provide optimised operations for common mathematical tasks.

Key Concepts in NumPy

ndarray

NumPy's ndarray is the structure used by NumPy for keeping and handling numbers. It can hold data in one or more dimensions, which makes it good for holding vectors, matrices and larger sets of numbers that are used in machine learning.

Unlike Python lists, ndarray is built for numeric work and lets you do calculations directly on all its entries. It also supports actions like indexing, slicing, reshaping, and gathering totals, which are often needed when getting data ready for machine learning models.

Broadcasting

Broadcasting allows NumPy to perform operations between arrays that have different but compatible shapes. Instead of requiring the arrays to have exactly the same dimensions, NumPy automatically adjusts how the operation is applied based on their shapes.

This is particularly useful when the same value or a smaller array needs to be applied across a larger dataset. For example, a value can be added to every element in an array without manually creating a new array of repeated values, simplifying numerical calculations and data transformations.

Vectorisation

Vectorization allows mathematical operations to be applied to an entire array at once rather than processing each value individually through Python loops. This results in shorter and more efficient code when working with numerical datasets.

In machine learning workflows, vectorization is useful for operations such as transforming features, performing mathematical calculations, and preparing numerical data. By operating directly on arrays, NumPy can handle these calculations more efficiently than repeatedly processing individual elements.

Universal Functions (ufuncs)

Universal functions, or ufuncs, are NumPy functions designed to perform element-wise operations on arrays. They include mathematical operations such as addition, multiplication, square roots, exponential calculations, and other commonly used numerical functions.

Ufuncs can operate on individual values as well as entire arrays while following NumPy's broadcasting rules. This makes it easier to apply the same mathematical operation across large datasets without writing separate loops or functions for each element.

Key Features of NumPy

  1. Multidimensional arrays: NumPy provides the ndarray, an efficient structure for storing and manipulating numerical data across one or more dimensions. It supports operations such as indexing, slicing, reshaping, and mathematical calculations on arrays.
  2. Vectorized operations: NumPy allows calculations to be performed on entire arrays without writing separate loops for individual values. This makes numerical operations more concise and efficient when working with large datasets.
  3. Mathematical functions: NumPy includes a wide range of built-in functions for numerical and scientific calculations, including statistical operations, square roots, exponential functions, and other mathematical transformations.
  4. Linear algebra: NumPy provides tools for working with vectors and matrices, including matrix multiplication, dot products, and other linear algebra operations. These operations are commonly used in machine learning, data analysis, and scientific computing.

NumPy in Machine Learning

NumPy is commonly used for numerical calculations, data preprocessing, simulations, signal processing, and image manipulation. It can help clean, transform, reshape, and calculate values in datasets before the data is passed to a machine learning model. Its arrays also work closely with many Python libraries used for machine learning, data analysis, and scientific computing.

Machine Learning Example

import numpy as np

features = np.array([10, 20, 30, 40])

scaled_features = features / np.max(features)

print(scaled_features)


Here, NumPy creates an ndarray and uses vectorized operations to divide every value by the maximum value in the array. This demonstrates how NumPy can quickly transform numerical features during machine learning data preprocessing.

NumPy provides the numerical foundation needed for many machine learning workflows, from preparing and transforming datasets to performing mathematical and matrix-based operations. Its efficient arrays, broadcasting, vectorization, and mathematical functions make it useful for handling numerical computations before data reaches a machine learning model. Since many tools in the Python data science ecosystem work closely with NumPy arrays, understanding NumPy also provides a strong foundation for working with other machine learning and data analysis libraries.

Scikit-learn: A Core Library for Machine Learning in Python

Scikit-learn machine learning illustration with interconnected data points and model nodes

Scikit-learn is a Python machine learning library that provides tools for building, training, evaluating, and applying machine learning models. It supports common machine learning tasks such as classification, regression, clustering, dimensionality reduction, and model selection.

Scikit-learn provides a consistent interface for working with different machine learning algorithms. Its tools can be used to prepare datasets, train models, evaluate their performance, and make predictions without having to implement each algorithm from scratch.

Key Concepts in Scikit-learn

Estimators

Estimators are Scikit-learn objects that implement machine learning algorithms or data transformation methods. They provide methods such as fit() learning patterns or parameters from data and, depending on the estimator, methods such as predict() working with new data.

For example, a classification algorithm can be used fit() to learn from training data and predict() to classify new observations. This common interface makes it easier to work with different algorithms using a similar workflow.

Transformers

Transformers are Scikit-learn tools used to modify or prepare data before it is passed to a machine learning model. They can perform tasks such as feature scaling, encoding categorical values, imputing missing values, and transforming numerical features.

Transformers are commonly used fit() to learn the required transformation from the training data and transform() to apply it. This helps keep data preparation separate from model training and makes preprocessing steps easier to manage.

Model Evaluation

Scikit-learn provides tools for measuring how well a machine learning model performs on data. Depending on the type of problem, different evaluation metrics can be used, such as accuracy, precision, recall, F1-score, mean squared error, and R² score.

These evaluation tools help compare model predictions with the actual values and determine whether a model is performing appropriately for a particular machine learning task.

Model Selection

Scikit-learn includes tools for selecting and improving machine learning models. Techniques such as train-test splitting, cross-validation, and hyperparameter tuning can be used to evaluate models and identify suitable configurations.

For example, it train_test_split() can divide a dataset into training and testing sets, while tools such as it GridSearchCV can test different parameter combinations to find a suitable model configuration.

Key Features of Scikit-learn

  1. Wide range of algorithms: Provides ready-to-use algorithms for classification, regression, clustering, dimensionality reduction, and other common machine learning tasks without requiring models to be implemented from scratch.
  2. Data preprocessing: Includes tools for scaling features, encoding categorical data, handling missing values, and transforming datasets before they are used for model training.
  3. Model evaluation and selection: Provides techniques such as cross-validation, train-test splitting, and hyperparameter tuning to evaluate models and identify suitable configurations for a given task.
  4. Consistent API: Uses a common interface with methods such as fit(), predict(), and transform(), making it easier to work with different models and preprocessing tools using a similar workflow.

Scikit-learn in Machine Learning

Scikit-learn is commonly used throughout the machine learning workflow, from preparing data to training and evaluating models. It provides algorithms for classification, regression, clustering, dimensionality reduction, and other common machine learning tasks.

It can also be used alongside libraries such as NumPy and pandas. NumPy provides numerical arrays and calculations, while pandas is commonly used for handling tabular data. Scikit-learn can then use this prepared data for preprocessing, model training, evaluation, and prediction.

Scikit-learn Code Example

The following example demonstrates how Scikit-learn can be used to train a simple classification model and make predictions:

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

X = [[20], [25], [30], [35], [40], [45]]
y = [0, 0, 0, 1, 1, 1]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

model = DecisionTreeClassifier()
model.fit(X_train, y_train)

predictions = model.predict(X_test)

print(predictions)


Here, X contains the input features, while y contains the corresponding class labels. Scikit-learn's train_test_split() divides the data into training and testing sets. The DecisionTreeClassifier is then trained using the fit() method and used to classify new data with predict().

Scikit-learn provides the tools needed to build and evaluate machine learning models in Python. Its consistent workflow makes it easier to prepare data, train models, make predictions, and evaluate results across different machine learning tasks. When used with libraries such as NumPy and pandas, it forms an important part of the Python machine learning ecosystem.

TensorFlow: A Framework for Building Machine Learning Models

TensorFlow logo with neural network nodes representing deep learning and machine learning

TensorFlow is an open-source machine learning framework used to build, train, and deploy machine learning and deep learning models. It provides tools for working with numerical data, neural networks, model training, and large-scale machine learning workflows.

TensorFlow uses tensors as its primary data structure for representing numerical data. It also provides APIs such as Keras for building neural networks and supports tasks such as image classification, natural language processing, time-series analysis, and other deep learning applications.

Key Concepts in TensorFlow

Tensors

Tensors are multidimensional arrays used by TensorFlow to represent and process numerical data. They can have different numbers of dimensions depending on the type of data being handled.

For example, a single value can be represented as a scalar, a list of values as a vector, and image or batch data as higher-dimensional tensors. Tensor operations form the basis of calculations performed by TensorFlow models.

Neural Networks

TensorFlow provides tools for creating neural networks made up of layers of interconnected nodes. Each layer processes the input data and passes the results to the next layer to learn patterns from the training data.

Neural networks can be used for tasks such as image classification, text processing, prediction, and other machine learning problems.

Model Training

Model training is the process of teaching a machine learning model to identify patterns in training data. TensorFlow provides methods for defining a model, selecting a loss function and optimiser, and training the model over multiple iterations.

The fit() method is commonly used to train a model on available training data.

Keras API

Keras is a high-level API integrated with TensorFlow that simplifies the process of building and training neural networks. It provides ready-to-use layers, model structures, optimisers, and training methods.

Using Keras, developers can create neural network models with fewer lines of code while still using TensorFlow's underlying machine learning capabilities.\

Key Features of TensorFlow

  1. Tensor-based computing: Uses tensors to represent and process numerical data across different dimensions. This allows TensorFlow to perform the mathematical operations required for training and running machine learning models.
  2. Neural network development: Provides layers, activation functions, optimisers, and other components for building neural networks. These can be used to develop models for tasks such as classification, image processing, and natural language processing.
  3. Model training and evaluation: Provides tools for configuring, training, and evaluating models using functions such as compile(), fit(), and evaluate(). This supports different loss functions, optimisers, and performance metrics.
  4. Keras integration: Includes Keras as a high-level API for building and training neural networks with less code. It provides ready-to-use layers and model structures while using TensorFlow for the underlying computations.

TensorFlow in Machine Learning

TensorFlow is commonly used for developing deep learning models that work with large and complex datasets. It supports applications such as image and speech recognition, natural language processing, recommendation systems, and time-series prediction.

It can work with data prepared using libraries such as NumPy and pandas, while Keras provides a simpler interface for creating and training neural networks within the TensorFlow ecosystem.

TensorFlow Code Example

The following example demonstrates how TensorFlow can be used to build and train a simple neural network for classification:

import tensorflow as tf

X = [[20], [25], [30], [35], [40], [45]]
y = [0, 0, 0, 1, 1, 1]

model = tf.keras.Sequential([
    tf.keras.layers.Dense(8, activation="relu"),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

model.fit(X, y, epochs=20, verbose=0)

predictions = model.predict([[32], [42]])

print(predictions)


Here, the Sequential model contains two neural network layers. The first layer uses the ReLU activation function to process the input, while the second uses sigmoid to produce a classification output. The compile() method configures the model, and fit() trains it using the provided data. The trained model can then use predict() to generate outputs for new input values.

TensorFlow provides tools for building, training, and deploying machine learning and deep learning models. Its tensor-based operations, neural network layers, and Keras API make it useful for developing models for tasks such as classification, image processing, and natural language processing.

PyTorch: A Flexible Framework for Deep Learning

PyTorch logo with neural network connections representing deep learning model development

PyTorch is an open-source machine learning framework used to build, train, and deploy machine learning and deep learning models. It provides tools for working with tensors, neural networks, automatic differentiation, and model optimisation.

PyTorch is widely used for developing deep learning models for tasks such as image recognition, natural language processing, computer vision, and other complex machine learning applications. Its flexible approach to model development makes it useful for both research and practical machine learning projects.

Key Concepts in PyTorch

Tensors

Tensors are PyTorch's primary data structure for storing and processing numerical data. Similar to NumPy arrays, tensors can represent data in multiple dimensions and can be used for mathematical operations required during model training.

PyTorch tensors can also be processed using GPUs, which can speed up computations for large datasets and deep learning models.

Neural Networks

PyTorch provides the torch.nn module for creating neural networks using layers, activation functions, and other components. Developers can define how data moves through different layers of a model and customise the architecture according to the machine learning task.

These neural networks can be used for tasks such as classification, prediction, image processing, and natural language processing.

Automatic Differentiation

PyTorch includes automatic differentiation through its autograd system. It automatically calculates gradients needed to update a model's parameters during training.

This reduces the need to manually calculate derivatives and allows neural networks to be trained using optimisation algorithms such as gradient descent.

Model Training

PyTorch provides tools for training models by combining data, neural network architectures, loss functions, and optimisers. The model processes training data, calculates its error, and updates its parameters based on the calculated gradients.

This process is repeated across multiple training iterations until the model learns useful patterns from the data.

Key Features of PyTorch

  1. Tensor computation: Provides multidimensional tensors for storing and processing numerical data. These tensors can run on CPUs or GPUs, making them suitable for computationally intensive deep learning tasks.
  2. Neural network tools: Includes torch.nn and other components for building neural networks with layers, activation functions, loss functions, and custom architectures for different machine learning tasks.
  3. Automatic differentiation: Uses the autograd system to automatically calculate gradients during model training. This simplifies the process of updating model parameters through optimisation algorithms.
  4. Model optimisation: Provides optimisers such as Adam and SGD to update model parameters based on calculated gradients. These tools can be combined with custom training loops for greater control over the learning process.

PyTorch in Machine Learning

PyTorch is commonly used for developing and training deep learning models, particularly for applications involving images, text, audio, and other complex data. It provides tools for creating custom neural network architectures and controlling different parts of the training process.

PyTorch can also work with libraries such as NumPy and pandas for data preparation, while its tensor and neural network tools handle model training and numerical computations.

PyTorch Code Example

The following example demonstrates how PyTorch can be used to create and train a simple neural network for classification:

import torch
import torch.nn as nn

X = torch.tensor([[20.0], [25.0], [30.0], [35.0], [40.0], [45.0]])
y = torch.tensor([[0.0], [0.0], [0.0], [1.0], [1.0], [1.0]])

model = nn.Sequential(
    nn.Linear(1, 8),
    nn.ReLU(),
    nn.Linear(8, 1),
    nn.Sigmoid()
)

loss_function = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

for _ in range(100):
    predictions = model(X)
    loss = loss_function(predictions, y)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

new_data = torch.tensor([[32.0], [42.0]])
predictions = model(new_data)

print(predictions)


Here, PyTorch first converts the input data into tensors. The Sequential model contains two linear layers with a ReLU activation function between them. BCELoss() measures the difference between the model's predictions and the actual labels, while the Adam optimiser updates the model's parameters during training.

The backward() method calculates the gradients required for learning, and step() updates the model based on those gradients. After training, the model uses new input data to generate predictions.

PyTorch provides flexible tools for building and training deep learning models. Its tensors, neural network modules, automatic differentiation, and optimisation tools make it useful for developing models for tasks such as classification, computer vision, and natural language processing.

XGBoost: A Powerful Gradient Boosting Library

XGBoost logo with decision tree structures representing gradient boosting and machine learning

XGBoost is an open-source machine learning library designed for gradient boosting and is commonly used for classification, regression, and ranking tasks. It builds models by combining multiple decision trees, with each new tree helping improve the errors made by the previous trees.

XGBoost is widely used for structured or tabular data because it can handle complex relationships between features and target values. It also provides features such as regularisation, missing-value handling, and support for different model objectives, making it useful for a range of machine learning applications.

Key Concepts in XGBoost

Gradient Boosting

Gradient boosting is a machine learning technique that builds an ensemble of decision trees sequentially. Each new tree focuses on reducing the errors made by the existing model, gradually improving the overall predictions.

XGBoost uses this approach to build models that can capture complex patterns in data. The process continues across multiple trees until the model reaches the required level of performance or a specified number of boosting rounds.

Decision Trees

Decision trees are the basic building blocks used by XGBoost. Each tree makes a series of decisions based on input features and produces a prediction at the end of the path.

Instead of relying on a single tree, XGBoost combines predictions from many trees. This ensemble approach allows the model to learn more complex relationships within the dataset.

Regularisation

Regularisation helps control model complexity and can reduce overfitting. XGBoost provides regularisation techniques that can limit how complex individual trees become and how strongly they contribute to the final model.

This is useful when a model performs very well on training data but needs to maintain its performance when applied to unseen data.

Learning Rate and Boosting Rounds

The learning rate controls how much each new tree contributes to the overall model. A smaller learning rate generally requires more boosting rounds, while a larger learning rate can require fewer rounds.

These settings can be adjusted to control how quickly the model learns from the training data and how complex the final model becomes.

Key Features of XGBoost

  1. Gradient-boosted trees: Builds an ensemble of decision trees sequentially, with each new tree learning from the errors of previous trees to improve the model's predictions.
  2. Regularisation: Includes L1 and L2 regularisation techniques that help control model complexity and reduce the risk of overfitting on training data.
  3. Missing-value handling: Can automatically handle missing values during tree construction, reducing the need for manual handling of every missing value before training.
  4. Model tuning: Provides parameters such as learning rate, tree depth, and number of boosting rounds that can be adjusted to control model complexity and improve prediction performance

Key Features of XGBoost

Gradient-boosted trees: Builds an ensemble of decision trees sequentially, with each new tree learning from the errors of previous trees to improve the model's predictions.

Regularisation: Includes L1 and L2 regularisation techniques that help control model complexity and reduce the risk of overfitting on training data.

Missing-value handling: Can automatically handle missing values during tree construction, reducing the need for manual handling of every missing value before training.

Model tuning: Provides parameters such as learning rate, tree depth, and number of boosting rounds that can be adjusted to control model complexity and improve prediction performance

XGBoost in Machine Learning

XGBoost is widely used for classification, regression, ranking, and predictive modelling. It can work with structured datasets containing numerical and categorical information after appropriate preprocessing.

It is commonly applied to problems such as customer classification, sales prediction, risk assessment, fraud detection, and other tasks involving tabular data. Its combination of boosting, regularisation, and efficient tree-based learning makes it suitable for many supervised machine learning workflows.

XGBoost Code Example

The following example demonstrates how XGBoost can be used to train a classification model and make predictions:

from xgboost import XGBClassifier

X = [[20], [25], [30], [35], [40], [45]]
y = [0, 0, 0, 1, 1, 1]

model = XGBClassifier(
    n_estimators=50,
    max_depth=3,
    learning_rate=0.1,
    random_state=42
)

model.fit(X, y)

predictions = model.predict([[32], [42]])

print(predictions)


Here, X contains the input features and y contains the corresponding class labels. XGBClassifier creates the classification model, while fit() trains it using the available data. The predict() method then uses the trained model to classify new input values.

This example demonstrates the basic XGBoost workflow of preparing data, configuring a model, training it, and generating predictions.

XGBoost provides a powerful approach to machine learning using gradient-boosted decision trees. Its support for classification, regression, regularisation, and model tuning makes it useful for working with structured datasets and complex prediction tasks.

LightGBM: An Efficient Gradient Boosting Framework

LightGBM logo with gradient boosting decision trees representing efficient machine learning

LightGBM is an open-source machine learning framework developed by Microsoft for gradient boosting. It is commonly used for classification, regression, and ranking tasks, particularly when working with large structured or tabular datasets.

LightGBM builds decision tree-based models using gradient boosting and is designed to train models efficiently while using relatively low memory. It uses techniques such as histogram-based learning and leaf-wise tree growth to improve training efficiency and model performance.

Key Concepts in LightGBM

Gradient Boosting

Gradient boosting builds a model by combining multiple decision trees sequentially. Each new tree focuses on reducing the errors made by the existing model, allowing the overall model to improve over successive boosting rounds.

LightGBM applies gradient boosting to learn complex relationships between input features and target values.

Leaf-Wise Tree Growth

LightGBM uses leaf-wise tree growth, where the algorithm selects the leaf that provides the greatest reduction in the model's loss and splits it further.

This approach can help the model reduce errors efficiently, although parameters such as maximum tree depth may be used to control model complexity and reduce overfitting.

Histogram-Based Learning

LightGBM groups continuous feature values into discrete bins instead of evaluating every possible split value. This reduces the amount of computation required when building decision trees.

Histogram-based learning helps LightGBM train efficiently, particularly when working with large datasets containing many features.

Categorical Features

LightGBM can work directly with categorical features when they are provided in the appropriate format. This can reduce the need for extensive preprocessing, such as manually converting every categorical feature into separate numerical columns.

This feature is particularly useful when working with structured datasets containing categories such as product types, regions, or customer segments.

Key Features of LightGBM

  1. Fast training: LightGBM uses histogram-based learning and leaf-wise tree growth to find useful splits efficiently. This allows it to train gradient boosting models faster, especially when working with large tabular datasets and many features.
  2. Efficient memory usage: LightGBM groups continuous feature values into discrete bins instead of processing every possible value individually. This reduces memory usage and helps speed up the training process on larger datasets.
  3. Categorical feature support: LightGBM can work with categorical features directly, reducing the need for extensive manual encoding before training. This can simplify preprocessing when datasets contain many categorical variables.
  4. Parallel and GPU training: LightGBM supports parallel, distributed, and GPU-based training for larger machine learning workloads. These options allow training tasks to use multiple CPU cores or GPU resources when working with large datasets.

LightGBM in Machine Learning

LightGBM is commonly used for classification, regression, and ranking problems involving structured or tabular data. It can be applied to tasks such as predicting categories, estimating numerical values, and ranking search or recommendation results.

It can also work alongside libraries such as NumPy, pandas, and Scikit-learn. These libraries can be used to prepare and manage data, while LightGBM handles model training and prediction.

LightGBM Code Example

The following example demonstrates how LightGBM can be used to train a simple classification model and make predictions:

from lightgbm import LGBMClassifier

X = [[20], [25], [30], [35], [40], [45]]
y = [0, 0, 0, 1, 1, 1]

model = LGBMClassifier(
    n_estimators=50,
    learning_rate=0.1,
    max_depth=3,
    random_state=42,
    verbosity=-1
)

model.fit(X, y)

predictions = model.predict([[32], [42]])

print(predictions)


Here, X contains the input features and y contains the corresponding class labels. LGBMClassifier creates the classification model, while fit() trains the model using the available data. The predict() method then uses the trained model to classify new input values.

This example demonstrates the basic LightGBM workflow of preparing data, configuring a model, training it, and generating predictions.

LightGBM provides an efficient approach to gradient boosting for machine learning tasks involving structured data. Its leaf-wise tree growth, histogram-based learning, and support for categorical features make it useful for classification, regression, and ranking problems, particularly with large datasets.

Keras: A High-Level API for Deep Learning

Keras logo with neural network layers representing deep learning model development

Keras is a high-level deep learning API used to build, train, and evaluate neural network models. It provides a simple interface for creating deep learning models while handling many of the lower-level details involved in model development.

Keras is commonly used for tasks such as image classification, natural language processing, time-series prediction, and other neural network applications. It can run on top of machine learning backends such as TensorFlow and provides ready-to-use layers, optimisers, loss functions, and training tools.

Key Concepts in Keras

Layers

Layers are the basic building blocks of a Keras neural network. Each layer processes input data and passes the resulting output to the next layer.

Keras provides different types of layers, including dense layers, convolutional layers, recurrent layers, and dropout layers, allowing models to be designed according to the requirements of different tasks.

Models

A model defines how different layers are connected to process input data and produce predictions. Keras provides model-building approaches such as Sequential for creating a straightforward stack of layers.

More complex architectures can be created using the Functional API, which allows multiple inputs, outputs, and connections between layers.

Activation Functions

Activation functions determine how the output of a neural network layer is transformed. Common activation functions in Keras include ReLU, sigmoid, and softmax.

They help neural networks learn complex relationships in data and are selected based on the requirements of the model and task.

Model Training

Keras provides methods for configuring and training neural network models. The compile() method is used to specify the optimiser, loss function, and evaluation metrics, while fit() trains the model using the provided data.

After training, evaluate() can be used to measure model performance and predict() can generate predictions for new data.

Key Features of Keras

  1. Simple model development: Keras provides a high-level API for building neural networks with a clear and consistent interface. Developers can create, configure, and train models without writing large amounts of low-level code.
  2. Ready-to-use layers: Includes commonly used layers, activation functions, loss functions, and optimisers for developing different types of neural networks. These components can be combined to create models for tasks such as classification and image processing.
  3. Model training tools: Provides built-in methods such as compile(), fit(), and evaluate() for configuring, training, and evaluating neural network models. This makes common training workflows easier to implement.
  4. Multiple backend support: Keras can work with backends such as TensorFlow, JAX, and PyTorch, allowing developers to build models using the Keras API while choosing a suitable computational backend.

Keras in Machine Learning

Keras is commonly used to develop deep learning models for tasks such as image classification, text processing, speech recognition, and prediction. Its high-level interface makes it easier to create neural networks without manually implementing every part of the training process.

Keras can work with NumPy and pandas for data preparation and can use TensorFlow as its backend for numerical operations and model training.

Keras Code Example

The following example demonstrates how Keras can be used to build and train a simple neural network for classification:

import tensorflow as tf

X = [[20], [25], [30], [35], [40], [45]]
y = [0, 0, 0, 1, 1, 1]

model = tf.keras.Sequential([
    tf.keras.layers.Dense(8, activation="relu"),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

model.fit(X, y, epochs=20, verbose=0)

predictions = model.predict([[32], [42]])

print(predictions)


Here, Sequential creates a simple neural network by stacking two layers. The first Dense layer uses ReLU to process the input, while the second uses sigmoid to produce the classification output. The compile() method configures the training process, and fit() trains the model using the provided data.

After training, predict() is used to generate outputs for new input values. This demonstrates the basic Keras workflow of building a model, configuring it, training it, and generating predictions.

Keras provides a simple and flexible way to build, train, and evaluate neural networks. Its layers, models, activation functions, and built-in training methods make it useful for developing deep learning applications across tasks such as classification, image processing, and natural language processing.

NLTK: A Toolkit for Working with Human Language

NLTK illustration representing natural language processing and text analysis

NLTK (Natural Language Toolkit) is a Python library designed for processing and analysing human language data. It provides tools for common natural language processing (NLP) tasks such as tokenisation, stemming, tagging, parsing, and text classification.

NLTK is commonly used to prepare and analyse text before it is passed to machine learning or NLP models. It also includes text datasets, lexical resources, and utilities that can help developers experiment with and understand different NLP techniques.

Key Concepts in NLTK

Tokenisation

Tokenisation breaks text into smaller units such as words or sentences. NLTK provides tools for separating a piece of text into individual tokens that can then be analysed or processed.

For example, a sentence can be divided into individual words, making it easier to perform tasks such as text analysis, frequency counting, or further preprocessing.

Stemming

Stemming reduces words to a simpler root-like form by removing common word endings. For example, words such as “playing,” “played,” and “plays” may be reduced to a similar stem.

This can help reduce variations of the same word during text processing, although the resulting stem may not always be a complete dictionary word.

Part-of-Speech Tagging

Part-of-speech (POS) tagging identifies the grammatical role of words in a sentence, such as noun, verb, adjective, or adverb.

NLTK can assign these tags to tokens, providing information that can be useful for analysing the structure and meaning of text.

Text Classification

NLTK provides tools and resources for building basic text classification systems. Text classification involves assigning text to predefined categories based on its content.

For example, text can be classified into categories such as positive or negative sentiment, different topics, or other predefined groups.

Key Features of NLTK

  1. Text preprocessing: NLTK provides tools for common text preprocessing tasks such as tokenisation, stemming, lemmatisation, and stop-word removal. These steps help convert raw text into a form that can be processed for NLP tasks.
  2. Linguistic analysis: Includes resources and tools for analysing the structure and meaning of text, such as part-of-speech tagging, named entity recognition, and sentence parsing.
  3. Language datasets: Provides access to linguistic datasets and corpora that can be used for training, testing, and experimenting with natural language processing techniques.
  4. Classical NLP algorithms: Supports traditional NLP approaches for tasks such as text classification, sentiment analysis, and language processing, making it useful for learning and working with rule-based or statistical NLP methods.

NLTK in Machine Learning

NLTK is commonly used for text preprocessing and linguistic analysis in NLP workflows. It can help clean and transform text through tasks such as tokenisation, stemming, stop-word removal, and part-of-speech tagging before the data is used for machine learning.

It can also work alongside libraries such as NumPy and Scikit-learn. NLTK can handle language-focused preprocessing, while Scikit-learn can be used to convert text into numerical features, train machine learning models, and evaluate their performance.

NLTK Code Example

The following example demonstrates how NLTK can be used to tokenize a sentence and identify its individual words:

import nltk
from nltk.tokenize import word_tokenize

nltk.download("punkt")

text = "Machine learning helps computers learn from data."

tokens = word_tokenize(text)

print(tokens)


Here, word_tokenize() divides the sentence into individual words and punctuation marks. The resulting tokens can then be used for further NLP tasks such as text cleaning, feature extraction, or classification.

This demonstrates a basic NLTK workflow of taking raw text, breaking it into tokens, and preparing it for further language processing.

NLTK provides a collection of tools for processing and analysing human language in Python. Its tokenisation, stemming, POS tagging, and text-processing capabilities make it useful for preparing and analysing text in NLP workflows. It can also be combined with machine learning libraries such as Scikit-learn to build text-based applications.

Hugging Face Transformers: A Library for Pretrained AI Models

Hugging Face Transformers illustration representing pretrained transformer models and natural language processing

Hugging Face Transformers is a Python library that provides access to pretrained models for natural language processing, computer vision, audio, and multimodal machine learning tasks. It includes models that can be used for tasks such as text classification, text generation, translation, summarisation, question answering, image classification, and speech processing.

The library allows developers to use pretrained models and adapt them to specific tasks instead of building every model from scratch. It also provides tools for loading models, processing input data, fine-tuning models, and generating predictions.

Key Concepts in Hugging Face Transformers

Pretrained Models

Pretrained models are machine learning models that have already been trained on large datasets. Hugging Face provides access to many pretrained models that can be used for different tasks and types of data.

Developers can use these models directly for inference or further train them on task-specific datasets.

Tokenisation

Tokenisation converts text into smaller units that a model can process. A tokenizer can divide text into tokens and convert those tokens into numerical representations that can be passed to a transformer model.

Different models may use different tokenisation methods, so the appropriate tokenizer is generally loaded along with the model.

Transformers

Transformers are neural network architectures designed to process sequences of data and identify relationships between different parts of the input. They use mechanisms such as attention to determine which parts of the input are important when generating or understanding outputs.

Transformer-based models are widely used for NLP tasks such as language understanding, translation, summarisation, and text generation.

Pipelines

Hugging Face provides pipeline() as a simple interface for performing common machine learning tasks. It can automatically handle steps such as preprocessing input, running the model, and processing the output.

For example, a sentiment-analysis pipeline can take a sentence as input and return the predicted sentiment and confidence score.

Key Features of Hugging Face Transformers

  1. Pre-trained models: Provides access to a wide range of pre-trained transformer models for tasks such as text generation, classification, translation, summarization, and other AI applications. These models can be used without building an architecture from scratch.
  2. Unified API: Offers a consistent interface for loading, training, and using different transformer models. This makes it easier to experiment with models and switch between architectures for different tasks.
  3. Fine-tuning support: Provides tools for adapting pre-trained models to specific datasets and tasks. Fine-tuning can help a model perform more effectively on a particular domain or application.
  4. Multiple framework support: Works with major machine learning frameworks and supports PyTorch, TensorFlow, and JAX backends. This allows developers to use transformer models within different machine learning workflows.

Hugging Face Transformers in Machine Learning

Hugging Face Transformers is commonly used for NLP and other AI tasks that require pretrained models. It can be used for sentiment analysis, text classification, summarisation, question answering, translation, text generation, and more.

The library can also work alongside frameworks such as PyTorch and TensorFlow. Developers can use pretrained models for inference or fine-tune them using their own datasets for more specialised applications.

Hugging Face Transformers Code Example

The following example demonstrates how Hugging Face Transformers can be used to perform sentiment analysis using a pretrained model:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")

text = "The machine learning model performed very well."

result = classifier(text)

print(result)


Here, pipeline() creates a sentiment-analysis pipeline using a pretrained model. The text is passed to the classifier, which processes it and returns a predicted sentiment along with a confidence score.

This demonstrates the basic Hugging Face workflow of loading a pretrained model, providing input text, and generating a prediction.

Hugging Face Transformers provides access to pretrained transformer models and tools for building AI applications. Its support for tokenisation, pretrained models, pipelines, and model fine-tuning makes it useful for tasks such as text classification, translation, summarisation, and text generation.

LangChain: A Framework for Building LLM Applications

LangChain illustration representing LLM workflows, retrieval, tools, and application orchestration

LangChain is a framework for developing applications that use large language models (LLMs). It provides components for connecting language models with external data, tools, APIs, databases, and application workflows.

LangChain can be used to build applications such as chatbots, question-answering systems, document-based assistants, and AI workflows. It provides reusable components for handling prompts, retrieving information, managing conversation context, and connecting different parts of an LLM application.

Key Concepts in LangChain

LLMs and Chat Models

LangChain provides interfaces for working with language models and chat models from different providers. These models can process user input and generate natural language responses.

Using a common interface makes it easier to connect language models to other components of an application and change models when required.

Prompt Templates

Prompt templates provide a structured way to create prompts for language models. Instead of writing a complete prompt every time, developers can define a template with variables that can be filled with different inputs.

This makes prompts easier to reuse and manage across different parts of an application.

Chains

Chains connect multiple steps together to create a specific workflow. For example, a chain can take a user's question, retrieve relevant information, pass that information to a language model, and generate a response.

This allows individual components to work together as part of a larger LLM application.

Retrieval

Retrieval allows an application to find relevant information from external sources before sending it to a language model. LangChain provides components for working with document loaders, text splitters, embeddings, and vector stores.

Key Features of LangChain

  1. LLM integrations: LangChain provides integrations with different large language models and AI services through common interfaces. This makes it easier to connect models with applications without building each integration from scratch.
  2. Retrieval and data integration: Provides components for connecting LLM applications with documents, vector databases, and other data sources. This is useful for building applications that retrieve relevant information before generating a response.
  3. Agents and tools: Supports AI agents that can decide which tools or actions to use based on the task. Applications can connect models with external APIs, databases, search tools, and other functions.
  4. Prompt and workflow management: Provides tools for creating reusable prompts and combining multiple model or tool calls into workflows. This helps organise multi-step applications where several operations need to work together.

LangChain in Machine Learning and AI

LangChain is commonly used to develop applications around LLMs rather than to train traditional machine learning models from scratch. It can connect language models with external information, tools, APIs, and databases to create more capable AI applications.

It can also work with model providers and libraries such as OpenAI, Hugging Face, and vector databases, allowing developers to combine different components within a single application workflow.

LangChain Code Example

The following example demonstrates how LangChain can be used to create a simple prompt and send it to a language model:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template(
    "Explain {topic} in simple terms."
)

messages = prompt.invoke({"topic": "machine learning"})

print(messages)


Here, ChatPromptTemplate creates a reusable prompt containing a variable called topic. The invoke() method fills the variable with the provided value and creates the resulting message that can be passed to a chat model.

This demonstrates a basic LangChain workflow of creating a reusable prompt, providing input, and preparing it for a language model.

LangChain provides components for building applications around large language models. Its support for prompt templates, chains, retrieval, and integrations makes it useful for developing chatbots, question-answering systems, document assistants, and other LLM-based applications.

Pandas: A Library for Data Analysis and Preparation

Pandas logo with structured data tables representing data analysis and preprocessing

Pandas is a Python library used for working with structured and tabular data. It provides data structures such as DataFrame and Series that make it easier to load, organise, clean, transform, and analyse datasets.

Pandas is widely used in machine learning workflows for preparing data before it is passed to a model. It can handle tasks such as filtering rows, selecting columns, managing missing values, combining datasets, and performing basic data analysis.

Key Concepts in Pandas

DataFrame

A DataFrame is Pandas' main data structure for working with tabular data. It organises data into rows and columns, similar to a spreadsheet or database table.

DataFrames can be used to select specific columns, filter records, modify values, and perform calculations on datasets.

Series

A Series is a one-dimensional Pandas data structure that stores a sequence of values. Each Series can have an associated index, making it useful for representing individual columns or labelled data.

Series can also be used for filtering, calculations, and other data manipulation tasks.

Data Cleaning

Pandas provides functions for identifying and handling common data-quality issues. These include missing values, duplicate records, incorrect data types, and unwanted values.

Cleaning data before model training helps ensure that the dataset is in a suitable format for further analysis or machine learning.

Data Manipulation

Pandas provides tools for filtering, sorting, grouping, merging, and reshaping data. These operations make it easier to transform raw datasets into a format that can be analysed or used for machine learning.

For example, rows can be filtered based on conditions, multiple datasets can be combined using merge(), and data can be summarised using groupby().

Key Features of Pandas

  1. DataFrame structure: Pandas provides the DataFrame structure for storing and working with tabular data in rows and columns. It makes datasets easier to inspect, organise, filter, and manipulate during data analysis.
  2. Data cleaning: Includes tools for handling missing values, removing duplicates, changing data types, and cleaning inconsistent data. These operations help prepare datasets before they are used for machine learning.
  3. Data manipulation: Supports operations such as filtering, sorting, grouping, merging, and reshaping datasets. This makes it easier to transform raw data and prepare relevant features for analysis or model training.
  4. File and data handling: Can read and write data in formats such as CSV, Excel, JSON, and SQL. This allows datasets to be imported from different sources and prepared within the same Python workflow.

Pandas in Machine Learning

Pandas is commonly used during the data preparation stage of a machine learning workflow. It can load datasets from sources such as CSV files, Excel files, and databases, and then provide tools for cleaning and transforming the data.

Once the data has been prepared, it can be converted into formats that work with libraries such as Scikit-learn, XGBoost, LightGBM, and other machine learning frameworks.

Pandas Code Example

The following example demonstrates how Pandas can be used to create a dataset, filter its values, and calculate an average:

import pandas as pd

data = {
    "Age": [20, 25, 30, 35],
    "Score": [75, 82, 90, 68]
}

df = pd.DataFrame(data)

filtered_data = df[df["Score"] >= 80]

average_score = filtered_data["Score"].mean()

print(filtered_data)
print(average_score)


Here, DataFrame creates a table from the provided data. The filtering condition selects rows where the Score is 80 or higher, while mean() calculates the average score of the filtered values.

This demonstrates a basic Pandas workflow of creating, filtering, and analysing structured data.

Pandas provides practical tools for organising, cleaning, transforming, and analysing structured data in Python. Its DataFrame and Series structures make it useful for preparing datasets before they are passed to machine learning libraries such as Scikit-learn, XGBoost, and LightGBM.

Polars: A Fast DataFrame Library for Data Processing]

Polars logo with structured DataFrame elements representing fast data processing and analysis

Polars is a DataFrame library designed for fast data processing and analysis. Written in Rust with a Python API, it supports operations such as filtering, sorting, grouping, joining, and transforming structured datasets.

Polars is commonly used when working with large datasets that require efficient data manipulation. It uses a columnar data format and supports parallel processing and lazy execution, which can help optimise data-processing workflows.

Key Concepts in Polars

DataFrame

A DataFrame is Polars' primary structure for storing and working with tabular data. It organises data into columns and rows and provides operations for filtering, selecting, transforming, and analysing datasets.

Polars DataFrames can handle numerical, categorical, string, and other data types commonly found in structured datasets.

Lazy Execution

Lazy execution allows Polars to build a query plan before actually processing the data. Instead of immediately executing every operation, Polars can analyse the complete workflow and optimise how the operations should be performed.

This can reduce unnecessary processing and improve efficiency when working with larger datasets.

Expressions

Expressions are used to describe data transformations in Polars. They allow operations such as selecting columns, filtering rows, creating new columns, and performing calculations to be written in a structured way.

Expressions can also be optimised as part of Polars' query execution process.

Parallel Processing

Polars is designed to take advantage of multiple CPU cores when performing data-processing operations. This allows independent operations to run in parallel, which can improve processing speed for suitable workloads.

Key Features of Polars

  1. Fast data processing: Polars uses a columnar data format and multi-threaded execution to process large datasets efficiently. It can perform many data operations across multiple CPU cores instead of relying on a single execution thread.
  2. Lazy evaluation: Polars can delay execution until all query operations have been defined. This allows its query engine to optimise the complete workflow before processing the data.
  3. Memory-efficient processing: Its columnar approach helps reduce memory usage when working with large datasets. This makes Polars useful for data preparation and analysis when datasets become difficult to handle efficiently with traditional approaches.
  4. Expression-based API: Polars uses expressions to define filtering, grouping, aggregation, and transformation operations. These expressions can be optimised by the query engine to improve how the overall data operation is executed.

Polars in Machine Learning

Polars can be used during the data preparation stage of a machine learning workflow. It can load, clean, filter, transform, join, and analyse structured datasets before they are passed to a machine learning model.

It can also be used alongside libraries such as NumPy, Scikit-learn, XGBoost, and LightGBM. Once data preparation is complete, the processed data can be converted into formats supported by the selected machine learning library.

Polars Code Example

The following example demonstrates how Polars can be used to create a dataset, filter its values, and calculate an average:

import polars as pl

data = pl.DataFrame({
    "Age": [20, 25, 30, 35],
    "Score": [75, 82, 90, 68]
})

filtered_data = data.filter(
    pl.col("Score") >= 80
)

average_score = filtered_data["Score"].mean()

print(filtered_data)
print(average_score)


Here, DataFrame creates a table from the provided data. The filter() operation selects rows where the Score is 80 or higher, while mean() calculates the average of the filtered scores.

This demonstrates a basic Polars workflow of creating, filtering, and analysing structured data.

Polars provides efficient tools for processing and analysing structured datasets in Python. Its DataFrames, expressions, lazy execution, and parallel processing capabilities make it useful for data preparation, particularly when working with larger datasets before machine learning model training.

JAX: A Framework for High-Performance Numerical Computing

JAX logo with numerical arrays and computational acceleration representing high-performance machine learning research

JAX is a Python library for high-performance numerical computing and machine learning research. It provides NumPy-style operations while adding capabilities such as automatic differentiation, just-in-time compilation, and parallel computation.

JAX is commonly used for developing and training machine learning and deep learning models, particularly when efficient numerical computation is important. It can run computations on CPUs, GPUs, and TPUs, making it useful for large-scale numerical and scientific workloads.

Key Concepts in JAX

JAX Arrays

JAX arrays are used to store and manipulate numerical data, similar to NumPy arrays. They support mathematical and array operations while allowing JAX to optimise these computations for different hardware platforms.

JAX arrays form the foundation for many numerical calculations and machine learning operations performed using the library.

Automatic Differentiation

JAX provides automatic differentiation through functions such as grad(). It can calculate the derivatives of numerical functions automatically, which is essential for training machine learning models using gradient-based optimisation.

This allows developers to calculate gradients without manually deriving and implementing the required mathematical formulas.

Just-in-Time Compilation

JAX provides just-in-time (JIT) compilation through jit(). It can compile numerical functions so that repeated computations can be executed more efficiently.

JIT compilation is particularly useful when the same numerical operations need to be performed many times during model training or other computational workloads.

Vectorisation

JAX provides vmap() for automatically applying a function across batches of data. This makes it easier to perform the same computation on multiple inputs without manually writing loops.

Vectorisation can be useful when processing batches of data during machine learning workflows.

Key Features of JAX

  1. NumPy-compatible API: JAX provides an API that is similar to NumPy, making it easier for developers familiar with NumPy to work with JAX. It supports common array operations while adding transformations designed for machine learning and numerical computing.
  2. Automatic differentiation: JAX can automatically calculate derivatives and gradients of mathematical functions. This is useful for training machine learning models, optimising numerical functions, and developing algorithms that require gradient-based optimisation.
  3. Just-in-time compilation: JAX uses just-in-time (JIT) compilation to optimise numerical functions before execution. This can improve performance by compiling operations into efficient code for the hardware being used.
  4. GPU and TPU support: JAX can run computations on CPUs, GPUs, and TPUs with the same programming interface. This makes it useful for computationally intensive machine learning workloads that benefit from hardware acceleration.

JAX in Machine Learning

JAX is commonly used for numerical computing, scientific research, and developing machine learning and deep learning models. Its automatic differentiation, compilation, and hardware acceleration capabilities make it useful for computationally intensive workloads.

JAX can also work with neural network and optimisation libraries such as Flax and Optax, allowing developers to build and train more complex machine learning models.

JAX Code Example

The following example demonstrates how JAX can be used to calculate the gradient of a mathematical function:

import jax
import jax.numpy as jnp

def function(x):
    return x ** 2 + 3 * x

gradient = jax.grad(function)

x = 5.0

print(gradient(x))


Here, jax.numpy provides NumPy-style numerical operations, while jax.grad() automatically calculates the derivative of the function. For the given input, JAX calculates the gradient without requiring the derivative to be written manually.

This demonstrates a basic JAX workflow of defining a numerical function, applying automatic differentiation, and calculating its gradient.

JAX provides NumPy-style numerical computing with features such as automatic differentiation, JIT compilation, and vectorisation. These capabilities make it useful for high-performance numerical workloads and machine learning research, particularly when models require efficient gradient calculations and hardware acceleration.

Matplotlib and Seaborn: Data Visualization Libraries

Matplotlib and Seaborn visualization illustration with charts representing data analysis and statistical visualization

Matplotlib and Seaborn are Python libraries used to create charts and visualise data. Matplotlib provides a flexible foundation for creating different types of plots, while Seaborn builds on Matplotlib and offers a simpler interface for creating statistical visualisations.

Both libraries are commonly used during data analysis and machine learning workflows to understand datasets, identify patterns, compare values, and communicate results through visualisations.

Key Concepts in Matplotlib and Seaborn

Matplotlib

Matplotlib is a general-purpose visualization library that provides tools for creating line charts, bar charts, scatter plots, histograms, and other types of visualisations.

It gives developers detailed control over elements such as axes, labels, titles, markers, and figure layouts, making it useful when customised visualisations are required.

Seaborn

Seaborn is a statistical data visualization library built on top of Matplotlib. It provides higher-level functions for creating visualisations from structured datasets and includes built-in themes and statistical plotting features.

Seaborn is useful for exploring relationships, distributions, correlations, and patterns within datasets with relatively little code.

Statistical Visualisation

Both libraries can be used to visualise statistical information in datasets. Common examples include distributions, correlations, trends, and comparisons between different groups.

These visualisations can make patterns and relationships easier to identify during exploratory data analysis.

Customisation

Matplotlib provides extensive control over the appearance and structure of charts. Seaborn also provides styling options while simplifying many common statistical plots.

Developers can customise titles, labels, axes, legends, and other elements to make visualisations easier to interpret.

Key Features of Matplotlib and Seaborn

  1. Data visualisation: Matplotlib provides tools for creating a wide range of charts, including line graphs, bar charts, scatter plots, histograms, and other visualisations. Seaborn builds on Matplotlib to make statistical and data-focused visualisations easier to create.
  2. Statistical visualisations: Seaborn includes built-in functions for visualising distributions, relationships, correlations, and categorical data. It is useful for identifying patterns and trends in datasets before applying machine learning models.
  3. Customisation: Matplotlib provides detailed control over plot elements such as labels, axes, legends, markers, and figure layouts. Seaborn also offers built-in themes and styling options that help create clear and consistent statistical charts.
  4. Python data science integration: Both libraries work well with common Python data tools such as NumPy and Pandas. This allows developers to visualise datasets directly during data exploration, preprocessing, and machine learning workflows.

Matplotlib and Seaborn in Machine Learning

Matplotlib and Seaborn are commonly used during exploratory data analysis and model evaluation. They can help visualise feature distributions, relationships between variables, correlations, and model results before and after training.

They can also work alongside libraries such as Pandas, NumPy, and Scikit-learn. Pandas and NumPy can prepare numerical data, while Matplotlib and Seaborn can be used to visualise that data and understand patterns before building or evaluating a machine learning model.

Matplotlib and Seaborn Code Example

The following example demonstrates how Matplotlib and Seaborn can be used to visualise a simple dataset:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.DataFrame({
    "Age": [20, 25, 30, 35, 40],
    "Score": [65, 72, 80, 85, 90]
})

sns.scatterplot(data=data, x="Age", y="Score")

plt.title("Age vs Score")
plt.xlabel("Age")
plt.ylabel("Score")
plt.show()


Here, Pandas creates a DataFrame containing the dataset, while Seaborn's scatterplot() creates a visual relationship between age and score. Matplotlib is then used to add the chart title and axis labels.

This demonstrates how Pandas, Seaborn, and Matplotlib can work together to explore and visualise structured data.

Matplotlib and Seaborn provide useful tools for visualising and understanding data in Python. Their charts and statistical plots can help identify patterns, relationships, and trends during data analysis and machine learning workflows.

OpenCV: A Computer Vision Library for Python

OpenCV logo with image-processing elements representing computer vision and image analysis

OpenCV (Open Source Computer Vision Library) is an open-source library used for image and video processing, computer vision, and visual data analysis. It provides functions for reading, modifying, analysing, and processing images and video.

In machine learning workflows, OpenCV is often used to prepare visual data before it is passed to a machine learning or deep learning model. It can resize images, change colour formats, detect edges, and perform other preprocessing tasks required for computer vision applications.

Key Concepts in OpenCV

Image Processing

OpenCV provides functions for basic image operations such as resizing, cropping, rotating, and changing image formats. These operations help prepare images for analysis or model training.

Image Filtering

Image filtering techniques can be used to reduce noise, smooth images, or highlight specific visual features. OpenCV provides filters such as Gaussian blur and median filtering for these tasks.

Feature Detection

OpenCV includes techniques for detecting edges, corners, and other important features in images. These features can help identify shapes, objects, or patterns in visual data.

Video Processing

OpenCV can read and process video frames from files or cameras. Each frame can be analysed individually for tasks such as object detection, motion tracking, and other computer vision applications.

Key Features of OpenCV

  1. Image processing: OpenCV provides a wide range of functions for processing and manipulating images. It supports operations such as resizing, cropping, filtering, colour conversion, thresholding, and edge detection for different computer vision tasks.
  2. Computer vision algorithms: Includes algorithms for tasks such as feature detection, object detection, image segmentation, and geometric transformations. These capabilities can be used to extract useful information from images and video data.
  3. Video and camera processing: OpenCV can capture and process frames from cameras, video files, and other sources. Its efficient processing capabilities make it useful for applications that need to analyse video streams or perform computer vision operations on individual frames.
  4. Python and C++ support: OpenCV provides bindings for programming languages such as Python and C++, making it accessible for both rapid development and performance-focused applications. It can also be combined with machine learning and deep learning libraries to build more advanced computer vision systems.

OpenCV in Machine Learning

OpenCV is commonly used during the data preparation and preprocessing stages of computer vision workflows. Images can be resized, converted to grayscale, normalised, or transformed before being provided to machine learning models.

It can also be combined with libraries such as TensorFlow, PyTorch, and Scikit-learn to create computer vision applications for image classification, object detection, facial recognition, and image analysis.

OpenCV Code Example

The following example demonstrates how OpenCV can be used to read an image, convert it to grayscale, and resize it.

import cv2

image = cv2.imread("image.jpg")

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

resized_image = cv2.resize(gray_image, (300, 300))

cv2.imwrite("processed_image.jpg", resized_image)


This code reads an image using cv2.imread(), converts it from colour to grayscale using cv2.cvtColor(), and resizes it to 300 × 300 pixels using cv2.resize(). The processed image is then saved using cv2.imwrite().

The basic workflow is to load the image, apply the required preprocessing operations, and save or pass the processed data to the next stage of the computer vision pipeline.

OpenCV provides a wide range of tools for processing and analysing images and video. Its image processing, filtering, feature detection, and video processing capabilities make it useful for preparing visual data and developing computer vision applications alongside machine learning and deep learning frameworks.

Which Libraries Should You Actually Use? A Practical Decision Guide

You do not need to use all 14 Python machine learning libraries in every project. The right combination depends on the type of data you are working with, the machine learning task, and whether the project involves traditional machine learning, deep learning, computer vision, NLP, or generative AI.

The libraries can also be used together. For example, a project may use Pandas for data preparation, Scikit-learn for model training, and XGBoost for gradient boosting. Choosing libraries based on the project's requirements keeps the workflow focused and avoids adding tools that are not needed.

Classical Machine Learning on Tabular Data

For projects that work mainly with structured or tabular data, a practical starting combination is:

NumPy + Pandas or Polars + Scikit-learn

NumPy provides numerical operations and array handling, while Pandas or Polars can be used to load, clean, transform, and analyse structured datasets. Scikit-learn then provides tools for preprocessing, model training, prediction, and evaluation.

When the project requires gradient-boosted decision trees, XGBoost or LightGBM can be added. Both are useful for classification, regression, and other tasks involving structured data.

Deep Learning and Computer Vision

For deep learning projects, PyTorch or TensorFlow/Keras can be used to build and train neural networks. The choice between them depends on the project's requirements, existing team knowledge, and preferred development workflow.

For image processing and computer vision tasks, OpenCV can be used alongside these frameworks. It handles operations such as image resizing, filtering, colour conversion, and video processing before the visual data is passed to a machine learning or deep learning model.

JAX can also be considered for research-oriented experimentation and numerical workloads where automatic differentiation, compilation, vectorisation, and accelerator performance are important.

Generative AI and LLM Applications

For applications based on large language models, Hugging Face Transformers is useful for accessing pretrained models, tokenising text, running inference, and fine-tuning supported models.

LangChain can be added when an application needs more than direct model interaction. It provides components for workflows involving retrieval, external tools, prompts, multi-step processing, and agent-based applications.

These libraries can also be used together. For example, Hugging Face Transformers can provide the underlying model while LangChain handles parts of the application workflow around it.

Natural Language Processing

For classical NLP tasks and learning the fundamentals of text processing, NLTK remains useful. It provides tools for tokenisation, stemming, part-of-speech tagging, and other traditional NLP operations.

For many modern NLP applications, transformer-based models have become an important approach. Libraries such as Hugging Face Transformers provide access to pretrained transformer models that can be used for tasks such as text classification, summarisation, translation, and text generation.

A Simple Way to Choose


Project Type

Libraries to Consider

Main Purpose

Tabular machine learning

NumPy + Pandas/Polars + Scikit-learn

Data preparation, modelling, and evaluation

Gradient boosting

XGBoost or LightGBM

Tree-based prediction on structured data

Deep learning

PyTorch or TensorFlow/Keras

Building and training neural networks

Computer vision

OpenCV + PyTorch/TensorFlow/Keras

Image and video processing with deep learning

ML research and numerical experimentation

JAX

Automatic differentiation, compilation, and accelerated computing

Generative AI / LLMs

Hugging Face Transformers

Pretrained models and fine-tuning

LLM applications

LangChain + an LLM framework/model

Retrieval, tools, orchestration, and multi-step workflows

Classical NLP

NLTK

Traditional text processing and NLP learning


Why This Decision Guide Matters

The earlier sections explain what each library does, but knowing the purpose of a library does not always tell you which one belongs in a particular project. This decision guide connects the individual libraries to common machine learning use cases.

Instead of trying to learn or use every library at once, readers can start with the tools that match their project and add other libraries when a specific requirement calls for them.

Pick the Right ML Library for Your Project
From model building to data processing and deep learning, compare Python libraries based on what your project needs.

Conclusion

Python offers a wide range of libraries that support different stages of the machine learning workflow, from numerical computing and data preparation to model training, deep learning, computer vision, NLP, and generative AI. Libraries such as NumPy, Pandas, Polars, and Scikit-learn provide a foundation for working with data and traditional machine learning, while XGBoost and LightGBM support gradient boosting. PyTorch, TensorFlow, Keras, and JAX are useful for deep learning and numerical experimentation, while OpenCV supports image and video processing.

For NLP and generative AI applications, NLTK, Hugging Face Transformers, and LangChain serve different purposes, ranging from traditional text processing to pretrained models and LLM-based application workflows. The key is not to use every library in a single project, but to choose the tools that match the data, task, and development requirements. Understanding what each library does makes it easier to build a practical and efficient machine learning workflow in Python.

Frequently Asked Questions

Which machine learning library is best for beginners?
expand
Can multiple Python machine learning libraries be used together?
expand
What is the difference between a machine learning library and a framework?
expand
What is the difference between TensorFlow and PyTorch?
expand
Which Python libraries are used for Generative AI and LLM applications?
expand
How do you install machine learning libraries in Python?
expand
Which Python libraries are commonly used for data preprocessing?
expand
Which Python libraries are used for computer vision?
expand


Schedule a call now
Start your offshore web & mobile app team with a free consultation from our solutions engineer.

We respect your privacy, and be assured that your data will not be shared

Best Machine Learning Libraries in 2026 (Python & GenAI) | Code B