Artificial Neural Networks (ANNs)

Machine Learning Fundamentals with Python

4 min read

Published Nov 16 2025


10
0
0
0

ClusteringImagesK-MeansLinear RegressionLogistic RegressionMachine LearningNeural NetworksNLPNumPyPythonRandom Forestsscikit-learnSupervised LearningUnsupervised Learning

Artificial Neural Networks are models inspired by the human brain.
They are made up of layers of simple computing units called neurones, which learn to recognise patterns through training.

Neural networks form the foundation of deep learning, the technology behind computer vision, voice recognition, and language models.




Basic Neurone

A neurone takes several inputs, multiplies each by a weight, adds a bias, and passes the result through a nonlinear function (called an activation function).

machine learning fundamentals neurone equation

Where:

  • x1, x2, ... = inputs
  • w1, w2, ... = weights (learned parameters)
  • b = bias term
  • f = activation function (e.g., sigmoid, ReLU)

machine learning fundamentals neurone diagram

So there can be any number of inputs and the inputs can be any value. Each input is multiplied by its weight, and then all these totals are summed together and a bias is added to it. This then gets fed in to the activation function to generate an output value for that neurone. As networks are trained, the weights on each input and the neurones bias value are adjusted each cycle, these are the learned parameters for the neurone.


The input values, while they can be any value, they are often normalised or scaled to help training. This isn't required, but can make training faster, more stable and less likely to get stuck.






Network Architecture

A neural network typically has:

  1. Input layer – receives the data features.
  2. Hidden layers – transform inputs through neurones.
  3. Output layer – produces final predictions, e.g. class probabilities.

Each neurone in a layer connects to neurones in the next layer.


machine learning fundamentals neural network layers diagram





Activation Functions

Activation functions introduce nonlinearity, allowing networks to learn complex patterns.


If a neural network used only linear operations, then no matter how many layers it had, the whole system would still behave like a single linear classifier. That means it could only draw a straight line to separate classes.


But real-world data is rarely separable by a straight line.
For example, imagine a dataset where one class forms a tight circle inside another class. A straight line can’t separate the inner points from the outer ones.


Activation functions introduce non-linearity, which lets the network bend, twist, and combine many small curves to form complex decision boundaries.
Instead of one straight line, the network can create an irregular closed shape that perfectly wraps around the inner cluster.
This is what gives neural networks their expressive power.



Example

Here you can see two clusters plotted on a scatter chart:

machine learning fundamentals neural network non linear cluster example

If you tried to use a linear approach then there is no line that you can draw that separates the clusters and give you good results:

machine learning fundamentals neural network cluster example linear classifier

However, using neural networks with activation functions, you can separate clusters with a lot more detailed, irregular shaped, line that is made up with many small curves. This allows you to properly identify clusters (the example image is a circle but in reality can be any irregular shape):

machine learning fundamentals neural network cluster example non linear classifier


Some common activation functions

machine learning fundamentals neural network activation functions
Charts displaying the shape of different activation functions


When to use each activation?

Activation

Output Range

Used For

Sigmoid

0 to 1

binary classification output layer

Softmax

0 to 1 (sums to 1)

multi-class classification output layer

Tanh

-1 to 1

hidden layers (rare today)

ReLU

0 to ∞

hidden layers (most common)

Leaky ReLU

-∞ to ∞

avoids dead ReLU

GELU

-∞ to ∞

transformers

Linear

-∞ to ∞

regression output layer






Implementing a Simple Neural Network with Keras

We’ll use Keras, a high-level library built on top of TensorFlow, to train a simple ANN on a classic dataset.

Example Classifying Iris Flower Species:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import seaborn as sns

# Load the dataset
df = sns.load_dataset("iris")

# Encode target labels (species)
encoder = LabelEncoder()
df['species_encoded'] = encoder.fit_transform(df['species'])

# Features and labels
X = df.drop(columns=['species', 'species_encoded'])
y = df['species_encoded']

# Scale features for better training
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

# Build the model
model = Sequential([
    # hidden layer 1
    Dense(8, input_shape=(4,), activation='relu'),
    # hidden layer 2
    Dense(6, activation='relu'),
    # output layer (3 classes)
    Dense(3, activation='softmax')
])

# Compile the model
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# Train the model
history = model.fit(X_train, y_train, epochs=50, batch_size=8, validation_split=0.2, verbose=0)

# Evaluate on test data
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=0)
print(f"Test Accuracy: {test_acc:.2f}")

# Plot training progress
import matplotlib.pyplot as plt

plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title("Model Accuracy During Training")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend()
plt.show()

Output:

Test Accuracy: 0.93

machine learning fundamentals nural network example model accuracy.

Explanation:

  • Each Dense layer connects all neurons from one layer to the next.
  • relu activation allows complex nonlinear learning.
  • softmax converts the last layer’s outputs into probabilities that sum to 1.
  • We train for 50 “epochs” — one full pass over the training data.
  • The history object stores accuracy and loss per epoch for visualisation.





Understanding Model Output

After training, you can use the model to make predictions:

import numpy as np

# example flower to classify
sample = np.array([[5.1, 3.5, 1.4, 0.2]])
sample_scaled = scaler.transform(sample)

prediction = model.predict(sample_scaled)
predicted_class = encoder.inverse_transform([prediction.argmax()])

print("Predicted species:", predicted_class[0])

Output:

Predicted species: setosa

Explanation:

  • The model outputs 3 probability scores (for each species).
  • The class with the highest probability is selected as the prediction.





Preventing Overfitting

Overfitting happens when the model performs very well on training data but poorly on new data.
You can reduce overfitting by:

  • Using validation sets (like validation_split)
  • Applying Dropout layers
  • Collecting more data
  • Using early stopping (stop training when validation loss stops improving)

Example: adding a dropout layer

from tensorflow.keras.layers import Dropout

model = Sequential([
    Dense(8, activation='relu', input_shape=(4,)),
    Dropout(0.3),
    Dense(6, activation='relu'),
    Dense(3, activation='softmax')
])

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact