↓ Skip to main content

Machine Learning 101-II- Iris Flower Data Set? The 'Hello

Preparing for a Real-Life Machine Learning Project In the first part of this series of posts.

Updated

Diagram: Machine Learning 101-II- Iris Flower Data Set? The 'Hello
Original diagram for Techfastly by Vin Patel.
On this page

Key takeaways

  • In the first part of this series of posts, we leveraged a 2016 YouTube video by Google to create an algorithm with six lines of code.
  • !Diagram: Diagram Original diagram for Techfastly by Vin Patel. For the program to output the actual field values, we need to name the columns of the file and ensure that they and the values are read by it.
  • !Diagram: Diagram Original diagram for Techfastly by Vin Patel. Congratulations!

Preparing for a Real-Life Machine Learning Project #

In the first part of this series of posts, we leveraged a 2016 YouTube video by Google to create an algorithm with six lines of code. The purpose of the exercise was to show that with just a few lines of code, a non-programmer like me was able to create a Machine Learning Algorithm.

Now, let’s take this process a bit further. Whether you deal with a small project or a large project in Machine Learning, the overall process is the same. I have now relied on a combination of this post by Jason Brownlee, and this YouTube video from Google.

In the following steps, I have summarized the series of steps in Machine Learning:

  1. Setting Up the Environment
  2. Loading the data set
  3. Verifying the data set
  4. Choosing a Machine Learning Model & Training It
  5. Verifying the Accuracy of the Model

A. Setting Up the Environment #

You can view ready-made data sets in sci-kit (a machine learning library) here-. You can choose any of the seven data sets. In this post, I picked the Iris flower data set that contains a collection of 150 records under five attributes - petal length, petal width, sepal length, sepal width, and the name of species.

In case you are wondering what the difference between a sepal and a petal is, the following image should help clarify that.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

The database of 150 petal measurements covers three different flowers (labels) and has four dimensions (features). The four features are- sepal length in cm, sepal width in cm, petal length in cm, and petal width in cm. The three labels (flowers) are Iris Serosa, Iris Versicolor, and Iris Virginia.

To pick a data set, the first thing we do is- import SciPy. SciPy is a free and open-source Python library used for scientific computing and technical computing.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

As the data set is in a CSV format, we need to import a module that can read a CSV file. For that, we import pandas. In computer programming, pandas is a software library written for the Python programming language for data manipulation and analysis.

As all the values in this dataset are numerical, we also import NumPy. NumPy is a library for the Python programming language, adding support for large, mufti-dimensional arrays and matrices, along with a comprehensive collection of high-level mathematical functions to operate on these arrays.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

And lastly, as we need to import a Machine Learning Library, we import sklearn. Sklearn is used to build machine learning models.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

Now we need to import the specific components we need from each of these modules for use:

From NumPy, we import the ability to read a CSV file by typing in- from pandas import read_csv.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

From sklearn, we import the ability to select a specific machine learning model and the ability to split our data and examine it for training before being able to test it.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

We then add in two more lines of code- lines 7 and 8 to ensure that we can get a read on the accuracy of our algorithm and to be able to use the Decision Tree approach on which to train our classifier.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

At this point, we have all the libraries we think we need. If we find out at a later point in time that we need additional libraries, we can always go back and add them.

B. Loading the Dataset #

We can access the data set in any one of the following three ways:

  1. Some test data sets for beginners are available inside packages like SciPy

  2. Same data sets are available online URLs

  3. Some data sets are proprietary and are hence available internally on corporate systems

In our example here, the data set is on a CSV file, and we have called in the read_csv function from pandas. All we now need to do is to specify where the dataset resides.

We do that by specifying the URL by typing in URL = “https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv".

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

After each line of code, it is a good idea to use the print statement so that we can see if any errors have occurred. So, in this case, we execute print(URL).

We see that the program did execute the command and printed the URL itself. Our goal, though, is to have it print the contents of the file. We, therefore, did not get the validation for which we were looking.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

For the program to output the actual field values, we need to name the columns of the file and ensure that they and the values are read by it.

Let’s first examine if the CSV file has headers.

A screenshot of the file from the URL confirms that the file has no headers.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

So we type in the following to our program to name the columns and have the program read the values in:

names = [‘sepal_length’, ‘sepal_width’, ‘petal_length’, ‘petal_width’, ‘class’]

