.. toctree::
   :hidden:

101 - Association Discovery usage
=================================

Following the schema described in the `prediction workflow <api_sketch.html>`_,
document, this is the code snippet that shows the minimal workflow to
create an association and produce association sets.

.. code-block:: python

    from bigml.api import BigML
    # step 0: creating a connection to the service (default credentials)
    api = BigML()
    # step 1: creating a source from the data in your local "data/groceries.csv" file
    source = api.create_source("data/groceries.csv")
    # waiting for the source to be finished. Results will be stored in `source`
    api.ok(source)
    # step 3: creating a dataset from the previously created `source`
    dataset = api.create_dataset(source)
    # waiting for the dataset to be finished
    api.ok(dataset)
    # step 5: creating an association
    association = api.create_association(dataset)
    # waiting for the association to be finished
    api.ok(association)
    # the new input data to predict for
    input_data = {"Products": "Fruit, Wine"}
    # creating a single association set
    association_set = api.create_association_set(association, input_data)

In the previous code, the `api.ok <creating_resources.html>`_
method is used to wait for the resource
to be finished before calling the next create method
or accessing the resource properties.
In the first case, we could skip that `api.ok`call because the next
`create` method would internally do the waiting when needed.

You can also create association sets locally using the `Association`
class in the `association` module. A simple example of that is:

.. code-block:: python

    from bigml.association import Association
    local_association = Association("association/5968ec46983efc21b000001b")
    # association set for some input data
    local_association.association_set({"Products": "Fruit, Wine"})

Or you could store first your association information in a file and use that
file to create the local `Association` object:

.. code-block:: python

    # downloading the association JSON to a local file
    from bigml.api import BigML
    api = BigML()
    api.export("association/5968ec46983efc21b000001b",
               filename="my_association.json")
    # creating the association from the file
    from bigml.association import Association
    local_association = Association("my_association.json")
    # association set for some input data
    local_association.association_set({"Products": "Fruit, Wine"})


Every modeling resource in BigML has its corresponding local class. Check
the `Local resources <index.html#local-resources>`_ section of the
documentation to learn more about them.
