Train a model from the MongoDB API

Train model from mongodb

Note: This is work in progress, please join our slack channel if you have any questions.

Train new model

To train a new model, you will need to insertOne() a new document inside the mindsdb.models collection.

The object sent to the insertOne() for training the new model should contain:

  • name (string) — The name of the model.
  • predict (string) — The feature you want to predict. To predict multiple features, include a list of features.
  • connection(string) — The connection string for connecting to MongoDB. If you have used GUI to connect to MongoDB, that connection will be used.
  • select_data_query (object) — The object that contains info about getting the data to train the model.
    • database(string) - The name of the database
    • collection(string) - The name of the collection
    • find(dict) - The dict that selects the documents from the collection, must be valid JSON format. Same as db.collection.find({…})
  • training_options (dict) — Optional value that contains additional training parameters. To train timeseries model you need to provide training_options.
db.models.insertOne({
    'name': str,
    'predict': str | list of fields,
    'connection': str,  # optional
    'select_data_query':{
    'database': str,
    'collection': str,
    'find': dict
    },
    'training_options': dict  # optional
})

For the timeseries model:

db.models.insertOne({
    'name': str,
    'predict': str | list of fields,
    'connection': str,  # optional
    'select_data_query':{
    'database': str,
    'collection': str,
    'find': dict
    },
    'training_options': {
        "timeseries_settings": {
                "order_by": list of fields,
                "group_by": list of fields,    #optional
                "horizon": int,         #optional
                "use_previous_target": Boolean,
                "window": int
            }
    }
})

Train new model example

The following example shows you how to train a new model from a mongo client. The collection used for training the model is the Telcom Customer Churn dataset.

db.models.insertOne({
    'name': 'churn',
    'predict': 'Churn',
    'select_data_query':{
        'database': 'test_data',
        'collection': 'customer_churn',
        'find': {}
    }
})

Train model from mongo shell

This INSERT query will train a new model called churn that predicts the customer Churn value.

Model training status

To check that the training finished successfully, you can find() the model status inside mindsdb.models collection e.g.:

db.models.find();

Training model status

!!! Success “That’s it :tada: :trophy: :computer:” You have successfully trained a new model from a mongo shell. The next step is to get predictions by querying the model.

Delete model

To delete the model run remove function on models collection and send the name of the model to delete as:

db.models.remove({ name: "model_name" });

Query the model from MongoDB API

To get the predictions from the model, you will need to call find() method on the model collection and provide values for which you want to get prediction as an object:

db.model_name.find({ key: "value", key: "value" });

!!! Info “Note” The object provided to find() method must be valid JSON format.

Query example

The following example shows you how to query the model from a mongo client. The collection used for training the model is the Telcom Customer Churn dataset. MindsDB will predict the customer Churn value based on the object values sent to find() method.

db.churn.find({'PhoneService': 'Yes','InternetService': 'DSL', 'OnlineService': 'No','MonthlyCharges': 53.85, 'TotalCharges': 108.15, 'tenure': 2, 'PaperlessBilling': 'Yes'})

You should get a response from MindsDB similar to:

predicted_valueconfidenceinfo
Yes0.8Check JSON below
{
  "Churn": "Yes",
  "Churn_confidence": 0.8,
  "Churn_explain": {
    "class_distribution": {
      "No": 0.44513007027299717,
      "Yes": 0.5548699297270028
    },
    "predicted_value": "Yes",
    "confidence": 0.8,
    "prediction_quality": "very confident"
  }
}