dataset = read_csv(URL, names=names)

To confirm that our algorithm can see the named values in the data sets appropriately, we use the print statement again. Your program and the output should now look like this:

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

C. Verifying the Data set #

To confirm that the program has identified the rows and columns of the data set correctly, we can use the following line of code:

print(dataset.shape)

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

The output of 150, five refers to 150 rows of data and five columns of data.

If you wanted to view the first ten records of the data, you would use the following line of code:

print(dataset.head(10))

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

If you wanted to see the count of records for each label (flower type), you would use the following line of code:

print(dataset.groupby(‘class’).size())

All of these steps are a great way of validating that your program has fully read the data.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

At this point, you can delete or comment out the print command by adding a # as the first character in the line of code. Comments using # are a great way to make notes for yourself as you start your journey with Python.

D. Choosing a Machine Learning Model & Training It #

Choosing a Machine Learning Model #

Now that the program has been able to read our data, and we have verified that it is successfully reading all the elements of the data, it is time to choose a Machine Learning Model.

The Iris data follows a logical approach, where we can measure the length and width of a petal and a sepal, and based on that, we can tell what kind of an iris the flower is. As we can arrive at the answer by following a series of decisions, a Decision Tree model seems like the right choice for this algorithm.

We have already imported that library with the following line of code:

from sklearn import tree

Training a Machine Learning Model #

It is time now to split our data into Training data and Testing data. Let’s assume we wish to proceed with the standard approach of using 80% of our data as Training data.

As a first step, we need to specify the X values (Features) and Y values (Labels) for our program. We achieve this goal with the following lines of code:

array = dataset.values

X = array[:,0:4]

Y = array[:,4]

The ‘:’ used in the two lines above refers to using the slicing approach. A slice is a way for us to specify the starting index and the ending index in an array.

An array is a reference to a collection of items stored in the program’s memory. All the elements of an array must be of the same type. We already took care of the uniformity of data in our data set by maintaining all features in cm.

The line X = array[:,0:4] specifies the first three columns (0, 1,2, and 3) as the input variables assigned to X. Python starts counting with a 0. The last number, number 4, is only examined with an ‘up to? viewpoint but not taken into account for the variable X.

For Y, the label, we specify only column 4 in the array command.

Our code now should look like this:

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

As before, we can now validate if X and Y are picking up the correct values by using the print command.

When I use the command- print (X), I can see all of the numerical dimensions, and so should you.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

When I print (Y), I can see all the labels, and so should you.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

As before, you can now delete or comment out the print statement.

Our next goal is to split the data into 80% for Training and keep the remaining 20% for testing.

To do that we type in:

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.8)

At this point, we run our program to check if it generates any errors. As you can see in the screenshot below, it reported no errors.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

By adding ‘my_classifier = tree. DecisionTreeClassifier()? we have named our classifier. Once again, we run our program, and we can see that it did not generate any errors.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

It is now time to train our algorithm on the test data.

We do this by invoking the fit command and typing in:

my_classifier.fit (X_train, Y_train)

This line feeds in the training data to the algorithm. Once again, we run the program and do not see any errors:

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

Now that we have a trained algorithm, it is time to test it. To trigger the testing, we invoke the predictions command by typing in the following:

predictions = my_classifier.predict(X_test)

Once again, we run the program to check for any errors and use the print command to see if we get any output.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

As expected of the algorithm, we did not get errors, and all of our predictions are the names of flowers.

E. Verifying the Accuracy of Our Algorithm #

Scikit has a measurement module that provides an accuracy score of the algorithm that we created and tested. To output the accuracy score, we type in the following:

from sklearn.metrics import accuracy_score

print accuracy_score(Y_test, predictions)

When these lines get added to the program and run it, you can see that the algorithm had an accuracy score of over 96%.

Diagram: Diagram

Original diagram for Techfastly by Vin Patel.

Congratulations!

Where do we go from here? #

We still have some unanswered questions like:

A. What if a decision tree is not the best choice for an algorithm? What do we do then?

B. How do we know that this model had the highest possible accuracy score? How would we test the other machine algorithm approaches?

C. What happens when our data set includes data or different types?

D. How can I tell whether some variables have a more direct relationship on the outcome than others?

These are some of the questions we will try to solve in the next post.

Related reading

More in Artificial intelligence