Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
Step-by-Step Guide to Build, Train, and Evaluate Your
First Neural Network
🧠Introduction
Now that you’ve explored the architecture and types of
neural networks, it’s time to build your first real project.
In this hands-on chapter, you’ll build a simple yet complete
neural network using Python and Keras to solve a real-world problem: predicting
whether a customer will make a purchase based on features.
You’ll learn how to:
Let’s begin!
📘 Section 1: Problem
Statement
We’ll use a binary classification problem:
Predict whether a customer will buy a product based
on features like age, salary, and prior behavior.
Dataset: Simulated dataset with 4 features:
Target label: Purchased (0/1)
📘 Section 2: Environment
Setup
📦 Required Libraries:
bash
pip
install numpy pandas scikit-learn matplotlib seaborn keras tensorflow
📘 Section 3: Import
Libraries and Load Data
python
import
numpy as np
import
pandas as pd
import
matplotlib.pyplot as plt
import
seaborn as sns
from
sklearn.model_selection import train_test_split
from
sklearn.preprocessing import StandardScaler
from
keras.models import Sequential
from
keras.layers import Dense
Sample Data Creation (if no CSV):
python
#
Create a dummy dataset
data
= {
'Age': np.random.randint(20, 60, 200),
'Salary': np.random.randint(30000, 100000,
200),
'Last_Purchase': np.random.randint(0, 2,
200),
'SpendingScore': np.random.randint(20, 100,
200),
'Purchased': np.random.randint(0, 2, 200)
}
df
= pd.DataFrame(data)
df.head()
📘 Section 4: Data
Preprocessing
python
X
= df[['Age', 'Salary', 'Last_Purchase', 'SpendingScore']]
y
= df['Purchased']
#
Normalize features
scaler
= StandardScaler()
X_scaled
= scaler.fit_transform(X)
#
Split data
X_train,
X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2,
random_state=42)
📘 Section 5: Building the
Neural Network
python
model
= Sequential()
#
Input + First hidden layer
model.add(Dense(units=8,
activation='relu', input_dim=4))
#
Second hidden layer
model.add(Dense(units=4,
activation='relu'))
#
Output layer
model.add(Dense(units=1,
activation='sigmoid'))
#
Compile model
model.compile(optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy'])
📘 Section 6: Training the
Model
python
history
= model.fit(X_train, y_train, validation_split=0.2, epochs=50, batch_size=8,
verbose=1)
📘 Section 7: Visualizing
Training Performance
python
plt.plot(history.history['accuracy'],
label='Training Accuracy')
plt.plot(history.history['val_accuracy'],
label='Validation Accuracy')
plt.title('Model
Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
python
plt.plot(history.history['loss'],
label='Training Loss')
plt.plot(history.history['val_loss'],
label='Validation Loss')
plt.title('Model
Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
📘 Section 8: Evaluating
the Model
python
loss,
accuracy = model.evaluate(X_test, y_test)
print(f"Test
Loss: {loss:.2f}")
print(f"Test
Accuracy: {accuracy:.2f}")
📘 Section 9: Making
Predictions
python
new_data
= np.array([[35, 50000, 1, 70]])
new_scaled
= scaler.transform(new_data)
prediction
= model.predict(new_scaled)
print("Purchase
Probability:", prediction[0][0])
📘 Section 10: Summary
& Project Tips
🔧 Common Issues and
Fixes:
Issue |
Solution |
Overfitting |
Use dropout, reduce
layers, early stopping |
Low accuracy |
Normalize
data, tune learning rate |
Poor predictions |
Add more data or
engineer better features |
Slow training |
Reduce batch
size or dimensions |
✅ Project Recap:
✅ Chapter Summary Table
Step |
Action |
Load Data |
Used dummy customer
dataset |
Preprocess Data |
Scaled and
split into train/test |
Build Model |
Keras Sequential with
2 hidden layers |
Train |
50 epochs
with accuracy tracking |
Evaluate |
Accuracy, loss,
prediction examples |
Answer: A neural network is a computer system designed to recognize patterns, inspired by how the human brain works. It learns from examples and improves its accuracy over time, making it useful for tasks like image recognition, language translation, and predictions.
Answer: It learns through a process called training, which involves:
Answer: Basic understanding of algebra and statistics helps, but you don’t need advanced math to get started. Many tools like Keras or PyTorch simplify the process so you can learn through experimentation and visualization.
Answer: Neural networks are the building blocks of deep learning. When we stack multiple hidden layers together, we get a deep neural network — the foundation of deep learning models.
Answer: An activation function decides whether a neuron should be activated or not. It introduces non-linearity to the model, allowing it to solve complex problems. Common ones include ReLU, Sigmoid, and Tanh.
Answer: Supervised learning is a type of machine learning where models learn from labeled data. Neural networks can be used within supervised learning as powerful tools to handle complex data like images, audio, and text.
Answer: Not always. Neural networks require large datasets and computing power. For small datasets or structured data, simpler models like decision trees or SVMs may perform just as well or better.
Answer: Start with:
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)