Skip to main content

Classifying Flowers Part 1: Data

Everybody loves plants. So we'll continue our descent by building a neural network that can do something with plants. This construct will be able to take characteristics that describe a plant and, if we're good enough, hopefully the neural network will be able to tell us with accuracy what type of plant it is.

To do this, we'll use the Iris flowers dataset, which contains 150 samples of 3 different species (50 samples each). The species in this dataset are Iris setosa, Iris versicolor, and Iris virginica.

The dataset can be found at the following links.
Data: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
ReadME: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.names

Here's a screenshot of the data we'll be working with:

Initially, this data doesn't look too exciting and if it at all confuses you, you're perfectly normal. But we can figure some immediate information just from looking at the format of this data. Firstly, the data is given in rows of comma-delimited lists. There appears to be 4 decimal values followed by a single string value in each row. Thus there are 5 values in each row.

We can find even more information about the data by looking at the ReadME.

Bullet point 7 in the ReadME gives some information regarding what the different values in each row mean. The columns are:

  1. sepal length (cm)
  2. sepal width (cm)
  3. petal length (cm)
  4. petal width (cm)
  5. species
So we can consider each row in the given data to be a specific plant, and for each plant, we are given some characteristics that describe it (i.e. sepal and petal measurements) as well as what type of plant it is (i.e. the species). 

Note: for those of you that don't know what a sepal is, here's an anatomically perfect depiction of the labelled parts of a flower: 

So cool, we have data with a bunch of instances of plants and we can try to throw deep learning at it to see if we can train a neural network to correctly classify them. I'm going to save the data at C:\Blog\iris.data.csv (the link tries to coerce you to saving the data as iris.data.txt so be sure to change the file's extension to .csv) and we'll try to do everything in R, which we installed in Blog Post 2. 

We'll open up RStudio and in the console, use the following commands to read the data into a dataframe, which is like a table in R that we can manipulate:


Please note that if you don't understand what all this R code means, that's okay. I'll be including comments to detail every action that's taken so people that are either learning R or don't really care to dip their hands in the statisticians' swimming pool can follow along. 

After loading the data into an R DataFrame, we'll have to partition it into training and test sets. So it's probably a good time to develop an understanding of training and test sets. 

When you want to train any predictive model (thing that makes predictions), you'll first throw examples at it and somehow it learns. We won't go into the details of how learning occurs because it's too math-y for this blog but, as I said in the first post, there are millions of resources on Google that'll spare no math and that give a thorough introduction to the procedure. Here's one that I hold dearly which explains training neural networks in the context of image classification: http://cs231n.github.io/optimization-1/

After training your predictive model, your model should be awesome at predicting all the different examples given to it during training because it's seen those examples countless times. But a good predictive model shouldn't just be able to predict the examples it trained on. It should be able to predict new data that it has never seen before. If our model is good at predicting training data but bad at predicting new data, then we can say that the model has overfit to the noise inherent in the training data. There's a whole area of ongoing research that studies ways to prevent overfitting, so it's a difficult problem for everyone and likely won't be solved in this blog.

To see how the model performs on data it has never seen before, we use a testing set. This set is kept in a box while training the model. Once the model is trained, we see how it does well on the training set, which it has never seen before, and we cross our fingers in hopes that the model generalizes well (i.e. performs well on the testing set).

So we'll need to partition our data into training and testing sets. Here's an image that I took from here that shows how we'll partition our data. 

We already have our data loaded into a DataFrame in R, so we'll put 80% of the data into a training set and the remaining 20% of the data into a testing set: 
Since the initial dataset contained 150 plants, our training set contains 120 plants and our testing set contains 30 plants. 

At this point, we're ready to build a neural network to classify these plants. But this post has been a long read and if you're new to the subject, you'll need time to digest everything. So I'll stop the post here and in the next post, we'll continue our descent by assembling a neural network and training it so that it can correctly tell us the species of a flower given only the petal and sepal measurements. 

Comments

Popular posts from this blog

Classifying Flowers Part 2: Training and Evaluation

In the previous post, we fiddled with data and set out for an ambitious task of classifying plants. We ended that post after having downloaded, prepared, and partitioned our data, resulting two non-overlapping sets of data: a test set and a training set. We'll continue now by building, training, and evaluating a neural network to classify these flowers. First, we'll need to separate the values that we'll use as input to the network from the values we're intending to use as the output from the network. In other words, we need to place all the sepal and petal widths and lengths into a bag that the network can read, and all the species corresponding to the individual sets of sepal and petal widths and lengths into another bag. That way, when the network trains, it only takes into account the 4 inputs. When it produces an output, we can use the other bag (the one with species inside) to see if the network has predicted the species correctly. Before doing that, we'...

Evaluating our Heart Disease Classifier

Let's continue our descent by evaluating how good our heart disease classifier is. We can do this by generating predictions on the test set and see how the predictions compare to the test set's ground truth labels. That can be done with the following lines of code: At the bottom we can see that the classifier predicted 24 true negatives, 9 false positives, 8 false negatives, and 19 true positives. That's pretty okay. There is obviously some inaccuracy in the predictions, but let's calculate the accuracy anyways. (24+19)/(24+19+8+9)=71.6. So the test accuracy was 71.6, while, if you recall from the last post, the training accuracy was nearing 90%. This disparity between training and testing accuracy is a result of overfitting. Essentially, 50,000 training loops was too much training for this little of data. The resulting network overfit to the noise inherent in the training data and, as a result, failed to generalize as well on the test set. Therefore, the testing...