B4J Tutorial [PyBridge] Linear regression with scikit learn

This example is based on: https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html
The steps are:

1. Read the CSV file with StringUtils.
2. Take the BMI and target columns.
3. Split the data to train and test sets.
4. Train the model with a call to LinearRegression.fit.
5. Evaluate the model using the test data set.
6. Plot it using MatplotLib.

I also added a step where the plot is fetched back to B4J and displayed with a B4XImageView. You can instead call plot.run("show") to show the standard plot window (which has advantages over a static image).

1739802196212.png

Note that the regression will work better with all inputs, instead of just the BMI. You can test it by adding all fields to "BMI":
Code:
For Each row() As String In data
    Dim x As List
    x.Initialize
    For i = 0 To row.Length - 2 'the last column is the target
        x.Add(row(i).As(Double))
    Next
    bmi.Add(x) 'the X input is expected to be a list of arrays.
    target.Add(row(10).As(Double))
Next
And skip the plotting code.

Don't miss the dependencies line at the top.
 

Attachments

The Objects/python folder is not included in the zip file. Is it intended that we set this up after unzipping? Or an oversight?
 
Good point. It is intended as it will make the zip very large.

You can make a copy of an existing python folder or change this line to point to an existing Python runtime:
Code:
Dim opt As PyOptions = Py.CreateOptions(File.Combine(File.DirApp, "Python/python/python.exe"))
In both cases you need to then install the dependencies if not already installed.
Code:
'dependencies: pip install scikit-learn matplotlib
 
OK. Thanks. I find that getting the environment right is one of the most difficult things about using python. I certainly don't want one big environment, and also don't want 100 that are identical. It will take some figuring out.
 
in the future, It might be worth considering adding an option in the IDE to copy a new python environment to the project if it doesn't exist.
 
Back
Top