Now Reading
Complete Guide To QLattice: A New And Transparent Machine Learning Framework

Complete Guide To QLattice: A New And Transparent Machine Learning Framework

Bhoomika Madhukar
W3Schools

Machine learning techniques and algorithms have time and again proven to be really effective over the past few years and there are more breakthroughs that will come in the near future. But most of the algorithms at hand use a lot of statistical methods and require a lot of human intervention. To reduce the amount of human interaction, a new machine learning algorithm was developed called QLattice. 

In this article, we will learn about the following in detail:-

  1. Introduction to Qlattice
  2. How to implement Qlattice

Introduction to QLattice

Qlattice has its advantages over neural networks and decision trees. It removes the concept of black box seen in neural networks and serves explainability similar to that of a decision tree. It was developed by a research startup named abzu earlier this year. 



Qlattice works by searching through thousands of potential models and finds one with the right features and interactions to fit as the perfect model for the problem. 

Initially, the user has to set a few parameters like the input features, target and the independent variables. QLattice works with supervised learning and the input features and target are called registers. These registers form the entry and exit points for the QLattice model. Once the registers are set, a collection of possible models can be extracted from the QLattice. This collection of models corresponds to something called QGraphs. These graphs are a set of nodes and edges where the nodes have activation functions and edges carry weights. This indicates that upon training a Qgraph, it extracts essential information from the features to predict the target. 

Qlattice

Implementation of QLattice

Let us understand the working of QLattice better by implementing a simple classification model. Before we begin, please make sure you have created an account in the abzu website. You will receive access to it and be directed to the dashboard. 

qlattice

Here, you can either use the playground they provide or use Colab. I have made use of Colab. 

For implementing this we need the Feyn library which is the software development kit that we use to interact with the QLattice.

To install the Feyn library use the command

pip install feyn

The dataset

To keep things simple I have used the diabetes dataset which is downloaded from Kaggle. Let us load the dataset and import the needed libraries

from google.colab import drive
import feyn
import numpy as np
import pandas as pd
drive.mount('/content/gdrive/')
dataset=pd.read_csv('/content/gdrive/My Drive/diabetes/diabetes.csv')
dataset.head()

 Splitting the dataset 

Before we split the dataset let us assign our target value to a variable named target. Next, we will split the dataset into train and test set.

target='Outcome'
train, test = train_test_split(dataset, test_size=0.4, random_state=42, stratify=dataset[target])

Qgraph

Qgraphs are produced by Qlattice which forms a graphical structure that connects the input to output. We need to give instructions about the features of this. To be able to access this since it belongs to the third party we need a URL and authorization token. This is made available in your abzu dashboard. 

qlattice = feyn.QLattice(url="the url", api_token="token")

Note here in the qgraph we are just giving the basic information and not specifying which algorithm to use. 

See Also
fake news classification

qgraph = qlattice.get_classifier(train.columns, target, max_depth=3)
qgraph.head(2)
qgraph

As you see for each feature automatically the nodes with algorithms and activation function are assigned. Green boxes represent the inputs and outputs, white boxed with pink border represent interactions. Interactions take in input as values, builds a function for that value and predicts the outcome. It is similar to the neural network but we can see exactly what is happening in each column. Here the classification algorithm seems to be gaussian. 

Training

Let us begin to train the qgraph over 5 fittings. Here epochs are called fittings. The code below shows the training loop.

depth = 5
loss_function = feyn.losses.squared_error
nloops = 5
for loop in range(nloops):
    qgraph.fit(train, loss_function=loss_function, threads=4,show='graph')
    qlattice.update(qgraph.best())
qgraph

Below, it shows the best loss value after 5 fittings, here the loss value is 0.152 which is a good loss value. 

Once training is done it is time to move to predictions. 

Predictions

output_graph = qgraph.sort(train)[0]
df_pred = valid.copy()
df_pred['predicted'] = output_graph.predict(valid) 
threshold = 0.5
y_pred = np.where(df_pred.predicted > threshold, True, False)
y_real = df_pred[target].astype(bool)
plt.figure(figsize=(8, 5))
feyn.plots.plot_confusion_matrix(y_real, y_pred)
confusion matrix
train_pred = output_graph.predict(train)
print('Overall training accuracy: %.4f' %np.mean(np.round(train_pred) == train[target]))
print('Overall validation accuracy: %.4f' %np.mean(y_pred == y_real))

The above code and confusion matrix shows that the model performs well because we have not performed any pre-processing and not specified the type of algorithm. Qgraph learnt and trained on only the best model for the dataset. 

Conclusion

In the article, we learnt about the new machine learning model called Qlattice and implemented it on a simple classification dataset and got good results. 

What Do You Think?

If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top