Feb 17, 2025 Replies: 5 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). 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 Project.zip Project.zip 27 KB · Views: 449 Click to expand...
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). 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.
stevel05 Expert Licensed User Longtime User Feb 17, 2025 #2 The Objects/python folder is not included in the zip file. Is it intended that we set this up after unzipping? Or an oversight?
The Objects/python folder is not included in the zip file. Is it intended that we set this up after unzipping? Or an oversight?
Erel B4X founder Staff member Licensed User Longtime User Feb 17, 2025 #3 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
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
stevel05 Expert Licensed User Longtime User Feb 17, 2025 #4 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.
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.
stevel05 Expert Licensed User Longtime User Feb 17, 2025 #5 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.
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.
Erel B4X founder Staff member Licensed User Longtime User Feb 17, 2025 #6 For now: 1. New Project - PyBridge With Python 2. Ctrl + Click the python shell link at the top. 3. Write: pip install scikit-learn matplotlib Wait for it to complete and close the window. 4. Move the Objects\Python to the existing project Objects folder.
For now: 1. New Project - PyBridge With Python 2. Ctrl + Click the python shell link at the top. 3. Write: pip install scikit-learn matplotlib Wait for it to complete and close the window. 4. Move the Objects\Python to the existing project Objects folder.