Difference between revisions of "Python-for-Machine-Learning/C3/Artificial-Neural-Networks/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " <div style="margin-left:1.27cm;margin-right:0cm;"></div> {| border="1" |- || '''Visual Cue''' || '''Narration''' |- || Show Slide: '''Welcome and Title Slide''' || Welcom...")
 
Line 1: Line 1:
 
 
 
 
 
<div style="margin-left:1.27cm;margin-right:0cm;"></div>
 
 
{| border="1"
 
{| border="1"
 
|-
 
|-
Line 11: Line 5:
 
|-
 
|-
 
|| Show Slide: '''Welcome and Title Slide'''
 
|| Show Slide: '''Welcome and Title Slide'''
|| Welcome to the Spoken Tutorial on''' Artificial Neural Networks'''
+
|| Welcome to the Spoken Tutorial on ''' Artificial Neural Networks'''
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn about
 
|| In this tutorial, we will learn about
* <div style="margin-left:1.27cm;margin-right:0cm;">The fundamentals of '''Artificial Neural Networks (ANN)'''</div>
+
* The fundamentals of '''Artificial Neural Networks (ANN)'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Implementing '''Multi- Layer Perceptron (MLP)''' classification </div>
+
* Implementing '''Multi- Layer Perceptron (MLP)''' classification  
* <div style="margin-left:1.27cm;margin-right:0cm;">Evaluating the performance of a trained '''MLP''' model</div>
+
* Evaluating the performance of a trained '''MLP''' model.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu Linux 24.04'''</div>
+
* '''Ubuntu Linux 24.04'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Jupyter Notebook IDE'''</div>
+
* '''Jupyter Notebook IDE'''
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
 
|| To follow this tutorial,
 
|| To follow this tutorial,
  
The learner must have basic knowledge of '''Python.'''
+
The learner must have basic knowledge of '''Python'''.
  
 
For pre-requisite Python tutorials, please visit this website.
 
For pre-requisite Python tutorials, please visit this website.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
Code-Files
 
Code-Files
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">The files used in this tutorial are provided in the '''Code files '''link.</div>
+
* The files used in this tutorial are provided in the '''Code files '''link.
* <div style="margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="margin-left:1.27cm;margin-right:0cm;">Make a copy and use them while practicing.</div>
+
* Make a copy and use them while practicing.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Artificial Neural Network'''
 
'''Artificial Neural Network'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Artificial Neural Networks''' are models inspired by the human brain’s processing.</div>
+
* '''Artificial Neural Networks''' are models inspired by the human brain’s processing.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Neurons''' receive '''inputs''', adjust '''weights''', and pass''' outputs''' to other neurons.</div>
+
* '''Neurons''' receive '''inputs''', adjust '''weights''', and pass''' outputs''' to other neurons.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''ANNs '''have '''input''', '''hidden''', and '''output''' layers to process and learn patterns.</div>
+
* '''ANNs '''have '''input''', '''hidden''', and '''output''' layers to process and learn patterns.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Artificial Neural Network'''
 
'''Artificial Neural Network'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">Each connection in '''ANN''' has a '''weight''' that helps it to make better predictions.</div>
+
* Each connection in '''ANN''' has a '''weight''' that helps it to make better predictions.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''ANNs''' learn by adjusting '''weights '''based on''' errors''' to improve accuracy.</div>
+
* '''ANNs''' learn by adjusting '''weights '''based on''' errors''' to improve accuracy.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Multi-Layer Perceptron'''
 
'''Multi-Layer Perceptron'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">'''MLP''' is a type of '''ANN''' with '''multiple hidden layers''' that enhance feature learning.</div>
+
* '''MLP''' is a type of '''ANN''' with '''multiple hidden layers''' that enhance feature learning.
* <div style="margin-left:1.27cm;margin-right:0cm;">It processes data through '''interconnected neurons''' in a forward direction.</div>
+
* It processes data through '''interconnected neurons''' in a forward direction.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''MLP''' is widely used for '''classification''' and''' regression''' tasks in machine learning.</div>
+
* '''MLP''' is widely used for '''classification''' and''' regression''' tasks in machine learning.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
Line 76: Line 70:
 
|| Let’s look at the architecture of an '''Artificial Neural Network'''.
 
|| Let’s look at the architecture of an '''Artificial Neural Network'''.
  
'''The Input Layer''' receives the data from the dataset.The number of input neurons matches the number of input features.'''Hidden Layers''' process these inputs through weighted connections.These layers help the network learn complex patterns.The number of hidden '''neurons''' depends on task complexity.
+
'''The Input Layer''' receives the data from the dataset.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
 
 +
The number of input neurons matches the number of input features.
 +
 
 +
'''Hidden Layers''' process these inputs through weighted connections.
 +
 
 +
These layers help the network learn complex patterns.
 +
 
 +
The number of hidden '''neurons''' depends on task complexity.
 +
|-
 
|| Show Slide:
 
|| Show Slide:
  
Line 83: Line 85:
  
 
'''arch.png'''
 
'''arch.png'''
|| '''The Output Layer''' produces the final prediction or classification.The number of '''output neurons''' matches the number of output classes.'''Weights''' are updated during training to optimize performance.
+
|| '''The Output Layer''' produces the final prediction or classification.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
 
 +
The number of '''output neurons''' matches the number of output classes.
 +
 
 +
'''Weights''' are updated during training to optimize performance.
 +
|-
 
|| Show Slide:
 
|| Show Slide:
  
Line 101: Line 107:
  
 
The output is then passed to the next layer in the network.
 
The output is then passed to the next layer in the network.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Hover over the files
 
|| Hover over the files
 
|| I have created Artificial neural networks. Ipynb file for the demonstration."
 
|| I have created Artificial neural networks. Ipynb file for the demonstration."
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Press '''Ctrl+Alt+T '''keys
 
|| Press '''Ctrl+Alt+T '''keys
  
Line 110: Line 116:
  
 
Press '''Enter'''
 
Press '''Enter'''
|| Let us open the Linux terminal by pressing '''Ctrl, Alt and T '''keys together.
+
|| Let us open the '''Linux''' terminal by pressing '''Ctrl, Alt and T '''keys together.
  
 
Activate the machine learning environment as shown.
 
Activate the machine learning environment as shown.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| To go to the '''Downloads '''folder,
 
|| To go to the '''Downloads '''folder,
  
Line 124: Line 130:
  
 
Then type, '''jupyter space notebook '''and press '''Enter.'''
 
Then type, '''jupyter space notebook '''and press '''Enter.'''
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Jupyter Notebook Home Page:Click on
 
|| Show Jupyter Notebook Home Page:Click on
  
Line 132: Line 138:
 
Click the''' Artificial neural networks dot ipynb''' file to open it.
 
Click the''' Artificial neural networks dot ipynb''' file to open it.
  
<div style="color:#000000;">Note that each cell will have the output displayed in this file.</div>
+
Note that each cell will have the output displayed in this file.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 140: Line 146:
 
'''import matplotlib.pyplot as plt'''
 
'''import matplotlib.pyplot as plt'''
 
'''import seaborn as sns'''
 
'''import seaborn as sns'''
|| First, we import the necessary libraries for ANN.
+
|| First, we import the necessary libraries for '''ANN'''.
  
 
Make sure to Press '''Shift '''and '''Enter '''to execute the code in each cell.
 
Make sure to Press '''Shift '''and '''Enter '''to execute the code in each cell.
Line 150: Line 156:
 
The target variable is '''0''' for '''malignant tumors '''and '''1''' for '''benign tumors'''.
 
The target variable is '''0''' for '''malignant tumors '''and '''1''' for '''benign tumors'''.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 157: Line 163:
  
 
Then, we create a dataframe using a '''pd dot dataframe''' for data handling.
 
Then, we create a dataframe using a '''pd dot dataframe''' for data handling.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
 
'''bcancer_df.tail() '''
 
'''bcancer_df.tail() '''
 
|| To inspect the dataset, we display the last five rows using the '''tail''' function.
 
|| To inspect the dataset, we display the last five rows using the '''tail''' function.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Only narration
 
|| Only narration
  
Line 169: Line 175:
 
'''bcancer_df.shape'''
 
'''bcancer_df.shape'''
 
|| The''' shape''' function returns the number of rows and columns in the dataframe.
 
|| The''' shape''' function returns the number of rows and columns in the dataframe.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
 
'''print("Feature names:", data.feature_names)'''
 
'''print("Feature names:", data.feature_names)'''
 
|| '''data dot feature underscore names''' displays the columns of different '''tumor '''characteristics.
 
|| '''data dot feature underscore names''' displays the columns of different '''tumor '''characteristics.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 180: Line 186:
  
 
|| Next we display the class labels of the target variable.
 
|| Next we display the class labels of the target variable.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 188: Line 194:
  
 
'''Malignant''' tumors are shown in red and '''benign''' tumors in green.
 
'''Malignant''' tumors are shown in red and '''benign''' tumors in green.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show output
 
|| Show output
 
|| Whiskers in a box plot represent the range of data within a certain boundary.
 
|| Whiskers in a box plot represent the range of data within a certain boundary.
Line 197: Line 203:
  
 
The '''benign''' class has a lower mean radius with fewer outliers.
 
The '''benign''' class has a lower mean radius with fewer outliers.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Only Narration
 
|| Only Narration
  
Line 207: Line 213:
 
|| Now, let’s preprocess the dataset.
 
|| Now, let’s preprocess the dataset.
  
To handle outliers, we normalize the features using '''Robust Scaler'''. It scales values based on the '''median''' and '''IQR''', making it resistant to '''outliers'''.
+
To handle outliers, we normalize the features using '''Robust Scaler'''.  
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
It scales values based on the '''median''' and '''IQR''', making it resistant to '''outliers'''.
 +
 
 +
|-
 
|| Highlight
 
|| Highlight
  
Line 217: Line 225:
  
 
The '''y''' variable stores the '''target classes''' for classification.
 
The '''y''' variable stores the '''target classes''' for classification.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
 
'''X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)'''
 
'''X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)'''
 
|| Next, we split the data into '''training '''and '''testing '''sets.
 
|| Next, we split the data into '''training '''and '''testing '''sets.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 232: Line 240:
 
Using the''' MLPClassifier''' function, we define '''mlp underscore relu'''.
 
Using the''' MLPClassifier''' function, we define '''mlp underscore relu'''.
  
It has '''two hidden layers''', the first with '''100 neurons '''and second with '''50 neurons'''.
+
It has '''two hidden layers''', the first with '''100 neurons ''' and second with '''50 neurons'''.
  
The model uses the '''Rectified Linear Unit '''i.e '''ReLU activation function''' for faster convergence.
+
The model uses the '''Rectified Linear Unit '''that is '''ReLU activation function''' for faster convergence.
  
 
'''ReLU '''helps the '''MLP''' to train effectively.
 
'''ReLU '''helps the '''MLP''' to train effectively.
  
 
The model is trained for a maximum of '''1000 iterations''' to optimize performance.
 
The model is trained for a maximum of '''1000 iterations''' to optimize performance.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
 
'''y_train_pred_relu = mlp_relu.predict(X_train)'''
 
'''y_train_pred_relu = mlp_relu.predict(X_train)'''
 
|| Once trained, we predict class labels for''' X_train''' using our model.
 
|| Once trained, we predict class labels for''' X_train''' using our model.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 251: Line 259:
  
 
The result is formatted to three decimal places and printed.
 
The result is formatted to three decimal places and printed.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show output
 
|| Show output
|| The MLP model achieves a training accuracy of '''92.2%''' using ReLU activation.
+
|| The MLP model achieves a training accuracy of '''92.2%''' using '''ReLU''' activation.
  
 
This indicates that the model has learned well from the training data.
 
This indicates that the model has learned well from the training data.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 269: Line 277:
  
 
A decreasing '''loss curve''' indicates effective training.
 
A decreasing '''loss curve''' indicates effective training.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show output
 
|| Show output
 
|| The''' training loss curve''' shows how the model's''' error''' decreases over iterations.
 
|| The''' training loss curve''' shows how the model's''' error''' decreases over iterations.
Line 278: Line 286:
  
 
Model '''convergence''' means training has optimized weights, with minimal further gain.
 
Model '''convergence''' means training has optimized weights, with minimal further gain.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 287: Line 295:
  
 
We calculate and print the testing accuracy.
 
We calculate and print the testing accuracy.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show output
 
|| Show output
 
|| The model achieves a testing accuracy of '''95.9%'''.
 
|| The model achieves a testing accuracy of '''95.9%'''.
  
 
This indicates strong generalization to unseen data.
 
This indicates strong generalization to unseen data.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 304: Line 312:
  
 
The curve shows the balance between '''true positive rate''' and '''false positive rate'''.
 
The curve shows the balance between '''true positive rate''' and '''false positive rate'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show output
 
|| Show output
|| The''' Area under the curve''' i.e, '''AUC value of 0.99''' confirms the model’s strong classification ability.
+
|| The''' Area under the curve''' that is, '''AUC value of 0.99''' confirms the model’s strong classification ability.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Highlight
 
|| Highlight
  
Line 313: Line 321:
 
|| Finally, we display the '''classification report''' of the model.
 
|| Finally, we display the '''classification report''' of the model.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show output
 
|| Show output
 
|| The output indicates that the model performs well in both classes with above 95% precision.
 
|| The output indicates that the model performs well in both classes with above 95% precision.
Line 320: Line 328:
  
 
Thus, the model has learnt to classify whether the patient has breast cancer or not.  
 
Thus, the model has learnt to classify whether the patient has breast cancer or not.  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:'''Summary'''
 
|| Show Slide:'''Summary'''
|| This brings us to the end of the tutorial. Let us summarize.
 
  
 
In this tutorial, we have learnt about
 
In this tutorial, we have learnt about
* <div style="margin-left:1.27cm;margin-right:0cm;">The fundamentals of '''Artificial Neural Networks (ANN)'''</div>
+
* The fundamentals of '''Artificial Neural Networks (ANN)'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Implementing '''Multi- Layer Perceptron (MLP)''' classification </div>
+
* Implementing '''Multi- Layer Perceptron (MLP)''' classification  
* <div style="margin-left:1.27cm;margin-right:0cm;">Evaluating the performance of a trained '''MLP''' model</div>
+
* Evaluating the performance of a trained '''MLP''' model
 +
|| This brings us to the end of the tutorial. Let us summarize.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Assignment '''
 
'''Assignment '''
 
|| As an assignment, please do the following
 
|| As an assignment, please do the following
* <div style="margin-left:1.27cm;margin-right:0cm;">Use''' activation='logistic' '''instead of '''activation='relu''''</div>
+
* Use''' activation='logistic' '''instead of '''activation='relu''''
* <div style="margin-left:1.27cm;margin-right:0cm;">Evaluate the performance using accuracy and classification report for test set</div>
+
* Evaluate the performance using accuracy and classification report for test set
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''Assignment Solution'''
 
'''Assignment Solution'''
 
|| After execution, we should get the accuracy and classification report as shown here.
 
|| After execution, we should get the accuracy and classification report as shown here.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  
 
'''FOSSEE Forum'''
 
'''FOSSEE Forum'''
|| <span style="background-color:#ffffff;">For any general or technical questions on </span>'''Python for'''
+
|| For any general or technical questions on '''Python for'''
  
'''Machine Learning'''<span style="background-color:#ffffff;">, visit the</span><span style="background-color:#ffffff;">''' FOSSEE forum'''</span><span style="background-color:#ffffff;"> and post your question.</span>
+
'''Machine Learning''', visit the''' FOSSEE forum''' and post your question.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-
 
|| Show Slide:
 
|| Show Slide:
  

Revision as of 15:19, 16 July 2025

Visual Cue Narration
Show Slide: Welcome and Title Slide Welcome to the Spoken Tutorial on Artificial Neural Networks
Show Slide:

Learning Objectives

In this tutorial, we will learn about
  • The fundamentals of Artificial Neural Networks (ANN)
  • Implementing Multi- Layer Perceptron (MLP) classification
  • Evaluating the performance of a trained MLP model.
Show Slide: To record this tutorial, I am using
  • Ubuntu Linux 24.04
  • Jupyter Notebook IDE
Show Slide: To follow this tutorial,

The learner must have basic knowledge of Python.

For pre-requisite Python tutorials, please visit this website.

Show Slide:

Code-Files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and use them while practicing.
Show Slide:

Artificial Neural Network

  • Artificial Neural Networks are models inspired by the human brain’s processing.
  • Neurons receive inputs, adjust weights, and pass outputs to other neurons.
  • ANNs have input, hidden, and output layers to process and learn patterns.
Show Slide:

Artificial Neural Network

  • Each connection in ANN has a weight that helps it to make better predictions.
  • ANNs learn by adjusting weights based on errors to improve accuracy.
Show Slide:

Multi-Layer Perceptron

  • MLP is a type of ANN with multiple hidden layers that enhance feature learning.
  • It processes data through interconnected neurons in a forward direction.
  • MLP is widely used for classification and regression tasks in machine learning.
Show Slide:

ANN Architecture

arch.png

Let’s look at the architecture of an Artificial Neural Network.

The Input Layer receives the data from the dataset.

The number of input neurons matches the number of input features.

Hidden Layers process these inputs through weighted connections.

These layers help the network learn complex patterns.

The number of hidden neurons depends on task complexity.

Show Slide:

ANN Architecture

arch.png

The Output Layer produces the final prediction or classification.

The number of output neurons matches the number of output classes.

Weights are updated during training to optimize performance.

Show Slide:

Artificial Neuron Model

neuron.png

Now, let's break this down further by understanding a single neuron.

Each neuron receives inputs along with a bias value.

Each input has a weight that determines its importance.

The summation function adds the weighted inputs and bias.

The activation function decides the neuron’s output.

The output is then passed to the next layer in the network.

Hover over the files I have created Artificial neural networks. Ipynb file for the demonstration."
Press Ctrl+Alt+T keys

Type conda activate ml

Press Enter

Let us open the Linux terminal by pressing Ctrl, Alt and T keys together.

Activate the machine learning environment as shown.

To go to the Downloads folder,

Type cd Downloads

Type jupyter notebook

I have saved my code file in the Downloads folder.

Please navigate to the respective folder of your code file location.

Then type, jupyter space notebook and press Enter.

Show Jupyter Notebook Home Page:Click on

Artificial Neural Networks.ipynb file

We can see the Jupyter Notebook Home page has opened in the web browser.

Click the Artificial neural networks dot ipynb file to open it.

Note that each cell will have the output displayed in this file.

Highlight

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

First, we import the necessary libraries for ANN.

Make sure to Press Shift and Enter to execute the code in each cell.

We will use the Breast Cancer Wisconsin dataset from sklearn library.

The dataset has 30 features describing breast tumor characteristics.

The target variable is 0 for malignant tumors and 1 for benign tumors.

Highlight

data = load_breast_cancer()

We load the dataset using the load underscore breast underscore cancer function.

Then, we create a dataframe using a pd dot dataframe for data handling.

Highlight

bcancer_df.tail()

To inspect the dataset, we display the last five rows using the tail function.
Only narration

Highlight

bcancer_df.shape

The shape function returns the number of rows and columns in the dataframe.
Highlight

print("Feature names:", data.feature_names)

data dot feature underscore names displays the columns of different tumor characteristics.
Highlight

print("Target names:", data.target_names)

Next we display the class labels of the target variable.
Highlight

plt.figure(figsize=(12,6)) plt.show()

We create a boxplot to compare mean radius across classes.

Malignant tumors are shown in red and benign tumors in green.

Show output Whiskers in a box plot represent the range of data within a certain boundary.

Outliers are values far from the rest of the data and appear as small dots.

In this plot, the malignant class has a higher mean radius with more variation.

The benign class has a lower mean radius with fewer outliers.

Only Narration

Highlight

scaler = RobustScaler() df_scaled = pd.DataFrame( scaler.fit_transform(bcancer_df.iloc[:, :-1]),

Now, let’s preprocess the dataset.

To handle outliers, we normalize the features using Robust Scaler.

It scales values based on the median and IQR, making it resistant to outliers.

Highlight

X = bcancer_df.drop(columns=["target"]) y = bcancer_df['target']

We define X as the feature set by dropping the target column.

The y variable stores the target classes for classification.

Highlight

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

Next, we split the data into training and testing sets.
Highlight

mlp_relu = MLPClassifier(hidden_layer_sizes=(100, 50), activation='relu', max_iter=1000, random_state=42)

mlp_relu.fit(X_train, y_train)

We then initialize the MLP model for classification.

Using the MLPClassifier function, we define mlp underscore relu.

It has two hidden layers, the first with 100 neurons and second with 50 neurons.

The model uses the Rectified Linear Unit that is ReLU activation function for faster convergence.

ReLU helps the MLP to train effectively.

The model is trained for a maximum of 1000 iterations to optimize performance.

Highlight

y_train_pred_relu = mlp_relu.predict(X_train)

Once trained, we predict class labels for X_train using our model.
Highlight

print("Training Accuracy (ReLU):", format(accuracy_score(y_train, y_train_pred_relu), ".3f"))

We then calculate the training accuracy using the accuracy underscore score.

The result is formatted to three decimal places and printed.

Show output The MLP model achieves a training accuracy of 92.2% using ReLU activation.

This indicates that the model has learned well from the training data.

Highlight

plt.plot(mlp_relu.loss_curve_, label="Training Loss", color="blue")

plt.xlabel("Iterations")

plt.ylabel("Loss")

We plot the training loss curve to track the learning progress.

The x-axis represents iterations, while the y-axis shows the loss value.

A decreasing loss curve indicates effective training.

Show output The training loss curve shows how the model's error decreases over iterations.

Initially, the loss is high but quickly drops, showing rapid learning.

After 20 iterations, the loss stabilizes, indicating model convergence.

Model convergence means training has optimized weights, with minimal further gain.

Highlight

y_pred_relu = mlp_relu.predict(X_test)

accuracy_relu = accuracy_score(y_test, y_pred_relu)

Next, we predict the class labels on the test data.

We calculate and print the testing accuracy.

Show output The model achieves a testing accuracy of 95.9%.

This indicates strong generalization to unseen data.

Highlight

y_probs = mlp_relu.predict_proba(X_test)[:, 1]

fpr, tpr, _ = roc_curve(y_test, y_probs)

roc_auc = auc(fpr, tpr)

We further evaluate the performance using a ROC curve.

The curve shows the balance between true positive rate and false positive rate.

Show output The Area under the curve that is, AUC value of 0.99 confirms the model’s strong classification ability.
Highlight

print("\nMLP with ReLU activation - Classification Report:")

Finally, we display the classification report of the model.
Show output The output indicates that the model performs well in both classes with above 95% precision.

The other metrics like recall and F1-score suggest that it correctly identifies most positive instances.

Thus, the model has learnt to classify whether the patient has breast cancer or not.

Show Slide:Summary

In this tutorial, we have learnt about

  • The fundamentals of Artificial Neural Networks (ANN)
  • Implementing Multi- Layer Perceptron (MLP) classification
  • Evaluating the performance of a trained MLP model
This brings us to the end of the tutorial. Let us summarize.
Show Slide:

Assignment

As an assignment, please do the following
  • Use activation='logistic' instead of activation='relu'
  • Evaluate the performance using accuracy and classification report for test set
Show Slide:

Assignment Solution

After execution, we should get the accuracy and classification report as shown here.
Show Slide:

FOSSEE Forum

For any general or technical questions on Python for

Machine Learning, visit the FOSSEE forum and post your question.

Show Slide:

Thank You

This is Anvita Thadavoose Manjummel, a FOSSEE Summer Fellow 2025, IIT Bombay signing off

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat