Featured Article

Your First Neural Network with Python Keras: A Step-by-Step Tutorial

Kenneth Jul 13, 2026

Embarking on your machine learning journey? Building your first neural network is an exhilarating step. This comprehensive guide will walk you through the process, using Python and the powerful deep learning library, Keras.

Neural Networks Explained from Scratch using Python
Neural Networks Explained from Scratch using Python

Whether you're a beginner or looking to brush up your skills, this tutorial promises a hands-on, step-by-step approach. By the end, you'll have a functioning neural network and a solid foundation for your future machine learning expeditions.

Machine Learning with Python Tutorial
Machine Learning with Python Tutorial

Setting Up Your Environment

Before we dive into neural networks, let's ensure our Python environment is equipped with the necessary tools. We'll use TensorFlow, a robust free and open-source software library for deep learning.

Python Projects | Notion
Python Projects | Notion

Install TensorFlow and Keras with these simple commands:

pip install tensorflow

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

pip install keras

Importing Essential Libraries

Once installed, import the required libraries. It's a tiny step, but it sets the stage for our neural network:

Brand Marketing Strategy, Artificial Neural Network, Digital Data, Python Programming, Big Data, Brand Marketing, Data Science, Python, Programming
Brand Marketing Strategy, Artificial Neural Network, Digital Data, Python Programming, Big Data, Brand Marketing, Data Science, Python, Programming

import numpy as np

from keras.models import Sequential

from keras.layers import Dense

Python
Python

Preparing Your Data

To illustrate, let's use the famous Iris dataset, available in sklearn's datasets. We'll predict the species of an iris flower based on its sepal length, width, petal length, and width.

the mind map to learn how to use python for programming and web development, with text below
the mind map to learn how to use python for programming and web development, with text below
an image of a computer screen with text and numbers on it, including the same language as
an image of a computer screen with text and numbers on it, including the same language as
🐍 Python for Everything 🚀 | Best Python Libraries & Tools Every Developer Should Learn
🐍 Python for Everything 🚀 | Best Python Libraries & Tools Every Developer Should Learn
Python Tutorial
Python Tutorial
Learn Python in 10 days
Learn Python in 10 days
a blue snake with the words python on it
a blue snake with the words python on it
بايثون
بايثون
Redes Neurais
Redes Neurais
Python Libraries for AI and Machine Learning (Cheat Sheet)
Python Libraries for AI and Machine Learning (Cheat Sheet)

Load the data and split it into features and labels:

from sklearn.datasets import load_iris

iris = load_iris()

X, y = iris.data, iris.target

Architecting Your Neural Network

Now, it's time to architect our neural network. In Keras, we add layers to our model sequentially. Here's a simple model with an input layer, two hidden layers, and an output layer:

model = Sequential()

model.add(Dense(16, input_dim=4, activation='relu'))

model.add(Dense(32, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

Compiling Your Model

Next, we compile the model by specifying the optimizer, loss function, and evaluation metrics:

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

Training Your Model

Finally, we train the model using our data:

model.fit(X, y, epochs=150, batch_size=10)

You've just built and trained your first neural network! Remember, machine learning is an iterative process. Tweak your model's structure, parameters, and training regime to boost its performance.

Your newfound skills aren't confined to Iris prediction. Apply your neural network building prowess to other datasets, from image classification to natural language processing. Happy learning!"