Hands-on Machine Learning with Python

Author: Ashwin Pajankar, Aditya Joshi
File Type: pdf
Size: 6.9 MB
Language: English
Pages: 335

Hands-on Machine Learning with Python: Implement Neural Network Solutions with Scikit-learn and PyTorch

Introduction

Machine learning becomes much easier to understand when theory is connected directly to working code. Neural networks are a perfect example. Instead of treating artificial intelligence as a collection of complicated equations, engineers can build practical models, train them on data, evaluate their performance, and improve them step by step using Python. 🧠🐍

Two particularly useful tools for this journey are Scikit-learn and PyTorch. Scikit-learn provides a straightforward environment for experimenting with neural networks and comparing them with conventional machine-learning algorithms. PyTorch goes further by providing flexible tensor operations, automatic differentiation, customizable neural-network architectures, and powerful training workflows.

Hands-on Machine Learning with Python

ImageImage

Image

Image

For beginners, Scikit-learn offers an excellent starting point because its MLPClassifier and MLPRegressor APIs hide much of the mathematical complexity. For advanced engineering students and professionals, PyTorch exposes the underlying mechanisms, making it suitable for sophisticated architectures and research-oriented development.

This article presents a practical journey from neural-network fundamentals to implementation, comparison, troubleshooting, and real-world deployment.

Image

ImageImage

ImageImage

Image


Background Theory

How Neural Networks Work

A neural network is a computational model inspired loosely by biological neurons. Its fundamental building block is an artificial neuron that receives inputs, multiplies them by weights, adds a bias, and passes the result through an activation function.

A simplified neuron can be expressed as:

z = w₁x₁ + w₂x₂ + … + wₙxₙ + b

The activation function then produces:

a = f(z)

where:

  • x = input features
  • w = learned weights
  • b = bias
  • f() = activation function
  • a = neuron output

Multiple neurons form a layer, while several layers create a neural network.

Forward Propagation

During forward propagation, information moves from the input layer through hidden layers toward the output layer.

A typical structure might look like:

Input → Hidden Layer 1 → Hidden Layer 2 → Output

Each layer transforms the information.

Loss Functions

A neural network needs a mathematical measure of how incorrect its predictions are. This is the loss function.

For regression, mean squared error is commonly written as:

MSE = (1/n) Σ(yᵢ − ŷᵢ)²

For classification, cross-entropy is frequently used.

The training objective is to minimize the loss:

min L(θ)

where θ represents the learnable parameters.

Backpropagation

Backpropagation calculates how much each parameter contributed to the prediction error.

The chain rule of calculus allows the network to calculate gradients:

∂L/∂w

These gradients are then used by an optimizer to update the weights.

A simplified gradient-descent update is:

wₙₑw = wₒₗd − η(∂L/∂w)

where η is the learning rate. ⚙️


Definition

What Is Hands-On Neural Network Machine Learning?

Hands-on machine learning means moving beyond theoretical descriptions and actually building, training, evaluating, and improving machine-learning systems.

In Python, a practical neural-network workflow generally contains:

  1. Data collection
  2. Data cleaning
  3. Feature preparation
  4. Train/test splitting
  5. Feature scaling
  6. Model construction
  7. Training
  8. Validation
  9. Performance evaluation
  10. Hyperparameter optimization
  11. Deployment

Scikit-learn vs PyTorch

Scikit-learn is primarily designed for practical machine learning and provides a high-level interface.

PyTorch is a deep-learning framework that provides substantially greater control over tensors, gradients, architectures, optimization, and training.

The two tools therefore complement rather than simply replace each other.


Step-by-Step Neural Network Implementation

Step 1: Install the Required Libraries

A typical environment can be prepared with:

pip install numpy pandas scikit-learn matplotlib torch

A professional project should ideally use a virtual environment to prevent dependency conflicts.

Step 2: Prepare a Dataset

For an initial classification experiment, the breast-cancer dataset available through Scikit-learn is convenient.

🐍 from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

data = load_breast_cancer()

X = data.data
y = data.target

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

The stratify parameter helps maintain a similar class distribution between the training and testing datasets.

Step 3: Scale the Features

Neural networks often perform better when numerical inputs have comparable scales.

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

An important engineering rule is to fit the scaler only on training data. Otherwise, information from the test set can leak into the training process.

ImageImage

Image

ImageImage

Image

Step 4: Build a Neural Network with Scikit-learn

Scikit-learn provides the MLPClassifier class for multilayer perceptron classification.

from sklearn.neural_network import MLPClassifier

model = MLPClassifier(
    hidden_layer_sizes=(64, 32),
    activation="relu",
    solver="adam",
    learning_rate_init=0.001,
    max_iter=500,
    random_state=42
)

Here:

  • 64 = neurons in the first hidden layer
  • 32 = neurons in the second hidden layer
  • relu = activation function
  • adam = optimizer
  • 0.001 = initial learning rate

Step 5: Train the Model

model.fit(X_train, y_train)

During training, the model repeatedly processes the data and adjusts its parameters to reduce prediction error.

Step 6: Evaluate the Model

from sklearn.metrics import accuracy_score, classification_report

predictions = model.predict(X_test)

print("Accuracy:", accuracy_score(y_test, predictions))
print(classification_report(y_test, predictions))

Accuracy is useful, but it should not be the only metric.

For imbalanced datasets, engineers should also examine precision, recall, F1-score, and confusion matrices.

Step 7: Implement the Same Concept with PyTorch

PyTorch requires more explicit construction.

🐍 import torch
import torch.nn as nn
import torch.optim as optim

The data must be converted into tensors:

X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train, dtype=torch.long)

X_test_tensor = torch.tensor(X_test, dtype=torch.float32)
y_test_tensor = torch.tensor(y_test, dtype=torch.long)

Now create the network:

class NeuralNetwork(nn.Module):

    def __init__(self, input_size):
        super().__init__()

        self.network = nn.Sequential(
            nn.Linear(input_size, 64),
            nn.ReLU(),
            nn.Linear(64, 32),
            nn.ReLU(),
            nn.Linear(32, 2)
        )

    def forward(self, x):
        return self.network(x)

The number of output neurons corresponds to the two classes in this example.

Step 8: Configure the Optimizer and Loss

model = NeuralNetwork(X_train.shape[1])

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(
    model.parameters(),
    lr=0.001
)

The optimizer determines how parameters are updated.

Step 9: Train the PyTorch Network

for epoch in range(100):

    optimizer.zero_grad()

    outputs = model(X_train_tensor)

    loss = criterion(outputs, y_train_tensor)

    loss.backward()

    optimizer.step()

    if (epoch + 1) % 10 == 0:
        print(
            f"Epoch {epoch + 1}, "
            f"Loss: {loss.item():.4f}"
        )

The training loop contains four fundamental operations:

Forward pass → Loss calculation → Backpropagation → Parameter update

ImageImage

Image

ImageImage

Image

Image


Comparison

Scikit-learn and PyTorch

FeatureScikit-learnPyTorch
Learning curveEasierSteeper
Neural-network APIHigh-levelFlexible
Custom architecturesLimitedExcellent
Automatic differentiationAbstractedCore feature
GPU supportLimited for MLP workflowsExcellent
ResearchModerateExcellent
Traditional MLExcellentNot its primary purpose
Rapid prototypingExcellentVery good
Deployment flexibilityVery goodExcellent
Beginner friendliness⭐⭐⭐⭐⭐⭐⭐⭐

Which Should Engineers Choose?

For a student learning fundamental machine learning concepts, Scikit-learn is usually the smoother starting point.

For deep-learning projects involving convolutional networks, transformers, custom loss functions, large datasets, GPU acceleration, or research experimentation, PyTorch is usually more appropriate.

A practical learning sequence is:

Scikit-learn → PyTorch fundamentals → Advanced PyTorch → Production deep learning


Diagrams and Tables

Neural Network Architecture

A simple feed-forward network can be represented conceptually as:

Input Features
     │
     ▼
┌───────────────┐
│ Hidden Layer  │
│ 64 Neurons    │
└───────────────┘
     │
     ▼
┌───────────────┐
│ Hidden Layer  │
│ 32 Neurons    │
└───────────────┘
     │
     ▼
┌───────────────┐
│ Output Layer  │
│ 2 Classes     │
└───────────────┘

Typical Machine-Learning Pipeline

Raw Data
   ↓
Cleaning
   ↓
Feature Engineering
   ↓
Train / Validation / Test
   ↓
Scaling
   ↓
Neural Network
   ↓
Training
   ↓
Evaluation
   ↓
Optimization
   ↓
Deployment

Important Hyperparameters

ParameterPurposeTypical Starting Point
Learning rateControls update size0.001
Batch sizeSamples processed together32–128
EpochsTraining iterations50–200
Hidden layersNetwork depth1–4
NeuronsLayer capacity16–256
DropoutRegularization0.1–0.5
Weight decayPenalizes large weights1e-5–1e-3

The correct values depend heavily on the dataset and task.


Examples

Example: Binary Classification

Suppose an engineering company wants to classify whether a machine is likely to experience a particular operating condition.

Inputs could include:

  • Temperature
  • Pressure
  • Vibration
  • Rotational speed
  • Current
  • Load

The network receives:

X = [temperature, pressure, vibration, speed, current, load]

and produces a probability or class prediction.

Example: Regression

Neural networks can also predict continuous values.

For example:

Inputs → Operating conditions

Output → Predicted energy consumption

Instead of using a classification loss, a regression network may use mean squared error.

criterion = nn.MSELoss()

The final layer would generally contain a single output neuron.


Real-World Applications

Predictive Maintenance

Industrial systems generate enormous quantities of sensor information. Neural networks can analyze historical measurements and identify patterns associated with future equipment failures.

⚙️ Sensors → Neural Network → Failure Risk

This can help engineering teams move from reactive maintenance toward predictive maintenance.

Computer Vision

PyTorch is widely useful for computer-vision applications such as:

  • Defect detection
  • Object recognition
  • Medical image analysis
  • Automated inspection
  • Robotics perception

Convolutional neural networks can identify spatial patterns that conventional fully connected networks may struggle to capture.

Energy Engineering

Neural networks can model nonlinear relationships between weather, demand, equipment conditions, and energy consumption.

This can support:

  • Load forecasting
  • Solar-power prediction
  • Building-energy optimization
  • Smart-grid applications

Robotics

Robotic systems can use neural networks for perception, classification, trajectory-related prediction, and control-oriented tasks.


Common Mistakes

Training Without Scaling

A neural network can struggle when one feature ranges from 0–1 while another ranges from 0–1,000,000.

Solution: Apply appropriate normalization or standardization.

Data Leakage

Using test-set information while preprocessing training data can produce unrealistically strong results.

Solution: Fit preprocessing transformations exclusively on training data.

Overfitting

A network can memorize training examples instead of learning general patterns.

Signs include:

Training accuracy ↑

while:

Validation accuracy ↓

Solutions include:

  • More training data
  • Regularization
  • Dropout
  • Smaller networks
  • Early stopping
  • Data augmentation where appropriate

Using Accuracy Alone

A model achieving 95% accuracy may still be poor when the minority class represents only 1% of observations.

Solution: Evaluate precision, recall, F1-score, ROC-AUC, PR-AUC, and confusion matrices when appropriate.

Making the Network Too Large

More layers and neurons do not automatically mean better performance.

A giant network can increase:

  • Training time
  • Memory requirements
  • Overfitting
  • Deployment complexity

Start with a simple architecture and increase complexity only when evidence supports it.


Challenges & Solutions

Challenge: Slow Training

Solution: Use mini-batches, optimize preprocessing, reduce unnecessary model complexity, and use GPU acceleration when the workload justifies it.

Challenge: Unstable Training

Possible causes include an unsuitable learning rate, poorly scaled inputs, or an inappropriate architecture.

Try:

Learning rate ↓ + proper scaling + validation monitoring

Challenge: Overfitting

Use regularization techniques such as:

nn.Dropout(0.3)

or weight decay:

optimizer = optim.Adam(
    model.parameters(),
    lr=0.001,
    weight_decay=1e-4
)

Challenge: Choosing Between Frameworks

If the project is primarily classical machine learning, Scikit-learn may be sufficient.

If the project requires custom deep-learning architectures, GPU training, or advanced experimentation, PyTorch provides considerably more control.


Case Study

Predicting Industrial Equipment Failure

Consider a hypothetical manufacturing facility containing hundreds of rotating machines.

Each machine produces measurements every few seconds:

Temperature + vibration + rotational speed + electrical current

Historical maintenance records identify whether a failure occurred.

Engineers first clean the sensor data and remove impossible readings. They then divide the observations into training, validation, and test sets.

A baseline neural network is created with:

Input → 64 → 32 → 2 outputs

The features are standardized before training.

The initial model produces acceptable training performance but weaker validation performance. Engineers identify this as a possible overfitting problem.

They respond by:

  1. Reducing model complexity.
  2. Introducing regularization.
  3. Monitoring validation loss.
  4. Adjusting the learning rate.
  5. Evaluating precision and recall.
  6. Testing the final model on previously unseen equipment data.

The final objective is not simply achieving a high accuracy number. The real engineering goal is creating a model that can provide useful warnings under actual operating conditions.

That distinction is critical:

Good benchmark performance ≠ reliable engineering system.


Essential Tips

For Beginners 🎯

Start with small datasets.

Understand these concepts before moving into complex architectures:

  • Features
  • Labels
  • Training
  • Validation
  • Testing
  • Loss
  • Gradients
  • Epochs
  • Learning rate
  • Overfitting

Do not rush directly into advanced transformers or enormous neural networks.

For Advanced Engineers ⚙️

Think about the entire system rather than only the neural network.

Consider:

Data quality → Model quality → Evaluation → Deployment → Monitoring

A model can be mathematically sophisticated yet operationally useless if the incoming production data differs significantly from the training distribution.

Use Baseline Models

Before building a sophisticated neural network, establish a baseline using simpler algorithms.

For example:

Logistic Regression → Random Forest → MLP → Advanced PyTorch Model

This progression helps determine whether additional complexity actually provides value.

Monitor More Than Loss

Training loss alone does not tell the entire story.

Track:

  • Training loss
  • Validation loss
  • Precision
  • Recall
  • F1-score
  • Inference time
  • Memory consumption
  • Stability

Keep Experiments Reproducible

Record:

  • Dataset version
  • Random seeds
  • Hyperparameters
  • Model architecture
  • Software versions
  • Evaluation metrics

Reproducibility is especially important in professional engineering and research environments.


FAQs

What is the difference between Scikit-learn and PyTorch?

Scikit-learn provides high-level machine-learning tools and a relatively simple neural-network interface. PyTorch is a deep-learning framework designed for flexible neural architectures, automatic differentiation, GPU acceleration, and advanced experimentation.

Is PyTorch harder to learn than Scikit-learn?

Generally, yes. PyTorch exposes more of the underlying training process, including tensors, gradients, optimizers, and training loops. This makes it more complex but also much more flexible.

Do I need mathematics to learn neural networks?

You can begin without advanced mathematics, but understanding linear algebra, probability, calculus, and optimization becomes increasingly valuable as you progress.

Why should neural-network inputs be scaled?

Scaling places numerical features on comparable ranges. This can improve optimization and make neural-network training more stable.

What is an epoch?

An epoch represents one complete pass through the training dataset.

For example, if a model trains for 100 epochs, the training algorithm processes the dataset approximately 100 times, subject to the details of batching and training configuration.

How many hidden layers should a neural network have?

There is no universal number. Start with a simple architecture and increase complexity according to the problem, dataset size, and validation results.

Can PyTorch use a GPU?

Yes. PyTorch supports GPU acceleration, which can dramatically improve training performance for suitable deep-learning workloads.

Should I learn Scikit-learn before PyTorch?

For most beginners, this is a sensible route. Scikit-learn teaches the broader machine-learning workflow while allowing you to concentrate on data preparation, model evaluation, and experimentation before tackling PyTorch’s lower-level flexibility.


Conclusion

Hands-on neural-network engineering is fundamentally about connecting data, mathematics, software, and experimentation. 🧠⚙️🐍

Scikit-learn provides an approachable way to construct multilayer perceptrons and understand the practical machine-learning workflow. PyTorch opens the door to much greater flexibility, allowing engineers to design custom architectures, manipulate tensors, calculate gradients, use advanced optimizers, and exploit modern hardware.

The most effective learning path is not simply to memorize APIs. Instead, understand the complete pipeline:

Data → Preprocessing → Architecture → Forward Pass → Loss → Backpropagation → Optimization → Evaluation → Deployment

For students, this workflow builds a strong foundation for machine learning. For professional engineers, it provides the framework needed to transform neural networks from academic experiments into practical engineering systems.

Ultimately, successful machine learning is not about creating the biggest neural network. It is about creating the right model for the right problem, validating it honestly, understanding its limitations, and integrating it responsibly into the surrounding engineering system. 🚀

Unlock exclusive content
Enjoy all premium content by watching a short ad
Preparing ad...
BY ADX360