Train with Python

Python is widely used in Machine Learning field. Use it to train your model as you are used to and use it right on your database with pgpyml.

Get Started
                        from sklearn.tree import DecisionTreeClassifier
from joblib import dump
    
# some code to load your data...
    
# create the model
model = DecisionTreeClassifier()
    
# train your model
model.fit(X_train, y_train)
    
# Save the trained model
dump(model, '~/models/my_model.joblib')
                    

Deploy on PostgreSQL

Use your Machine Learning model inside your database. Deploy it directly on PostgreSQL, where your data is stored.

Deploy
                        SELECT * FROM predict(
    '~/models/my_model.joblib', 
    '{{5.2,3.5,1.5,0.2}}'
);

SELECT * FROM predict_table_row(
    '~/models/my_model.joblib', 
    'iris',
    '{"sepal_length", "sepal_width", 
    "petal_length", "petal_width"}', 
    (SELECT MAX(id) FROM iris)
);
    

Classify New Data

Use triggers to classify new data using your model, populate columns or avoid undesired insertions right on PostgreSQL.

Read More
                        CREATE TRIGGER classify_iris
BEFORE INSERT OR UPDATE ON "iris"
FOR EACH ROW 
EXECUTE PROCEDURE classification_trigger(
    '~/models/my_model.joblib', -- Model path
    'class', -- Column name to save the result
    'sepal_length', -- Feature 1
    'sepal_width', -- Feature 2
    'petal_length', -- Feature 3
    'petal_width'-- Feature 4
);