- Classification
- Prediction
This blog contains simple classification model for iris data set which is a sample data set that used to evaluate data analyzing related concepts around the world. these are the steps we are doing in order to create a classifier.
- Loading the dataset.
- Encode The Output Variable
- Define The Neural Network Model.
- Evaluate The Model.
- Making some predictions.
First You need to install some softwears by pip installer
- You have Python 2 or 3 installed and configured.
- You have SciPy (including NumPy) installed and configured.
- You have Keras and a backend (TensorFlow) installed and configured.
So we begin to build our model. Before that we need to import the required libraries
Next, we need to initialize the random number generator to a constant value (7). This is important to ensure that the results we achieve from this model can be achieved again precisely. It ensures that the stochastic process of training a neural network model can be reproduced.
1. Loading the dataset
The dataset can be loaded directly. Because the output variable contains strings, it is easiest to load the data using pandas. We can then split the attributes (columns) into input variables (X) and output variables (Y).
2.Encode The Output Variable
The output variable contains three different string values.
Iris-setosaWhen modeling multi-class classification problems using neural networks, it is good practice to reshape the output attribute from a vector that contains values for each class value to be a matrix with a boolean for each class value and whether or not a given instance has that class value or not.
Iris-versicolor
Iris-virginica
This is called one hot encoding or creating dummy variables from a categorical variable. For example, in this problem three class values are Iris-setosa, Iris-versicolor and Iris-virginica.
We can turn this into a one-hot encoded binary matrix for each data instance that would look as follows:
Iris-setosa 100We can do this by first encoding the strings consistently to integers using the scikit-learn class LabelEncoder. Then convert the vector of integers to a one hot encoding using the Keras function to_categorical().
Iris-versicolor 010
Iris-virginica 001
3.Define The Neural Network ModelThere is a KerasClassifier class in Keras that can be used as an Estimator in scikit-learn, the base type of model in the library. The KerasClassifier takes the name of a function as an argument. This function must return the constructed neural network model, ready for training. Below is a function that will create a baseline neural network for the iris classification problem. It creates a simple fully connected network with one hidden layer that contains 8 neurons. The hidden layer uses a rectifier activation function which is a good practice. Because we used a one-hot encoding for our iris dataset, the output layer must create 3 output values, one for each class. The output value with the largest value will be taken as the class predicted by the model. The network topology of this simple one-layer neural network can be summarized as:
4 inputs -> [16 hidden nodes] -> 3 outputsNote that we use a “softmax” activation function in the output layer. This is to ensure the output values are in the range of 0 and 1 and may be used as predicted probabilities. Finally, the network uses the efficient Adam gradient descent optimization algorithm with a logarithmic loss function, which is called “categorical_crossentropy” in Keras.
We can also pass arguments in the construction of the KerasClassifier class that will be passed on to the fit() function internally used to train the neural network. Here, we pass the number of epochs as 200 and batch size as 5 to use when training the model. Debugging is also turned off when training by setting verbose to 0.
4.Evaluate The Model.
We can now evaluate the neural network model on our training data using k-Fold Cross Validation.First we can define the model evaluation procedure. Here, we set the number of folds to be 10 (an excellent default) and to shuffle the data before partitioning it. Now we can evaluate our model (estimator) on our dataset (X and dummy_y) using a 10-fold cross-validation procedure (kfold).
Evaluating the model only takes approximately 10 seconds and returns an object that describes the evaluation of the 10 constructed models for each of the splits of the dataset.
Accuracy: 97.33% (4.42%)Conclusion
In this post you discovered how to develop and evaluate a neural network using the Keras Python library for deep learning. By completing this tutorial, you learned:
- How to load data and make it available to Keras.
- How to prepare multi-class classification data for modeling using one hot encoding.
- How to use Keras neural network models with scikit-learn.
- How to define a neural network using Keras for multi-class classification.
- How to evaluate a Keras neural network model using scikit-learn with k-fold cross validation