Skip to content

Functions

/

ML functions

ML functions

These functions can be used when calculating outputs from a trained machine learning model that has been uploaded to the database.

Function

Description

ml::name-of-model<version>()

Computes a value from a trained machine learning model

Once a model has been uploaded to the database, it can be called with inputs to produce a calculation from the trained ML model. A basic raw computation uses the following call:

API DEFINITION
ml::house-price-prediction<0.0.1>(500.0, 1.0);

In the above example, the model being called is house-price-prediction with the version 0.0.1, and it is passed a raw vector of [ [500.0, 1.0] ]. The name, version and inputs vary from model to model. The name and version of the model are defined in the .surml file that is uploaded to the database. A "buffered compute" can also be performed with the code below:

API DEFINITION
ml::house-price-prediction<0.0.1>({squarefoot: 500.0, num_floors: 1.0});

Here, the key mappings in the header of the uploaded .surml file are used to map the fields defined in the object passed into the ml:: function in the correct order. If there are any normalisation parameters in the header of the .surml file, these will also be applied.

The following example shows this function, and its output:

ml::house-price-prediction<0.0.1>({squarefoot: 500.0, num_floors: 1.0});

250000

As ML is integrated into SurrealQL, the ml:: function can be used to infer entire fields. The following example of house prices begins by defining a basic table:

CREATE house_listing SET squarefoot_col = 500.0, num_floors_col = 1.0;
CREATE house_listing SET squarefoot_col = 1000.0, num_floors_col = 2.0;
CREATE house_listing SET squarefoot_col = 1500.0, num_floors_col = 3.0;

The following query then returns all the records with the imputed price prediction:

SELECT 
	*, 
	ml::house-price-prediction<0.0.1>({ squarefoot: squarefoot_col, num_floors: num_floors_col }) AS price_prediction 
FROM house_listing;

This statement gives the following result:

[
	{
		"id": "house_listing:7bo0f35tl4hpx5bymq5d",
		"num_floors_col": 3,
		"price_prediction": 406534.75,
		"squarefoot_col": 1500
	},
	{
		"id": "house_listing:8k2ttvhp2vh8v7skwyie",
		"num_floors_col": 2,
		"price_prediction": 291870.5,
		"squarefoot_col": 1000
	},
	{
		"id": "house_listing:vnlv3nzr21oi5o23kydw",
		"num_floors_col": 1,
		"price_prediction": 177206.21875,
		"squarefoot_col": 500
	}
]

We can see that our price prediction is calculated in the query. We can build on the previous surql to filter based on the computed price prediction with the surql below:

SELECT * FROM (
		SELECT 
			*, 
			ml::house-price-prediction<0.0.1>({ squarefoot: squarefoot_col, num_floors: num_floors_col }) AS price_prediction 
		FROM house_listing
	) 
	WHERE price_prediction > 177206.21875;

This gives us the following result:

[
	{
		"id": "house_listing:7bo0f35tl4hpx5bymq5d",
		"num_floors_col": 3,
		"price_prediction": 406534.75,
		"squarefoot_col": 1500
	},
	{
		"id": "house_listing:8k2ttvhp2vh8v7skwyie",
		"num_floors_col": 2,
		"price_prediction": 291870.5,
		"squarefoot_col": 1000
	}
]



Was this page helpful?