0% found this document useful (0 votes)
10 views2 pages

Iris Classification with Neural Network

The document outlines a process for solving the Iris classification problem using a multi-layer neural network. It details loading the dataset, preprocessing features, splitting the data into training and test sets, and training the model with specified hyperparameters. The final output includes training results and accuracy metrics for the test set.

Uploaded by

milena3334araujo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Iris Classification with Neural Network

The document outlines a process for solving the Iris classification problem using a multi-layer neural network. It details loading the dataset, preprocessing features, splitting the data into training and test sets, and training the model with specified hyperparameters. The final output includes training results and accuracy metrics for the test set.

Uploaded by

milena3334araujo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Welcome to Juno!

#
# Here, we tackle the same Iris classification problem that we explore in the
introductory Jupyter notebook.
# We start by loading a sample dataset, and then proceed to build and train a classifier
model utilizing a multi-layer neural network -- all executed locally on your device!

import pandas as pd
from [Link] import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn import metrics

def pretty_format(text, color=None, font_weight=None):


"""
Formats text with specified color and font weight using ANSI escape sequences.
"""
colors = {
'green': '\033[32m',
'red': '\033[31m',
'blue': '\033[34m',
'gray': '\033[90m'
}
weights = {
'bold': '\033[1m',
'italic': '\033[3m'
}
reset = '\033[0m'
color_code = [Link](color, '')
weight_code = [Link](font_weight, '')
return f"{weight_code}{color_code}{text}{reset}"

