How to work with scikit learn using keras

Table of Contents

  1. Wrappers for Sklearn API
  2. Code Example
  3. Video Tutorial

1 Wrappers for Sklearn API:

  1. In this blog, we are going to learn about Keras wrappers for the sklearn API.
  2. We use them in our deep neural network analysis. We already know Keras is a popular library for deep learning, but the focus of the library is only on deep learning. On the other hand, the sklearn Library in Python is used for efficient numerical computation. 
  3. It is a fully-featured library for general machine learning and provides many utilities that are useful in the development of deep learning modules. 
  4. Keras library provides wrappers for deep learning modules to be used as classification or regulation estimators inside could learn. 
  5. These wrappers are available in Keras. Keras model, which will then be used to feed and predict can takes both model parameters and fitting parameters.
  6. It also accepts parameters for calling fit, predicts, predict_proba and score methods. 

Code Example

  1. Let’s see how we can implement this in Jupyter 
  2. First, we import sequential model API from Keras in this demonstration, we only use dense layers, so we have to import them from Keras. 
Figure-1
  1. This Keras classifier is used for the classification problems, which is available in sklearn. 
  2. We will also import stratified fold and cross Välidation from SkLearn.
  3. load_text from numpy will help us to load the data. we load our diabetes dataset, which is available in our working directory. Our dataset contains seven hundred sixty-eight samples of nine features. 
Figure-2
  1. We split our dataset into inputs with eight features and the final column as output. 
  2. So our input data contains seven hundred sixty-eight samples of eight features, so it will be our input dimension in the model. 
  3. Now we create a function that will generate and compile our model, this function will be used as an argument of Carus classifier. We generate a simple sequential model with only dense layers. First, the hidden layer contains four neurons with the activation function value. 
Figure-3
  1. We have to mention our input dimension in this first layer. The final output layer contains only one node as this is a binary classification problem. We use the sigmoid activation function, so the output will be in the range of zero and one.
  2. The functions will also compile our model as this is a classification problem. We use binary cross-entropy as the loss function. We are going to define Adam as an optimizer. We will also choose accuracy as a metric.
  3. We passed this function to Keras classifier by the building of an argument we set epochs as hundred, so at the time of model training, it will iterate a hundred times. We said Betsi as hundred as we have seven hundred sixty-eight samples, there will be eight batches of hundred samples each.
  4. These verbal zero will run it silently. These are automatically bundled up and passed on the feed function, which is called internally by the Keras classifier. 
  5. Then we use sklearn stratified fold, to perform stratified cross-validation. This is a resampling technique that we can provide a robust estimate of the performance of a machine learning model on unseen data.
Figure-4

Finally, we use the skLearn Function Cross_val_ score to evaluate our model using the cross-validation scheme. So our model is around 67.84 percent accurate.

Figure-5

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *