Model.to_sql

In [ ]:
Model.to_sql(X: list = [],
             return_proba: bool = False,)

Returns the SQL code needed to deploy the model without using Vertica built-in functions.

Parameters

Name Type Optional Description
X
list
input predictors name.
return_proba
bool
If set to True and the model is a classifier, the function will return the class probabilities.

Returns

str / list : SQL code

Example

In [18]:
from verticapy.learn.ensemble import XGBoostClassifier

model = XGBoostClassifier("xgb_titanic", max_ntree = 2, max_depth = 2)
model.fit("public.titanic", 
          ["age", "fare", "pclass",],
          "survived")

# Returns the string needed to deploy the model's prediction in Standard SQL
print(model.to_sql())
(CASE WHEN (1 / (1 + EXP(- (-0.43652089804547567 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN -0.325084 ELSE 1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN 0.556098 ELSE -0.615402 END)))))) / ((1 / (1 + EXP(- (0.4365208980454757 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN 0.325084 ELSE -1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN -0.556098 ELSE 0.615402 END)))))) + (1 / (1 + EXP(- (-0.43652089804547567 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN -0.325084 ELSE 1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN 0.556098 ELSE -0.615402 END))))))) > 0.5 THEN 1 ELSE 0 END)
In [19]:
# Returns the list of strings needed to deploy the model's probability in Standard SQL
print(model.to_sql(return_proba = True))
['(1 / (1 + EXP(- (0.4365208980454757 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN 0.325084 ELSE -1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN -0.556098 ELSE 0.615402 END)))))) / ((1 / (1 + EXP(- (0.4365208980454757 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN 0.325084 ELSE -1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN -0.556098 ELSE 0.615402 END)))))) + (1 / (1 + EXP(- (-0.43652089804547567 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN -0.325084 ELSE 1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN 0.556098 ELSE -0.615402 END)))))))', '(1 / (1 + EXP(- (-0.43652089804547567 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN -0.325084 ELSE 1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN 0.556098 ELSE -0.615402 END)))))) / ((1 / (1 + EXP(- (0.4365208980454757 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN 0.325084 ELSE -1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN -0.556098 ELSE 0.615402 END)))))) + (1 / (1 + EXP(- (-0.43652089804547567 + 0.1 * ((CASE WHEN "fare" < 48.030862 THEN -0.325084 ELSE 1.2022 END) + (CASE WHEN "pclass" < 2.0625 THEN 0.556098 ELSE -0.615402 END)))))))']
In [15]:
from verticapy.learn.ensemble import XGBoostRegressor

model = XGBoostRegressor("xgb_titanic", max_ntree = 2, max_depth = 2)
model.fit("public.titanic", 
          ["age", "fare", "pclass",],
          "survived")

# Returns the string needed to deploy the model's prediction in Standard SQL
print(model.to_sql())
((CASE WHEN "fare" < 48.030862 THEN -0.077519 ELSE 0.286675 END) + (CASE WHEN "pclass" < 2.0625 THEN 0.133376 ELSE -0.145705 END)) * 0.1 + 0.392570281124498
In [16]:
from verticapy.learn.cluster import KMeans

model = KMeans("kmeans_iris", n_cluster = 2)
model.fit("public.iris",)

# Returns the string needed to deploy the model's estimated centers in Standard SQL
print(model.to_sql())
CASE WHEN "SepalLengthCm" IS NULL OR "SepalWidthCm" IS NULL OR "PetalLengthCm" IS NULL OR "PetalWidthCm" IS NULL THEN NULL WHEN POWER(POWER("SepalLengthCm" - 5.00566037735849, 2) + POWER("SepalWidthCm" - 3.36037735849057, 2) + POWER("PetalLengthCm" - 1.5622641509434, 2) + POWER("PetalWidthCm" - 0.288679245283019, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 6.30103092783505, 2) + POWER("SepalWidthCm" - 2.88659793814433, 2) + POWER("PetalLengthCm" - 4.95876288659794, 2) + POWER("PetalWidthCm" - 1.69587628865979, 2), 1 / 2) THEN 1 ELSE 0 END
In [17]:
from verticapy.learn.preprocessing import Normalizer

model = Normalizer("norm_titanic",)
model.fit("public.titanic",)

# Returns the list of strings needed to deploy the model's transformation in Standard SQL
print(model.to_sql())
['("pclass" - 2.28444084278768) / 0.842485636190292', '("survived" - 0.364667747163695) / 0.481532018641288', '("age" - 30.1524573721163) / 14.4353046299159', '("sibsp" - 0.504051863857374) / 1.04111727241629', '("parch" - 0.378444084278768) / 0.868604707790392', '("fare" - 33.9637936739659) / 52.6460729831293', '("body" - 164.14406779661) / 96.5760207557808']