Differences between Python and Julia

Differences between Python and Julia

The IAI Python interface matches the Julia API very closely, so you can refer to the Julia documentation for information on most tasks. On this page we note the main differences between the Python and Julia interfaces.

Conversion of Julia data types to Python

In order to figure out the types to pass to an IAI function from the Python interface, you can refer to the equivalent function in the Julia API and translate the types to their Python equivalent. Most literal data types convert in a straighforward manner, for example:

The following Julia types can be passed as follows:

Object-oriented interface for learners

In the IAI Python interface, the API methods relating to learners are methods of the learner objects rather than functions that operate on learners as in the Julia interface. For instance the IAI.fit! method in Julia:

IAI.fit!(lnr, X, y)

would be called from the Python interface as

lnr.fit(X, y)

Interactive Visualizations

The write_html and show_in_browser functions work the same in Python as in Julia for saving visualizations to file or displaying in an external browser, respectively. Additionally, visualizations will be automatically shown in Jupyter notebooks as they are for Julia.

Below is an example that shows the equivalent Python code for the advanced visualization examples in Julia. In these examples we work with the following tree learner:

Optimal Trees Visualization

We can rename the features with a dict that maps from the original names to more descriptive names:

vis_renamed_features = iai.TreePlot(lnr, feature_renames={
    "Disp": "Displacement",
    "HP": "Horsepower",
    "WT": "Weight",
})
Optimal Trees Visualization

We can also add extra content for each node, such as summary statistics. We create a list of str with the content to show for each node and pass this as extra_content:

import numpy as np
node_inds = lnr.apply_nodes(X)
def get_text(inds):
    return ('<b>Mean horsepower in node:</b> ' +
            str(np.round(np.mean(X.HP.iloc[inds - 1]), decimals=2)))
extras = [get_text(inds) for inds in node_inds]
vis_extra_text = iai.TreePlot(lnr, extra_content=extras)
Optimal Trees Visualization

Finally, we can combine multiple learners in a single visualization as described in the Julia documentation. In Python, a question is a tuple of the form (question, responses), where question is the string prompt for the question and responses is a list of possible responses:

questions = ("Use learner with", [
    ("renamed features", vis_renamed_features),
    ("extra text output", vis_extra_text)
])
iai.MultiTreePlot(questions)
Optimal Trees Visualization