Preprocessing the Iris Dataset for TensorFlow

Introduction

In today's lesson, our focus is on preprocessing the Iris dataset for TensorFlow. We will explore various techniques, such as data splitting, feature scaling, and one-hot encoding. This foundation is invaluable in the field of machine learning as it aids in understanding the intricacies of data transformation before we feed it to a neural network. Let's get into it!

Overview of the Iris Dataset

Before we delve into data preprocessing, it is imperative to understand the data we are processing. The Iris dataset comprises measurements from 150 Iris flowers coming from three different species. Each sample includes the following 4 features:

  • Sepal length (cm): e.g., 5.1, 4.9, 4.7, etc.
  • Sepal width (cm): e.g., 3.5, 3.0, 3.2, etc.
  • Petal length (cm): e.g., 1.4, 1.4, 1.3, etc.
  • Petal width (cm): e.g., 0.2, 0.2, 0.2, etc.

Additionally, each sample has a class label representing the Iris species. The targets in the dataset are represented as one of the following options:

  • Iris setosa: 0
  • Iris versicolor: 1
  • Iris virginica: 2

With these measurements and labels, the Iris dataset becomes a multivariate dataset often used for machine learning introductions.

Insight into Data Preprocessing

Data preprocessing is a crucial step in machine learning. It is the process of converting or mapping data from the initial form to another format to prepare the data for the next processing phase. This converted data could be easier for the algorithms to extract information, hence improving their ability to predict. The steps involved in preprocessing we will cover in today's lesson include data load, split, scale, and encode.

Step 1: Loading the Dataset

Before diving into preprocessing, let's start by loading the Iris dataset. We use the load_iris function from scikit-learn for this purpose. It returns the feature matrix X and the target vector y.

Python
from sklearn.datasets import load_iris

# Load the Iris dataset
iris = load_iris()
X, y = iris.data, iris.target

# Displaying shapes
print(f'X shape: {X.shape}')
print(f'y shape: {y.shape}')

The output will be:

text
X shape: (150, 4)
y shape: (150,)

Here, X contains 150 samples, each with 4 features (sepal length, sepal width, petal length, and petal width). The y vector contains 150 class labels, with each label representing one of the three Iris species. This initial step helps us understand the dimensions of our dataset before we proceed with further processing.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal