<jupyter_start><jupyter_text>Using Iris Data Set with TensorFlow / Keras Iris is perhaps the best known dataset which originated from a 1936 research paper by a British statistician and biologist <NAME>. The dataset is often used for testing out machine learning algorithms and visualizations e.g. Scatter Plot. The data set contains 150 rows of data, each row contains the following data for each flower sample: sepal length, sepal width, petal length, petal width in centimeters., and the 3 types of flower species. References:- UCI Machine Learning Repository, https://archive.ics.uci.edu/ml/datasets/iris- Wikipedia, Iris flower Data Set, https://en.wikipedia.org/wiki/Iris_flower_data_set- TensorFlow, Iris tutorial, https://www.tensorflow.org/get_started/estimator We will be using [Keras](https://keras.io/) which is a high level neural networks API running on top of [TensorFlow](https://www.tensorflow.org/) for this problem sheet.  1. Use TensorFlow to create a modelCreate a model to predict the species of Iris from a flower’s sepal width, sepal length, petal width, and petal length.<jupyter_code>#importing python libraries
import numpy as np
import csv
import keras as kr

# Adapted from: https://github.com/salmanahmad4u/keras-iris/blob/master/iris_nn.py
# &
# Adapted from: https://github.com/emerging-technologies/keras-iris

#Load dataset from csv
# Open and read through file
iris = list(csv.reader(open('IRIS.csv')))[1:] # [1:] = ignore the first row and read the rest of the file

# We need to separate the data into 2 arrays, inputs and outputs.

# inputs contains sepal length, sepal width, petal length, petal width converted as floats
inputs = np.array(iris)[:,:4].astype(np.float) # [:,:4] = give us all the rows and the first 4 columns in rows

# outputs contains the 3 species as strings: setosa, versicolor and virginica
outputs = np.array(iris)[:,4] # [:,4] selects the last column (4th column) of all rows which is the species

# Converting the output strings to integers.
# np.unique returns the sorted unique elements of an array.
# return_inverse=True means also return the indices of the unique array.
outputs_vals, outputs_ints = np.unique(outputs, return_inverse=True)
# output_vals contains the 3 unique string values
# output_ints contains the indices

# The 1st string corresponds with the 1st interger in the array

# species are represented as integers, with 0 denoting setosa, 1 denoting versicolor and 2 denoting virginica

# Encoding the integers as binary categorical variables.
# basically creating a binary matrix 
# E.g. if output_ints is 0 then its encoded into 1,0,0 or if 1 then its 0,1,0 or if 2 then its 0,0,1
outputs_cats = kr.utils.to_categorical(outputs_ints)
# This means that if the output is:
# (1,0,0) = setosa
# (0,1,0) = versicolor
# (0,0,1) = virginica



# A model is understood as a sequence or a graph of standalone, 
# ..fully-configurable modules that can be plugged together with as little restrictions as possible.
# For more: https://keras.io/
# Creating model and a neural network
# model is used to organise layers
model = kr.models.Sequential() # using sequential model which is a linear stack of layers

# stacking 4 layers. 
# Add an initial layer with 4 input nodes and a hidden layer with 16 nodes/neurons.
# Applying the sigmoid activation function to the layer. Range 0 to 1.
model.add(kr.layers.Dense(16, input_shape=(4,), activation='sigmoid' ))


# Adding another layer, connected to the layer with 16 nodes/neurons, containing 3 output nodes 
# Using the softmax activation function here. Softmax calculates the probabilities distribution. Range 0 to 1.
model.add(kr.layers.Dense(3, activation='softmax'))<jupyter_output><empty_output><jupyter_text>2. Split the data into training and testingSplit the data set into a training set and a testing set.<jupyter_code># Split the input and output data sets into training and test subsets
inds = np.random.permutation(len(inputs)) # Shuffling the array.. randomly change order of the indicies

#Split the array into 2. first batch of indicies go into train and 2nd batch go into test
train_inds, test_inds = np.array_split(inds, 2)

# Organising the data into training and testing groups.
# inputs_train takes in the shuffled train_inds
# outputs_train takes in the shuffled train_inds in the encoded binary matrix array
inputs_train, outputs_train = inputs[train_inds], outputs_cats[train_inds]

# inputs_test takes in the shuffled test_inds
# outputs_test takes in the shuffled test_inds in the encoded binary matrix array
inputs_test,  outputs_test  = inputs[test_inds],  outputs_cats[test_inds]<jupyter_output><empty_output><jupyter_text>3. Train the modelUse the testing set to train your model.<jupyter_code># Configure the model for training.
# Uses the adam optimizer and categorical cross entropy as the loss function.
# using adam optimizer algorithm for gradient descent
# Loss function is the objective that the model will try to minimize
# Add in some extra metrics - accuracy being the only one.
model.compile(optimizer="adam",
              loss="categorical_crossentropy",
              metrics=["accuracy"])


# Fit the model using our training data.
# input_train = training data
# output_train = target data
# epochs means how many times to train
# batch_size is going to take one training example at a time
# verbose is used to log the model being trained
model.fit(inputs_train, outputs_train, epochs=100, batch_size=1, verbose=1)<jupyter_output>Epoch 1/100
75/75 [==============================] - 0s 5ms/step - loss: 1.3813 - acc: 0.2667
Epoch 2/100
75/75 [==============================] - 0s 1ms/step - loss: 1.1838 - acc: 0.3067
Epoch 3/100
75/75 [==============================] - 0s 1ms/step - loss: 1.0728 - acc: 0.3600
Epoch 4/100
75/75 [==============================] - 0s 1ms/step - loss: 1.0075 - acc: 0.6133
Epoch 5/100
75/75 [==============================] - 0s 2ms/step - loss: 0.9680 - acc: 0.6667
Epoch 6/100
75/75 [==============================] - 0s 2ms/step - loss: 0.9205 - acc: 0.6667
Epoch 7/100
75/75 [==============================] - 0s 2ms/step - loss: 0.8754 - acc: 0.6400
Epoch 8/100
75/75 [==============================] - 0s 1ms/step - loss: 0.8328 - acc: 0.7733
Epoch 9/100
75/75 [==============================] - 0s 1ms/step - loss: 0.7873 - acc: 0.8133
Epoch 10/100
75/75 [==============================] - 0s 1ms/step - loss: 0.7497 - acc: 0.7867
Epoch 11/100
75/75 [==============================] - 0s 1m[...]<jupyter_text>4. Test the modelUse the testing set to test your model, clearly calculating and displaying the error rate.<jupyter_code># Evaluate the model using the test data set.
# model.evaluate compare answers
# inputs_test = input data
# output_test = target (label) data
# verbose=1 means verbose mode 1 which is a progress bar
# Verbosity mode: 0 = silent, 1 = progress bar, 2 = one line per epoch.
loss, accuracy = model.evaluate(inputs_test, outputs_test, verbose=1)

# Output the accuracy of the model.
print("\n\nLoss: %6.4f\tAccuracy: %6.4f" % (loss, accuracy))<jupyter_output>75/75 [==============================] - 0s 361us/step


Loss: 0.1420	Accuracy: 0.9733<jupyter_text>Prediction<jupyter_code># Predict the class of a single flower.
# using model.predict
# numpy.around to evenly round given data
# numpy.expand_dims to expand the shape of an array
prediction = np.around(model.predict(np.expand_dims(inputs_test[0], axis=0))).astype(np.int)[0]


print("Actual: %s\tEstimated: %s" % (outputs_test[0].astype(np.int), prediction))
print("That means it's a %s" % outputs_vals[prediction.astype(np.bool)][0])
# Save the model to a file for later use. 
# faster to run next time
model.save("iris_nn.h5")
# Load the model again with: model = load_model("iris_nn.h5")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13325652841781874, 'fraction_numerical': 0.0409626216077829, 'mean_word_length': 4.678052325581396, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '240873', 'license': 'MIT', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2330, 'n_words': 935, 'path': 'TensorFlow_IrisDataSet.ipynb', 'repo_name': 'ianburkeixiv/TensorFlow', 'size': '7819'}