if __name__ == '__main__':
# The dataset is stored in the `[Link]` file located in Juno's on-device storage,
in the `/Documents/welcome-data` folder.
print(pretty_format('Dataset', font_weight='bold'))
print('Reading from ' + pretty_format('[Link]', 'gray', 'italic') + ' file on
disk...')
df = pd.read_csv("welcome-data/[Link]")
print('Data loaded successfully.')
print('\n')

# All unique Iris species from the data set


species = [Link](df['Species'].sort_values())
# List of measured features in the data set
features = ['Sepal length (cm)', 'Sepal width (cm)', 'Petal length (cm)', 'Petal
width (cm)']
# Number of samples for each species
counts = df['Species'].value_counts().sort_index()
print(pretty_format('Sample counts', font_weight='bold'))
for species, count in [Link]():
print(pretty_format(f' {species}', font_weight='italic') + ': ' +
pretty_format(f'{count}', font_weight='bold') + ' samples')
print('\n')
# Scale features and encode data labels
feature_scaler = StandardScaler()
X = feature_scaler.fit_transform(df[features].values)
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(df['Species'])

# Allocate 30% of original dataset to test set


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
print(pretty_format('Data split', font_weight='bold'))
print(pretty_format(' Training', font_weight='italic') + ' set: ' +
pretty_format(f'{len(X_train)}', font_weight='bold') + ' samples')
print(pretty_format(' Test', font_weight='italic') + ' set: ' +
pretty_format(f'{len(X_test)}', font_weight='bold') + ' samples')
print('\n')

# Training and hyperparameters


solver = 'sgd' # Use stochastic gradient descent as optimization method
max_iter = 500
learning_rate_init = 0.1
hidden_layer_sizes = (5, 3)
random_state = 42
model = MLPClassifier(solver=solver, max_iter=max_iter,
learning_rate_init=learning_rate_init, hidden_layer_sizes=hidden_layer_sizes,
random_state=random_state)
print(pretty_format('Hyperparameters and architecture', font_weight='bold'))
print(' Solver: ' + pretty_format(f'{solver}', font_weight='bold'))
print(' Max iterations: ' + pretty_format(f'{max_iter}', font_weight='bold'))
print(' Initial learning rate: ' + pretty_format(f'{learning_rate_init}',
font_weight='bold'))
print(' Hidden layers: ' + pretty_format(f'{hidden_layer_sizes}',
font_weight='bold'))
print('\n')

# Fit the classifier model


[Link](X_train, y_train)
print(pretty_format('Training', font_weight='bold'))
print('Completed after ' + pretty_format(f'{model.n_iter_}', font_weight='bold') + '
iterations.')
print(' Training loss: ' + pretty_format(f'{model.loss_:.4f}', color='green',
font_weight='bold'))
print(' Test set accuracy: ' +
pretty_format(f'{metrics.accuracy_score([Link](X_test), y_test):.4f}',
color='green', font_weight='bold'))

Common questions

Powered by AI

ANSI escape sequences are utilized in the Juno project to format the console output with colors and font weights, enhancing readability and improving user experience . This visual formatting helps users quickly identify different types of information, such as error messages or important details, making it easier to parse console outputs in real-time, especially during debugging and monitoring of model training .

The neural network model in the Juno project utilizes a multi-layer perceptron with a hidden layer configuration of (5, 3), meaning two hidden layers with 5 and 3 neurons respectively . The solver used for optimization is stochastic gradient descent (SGD), with a maximum of 500 iterations and an initial learning rate of 0.1. These architectural and training parameters directly affect model performance; the structure indicates the model's capacity to learn complex patterns, while the optimization parameters control the speed and stability of convergence. A small network might struggle with complex data, and inappropriate learning rates can lead to either slow training or a failure to converge .

The performance of the neural network in the Juno project is evaluated using accuracy on the test set . Accuracy measures the proportion of correctly classified instances out of the total instances and provides a quick overview of how effectively the model can predict the class labels. A high accuracy score on the test set suggests that the model has generalized well and performs effectively on unseen data, though it should be noted that relying solely on accuracy can be misleading in imbalanced datasets .

The Juno program utilizes the `StandardScaler` from `sklearn.preprocessing` to scale the features of the Iris dataset, which is crucial for ensuring that each feature contributes equally to the distance calculations, particularly important in gradient-based algorithms like the neural network used here . Label encoding is conducted using `LabelEncoder`, which converts the categorical species labels into numeric values. This step is essential because machine learning algorithms typically function on numeric data .

Determining the number of iterations and the learning rate is critical to the training process of neural networks, as seen in the Juno project . The number of iterations dictates how many times the model will update weights through the entire training set, influencing model performance and the likelihood of convergence. An overly high number may lead to overfitting, while too few could result in underfitting. The learning rate controls the step size during weight updates; it is essential for balancing speed of convergence and stability of the training. A high learning rate can cause diverging behavior, whereas a low rate can result in excessively slow training . Proper tuning of these hyperparameters is therefore crucial to achieving an optimal balance between model performance and training efficiency .

Stochastic gradient descent (SGD) offers several advantages in the training of neural networks, such as reduced memory footprint and faster initial learning due to processing one sample at a time . However, it also introduces more variance in the update process, which can lead to a less stable convergence path and might require careful tuning of learning rates and batch sizes. Despite these challenges, SGD can escape local minima more effectively in some landscapes, which is useful in training complex models .

The use of a multi-layer perceptron (MLP) in the Juno project provides the model with the ability to capture non-linear relationships within the Iris dataset through its layered, neural network structure . This is particularly beneficial in distinguishing between the three species of Iris, which may not be linearly separable. The MLP's flexibility in function approximation makes it a suitable choice for the Iris classification problem, as it can adjust weights during training to learn complex patterns through its hidden layers, improving prediction accuracy compared to simpler, linear models .

The Iris dataset is split into training and testing sets using a 70-30 ratio, with 70% of the data allocated to training and 30% to testing . Altering this ratio can significantly impact model performance; a larger training set may improve the model's ability to learn patterns, potentially enhancing accuracy, while a larger test set can provide a more reliable estimate of how the model will perform on unseen data. However, insufficient training data might lead to underfitting, whereas too little testing data can lead to unreliable evaluation .

Applying feature scaling using StandardScaler in the Juno project standardizes the range of continuous initial inputs, ensuring that each feature effect in the model's learning process is balanced . In neural networks, such scaling helps in achieving faster convergence during training, as it prevents features with larger magnitudes from disproportionately affecting the model and helps in avoiding numerical stability issues. This is particularly pertinent when using gradient-based optimizers like in the MLP implementation, facilitating more efficient and stable learning .

Setting a random state in the Juno project ensures that the random processes involved in splitting the data and initializing the weights in the neural network can be reproduced identically across different runs . This is crucial for reproducibility, allowing other researchers or users to verify the results and build upon the findings with confidence, as it removes variability introduced by random operations .

You might also like