Feyn

Feyn

  • Tutorials
  • Guides
  • API Reference
  • FAQ

›Advanced

Getting Started

  • Quick start
  • Using Feyn
  • Installation
  • Transition to Feyn 3.0
  • What is a QLattice?
  • Community edition
  • Commercial use

Essentials

  • Auto Run
  • Visualise a model
  • Summary plot
  • Semantic types
  • Categorical features
  • Estimating priors
  • Model parameters
  • Predicting with a model
  • Saving and loading models
  • Filtering models
  • Seeding a QLattice
  • Privacy

Evaluate Regressors

  • Regression plot
  • Residuals plot

Evaluate Classifiers

  • ROC curve
  • Confusion matrix
  • Plot probability scores

Understand Your Models

  • Plot response
  • Plot response 2D
  • Model signal
  • Segmented loss
  • Interactive flow

Primitive Operations

  • Using the primitives
  • Updating priors
  • Sample models
  • Fitting models
  • Pruning models
  • Diverse models
  • Updating a QLattice
  • Validate data

Advanced

  • Converting a model to SymPy
  • Setting themes
  • Saving a graph as an image
  • Using the query language
  • Model complexity

Converting a model to SymPy

by: Kevin Broløs & Meera Machado
(Feyn version 3.0 or newer)

Feyn models are, in truth, mathematical expressions represented as directed graphs. We can use SymPy to convert these graph representations to symbolic mathematical expressions. Specifically, models can be converted to SymPy objects for further manipulation or processing: executing them as equations, printing them in LaTeX, or just implementing them in any environment following the equation.

The only limitation is that we don't currently support exporting categorical registers to an executable function, so for models with categories, you'll only have an expression for understanding purposes.

Let's first generate a dataset and initialize a QLattice

import feyn

import numpy as np
import pandas as pd

# Generate a dataset and put it into a dataframe
np.random.seed(42)
X = np.random.normal(size=100)
y = np.exp(X)

data = pd.DataFrame(X, columns=['X'])
data['y'] = y

# Instantiate a QLattice
ql = feyn.QLattice(random_seed=666)

Sample and fit models real quick

We run the QLattice for just 2 epochs, and convert the best model to a mathematical expression.

models = ql.auto_run(data, 'y', kind='regression', n_epochs=2)

# Take the best model
model = models[0]
model

example graph

The model maps the input XXX to the output yyy through an exponential function, just like the generated data.

Convert to SymPy

Let's see how the SymPy expression looks converted to LaTeX:

sympy_model = model.sympify(signif=3)

sympy_model.as_expr()

1.0e1.0X−0.00306\displaystyle 1.0e^{1.0X} - 0.003061.0e1.0X−0.00306

The parameter signif specifies the number of significant digits in the expression's coefficients.

Let's try it for classification

For the sake of simplicity, we create a classification task by setting a boundary on the yyy values.

# Boundary at the median
data['y'] = np.where(data['y'] > 0.88, 1, 0)

# Instantiate a new QLattice
ql = feyn.QLattice(random_seed=666)

# Run QLattice for classification
models = ql.auto_run(data, 'y', kind='classification', n_epochs=2)

# Best model
model = models[0]
model

example graph

Let's see how the SymPy expression looks converted to LaTeX:

sympy_model = model.sympify(symbolic_lr=True, include_weights=False)

sympy_model.as_expr()

11+e−tanh⁡(X+eX)\displaystyle \frac{1}{1 + e^{-\tanh{(X + e^X)}}}1+e−tanh(X+eX)1​

In a classification problem, the mathematical expression in wrapped in a logistic function, as clearly seen above. Setting the parameter symbolic_lr to True makes the logistic function explicit. In addition, by setting include_weights to False the coefficients are not displayed, which can make the mathematical expression cleaner.

What to do with the SymPy object

You can check out their documentation on how to use it if you don't already know how. It works automatically by pretty printing in unicode terminals and IPython environments.

You can also use this for portability of final Feyn graphs, as you don't need the Python runtime to execute a simple mathematical equation. So you can take the output of these functions and port to R, STATA, JavaScript, or even Excel if you want to.

← Validate dataSetting themes →
  • Let's first generate a dataset and initialize a QLattice
  • Sample and fit models real quick
  • Convert to SymPy
  • Let's try it for classification
  • What to do with the SymPy object
Copyright © 2023 Abzu.ai
Feyn®, QGraph®, and the QLattice® are registered trademarks of Abzu®