Neural-Network-based Iris Flower Classification
0 Dataset Description
Description Source: https://archive.ics.uci.edu/ml/datasets/Iris/
Data Set Information:
This is perhaps the best known database to be found in the pattern recognition literature. Fisher’s paper is a classic in the field and is referenced frequently to this day. (See Duda & Hart, for example.) The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is linearly separable from the other 2; the latter are NOT linearly separable from each other.
Predicted attribute: class of iris plant.
This is an exceedingly simple domain.
Attribute Information:
column 1: sepal length in cm
column 2: sepal width in cm
column 3: petal length in cm
column 4: petal width in cm
column 5: class:
– Iris Setosa
– Iris Versicolour
– Iris Virginica
1 Observe the data
Load dataset from csv file.
1 | %matplotlib inline |
Observe the format of data. (Take 10 examples randomly)
1 | h =set() |
1 | ID SEPAL_LENGTH SEPAL_WIDTH PETAL_LENGTH PETAL_WIDTH || CLASS |
Plot dataset.
1 | plt.figure(figsize=(12,5)) |
2 Preprocess dataset and Divide dataset
Through observing the figure above, we can find that it’s easier to classify iris flowers by petal_width
and petal_length
. So, we can simplify the problem by ignoring sepal_width
and sepal_length
. (This kind of operation may decrease accuracy)
1 | #put the petal values into array |
1 | num = len(iris_class) |
Encode the classes into one-hot encoding.
For example,
0→[1,0,0]
1→[0,1,0]
2→[0,0,1]
1 | # One-Hot Encoding |
Let’s observe the trainset and testset in different marker.
1 | #put the trainset and testset in one graph to compare |
3 Describe your neural network
1 | # describe the model |
1 | Model: "sequential_1" |
We can use Mean Square Error (MSE) as loss
function, and use Stochastic Gradient Descent (SGD) as the process we train the model (i.e. “optimizer
“).
(PS: MSE is not so suitable for classification)
1 | model.compile(loss='mse', optimizer='sgd') |
4 Training
1 | model.fit(train_x, train_y_onehot, batch_size=4, epochs=100, verbose=1) |
5 Evaluation of the trained model
We can use .evaluate
to compute loss on testset.
1 | cost = model.evaluate(test_x, test_y_onehot) |
1 | scores = model.predict(test_x)#predict the next five value |
1 | results = np.array([]) |
Visualize our model of prediction.
1 | plt.figure(figsize=(15,5)) |