Back to Mindsdb

Get a Single Prediction

docs/mindsdb_sql/sql/get-single-prediction.mdx

26.1.04.8 KB
Original Source

Description

The SELECT statement fetches predictions from the model table. The data is returned on the fly and the result set is not persisted.

But there are ways to save predictions data! You can save your predictions as a view using the CREATE VIEW statement. Please note that a view is a saved query and does not store data like a table. Another way is to create a table using the CREATE TABLE statement or insert your predictions into an existing table using the INSERT INTO statement.

Syntax

Here is the syntax for fetching a single prediction from the model table:

sql
SELECT target_name, target_name_explain
FROM mindsdb.predictor_name
WHERE column_name = value 
AND column_name = value;
<Warning> **Grammar Matters**
Here are some points to keep in mind while writing queries in MindsDB:

&nbsp;&nbsp;&nbsp;1. The `column_name = value` pairs may be joined by `AND` or `OR` keywords.

&nbsp;&nbsp;&nbsp;2. Do not use any quotations for numerical values.

&nbsp;&nbsp;&nbsp;3. Use single quotes for strings.
&nbsp;&nbsp;&nbsp;4. The tables and column names are case sensitive.
</Warning>

On execution, we get:

sql
+-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| target_name       | target_name_explain                                                                                                                           |
+-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| predicted_value   | {"predicted_value": 4394, "confidence": 0.99, "anomaly": null, "truth": null, "confidence_lower_bound": 4313, "confidence_upper_bound": 4475} |
+-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+

Where:

NameDescription
target_nameName of the column to be predicted.
target_name_explainObject of the JSON type that contains the predicted_value and additional information such as confidence, anomaly, truth, confidence_lower_bound, confidence_upper_bound.
predictor_nameName of the model used to make the prediction.
WHERE column_name = value AND ...WHERE clause used to pass the data values for which the prediction is made.

Example

Let's predict the rental_price value using the home_rentals_model model for the property having sqft=823, location='good', neighborhood='downtown', and days_on_market=10.

sql
SELECT sqft, location, neighborhood, days_on_market, rental_price, rental_price_explain
FROM mindsdb.home_rentals_model1
WHERE sqft=823
AND location='good'
AND neighborhood='downtown'
AND days_on_market=10;

On execution, we get:

sql
+-------+----------+--------------+----------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| sqft  | location | neighborhood | days_on_market | rental_price | rental_price_explain                                                                                                                          |
+-------+----------+--------------+----------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| 823   | good     | downtown     | 10             | 4394         | {"predicted_value": 4394, "confidence": 0.99, "anomaly": null, "truth": null, "confidence_lower_bound": 4313, "confidence_upper_bound": 4475} |
+-------+----------+--------------+----------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------+