Machine learning example on PythonAnywhere

70 views
Skip to first unread message

Drazen D. Babic

unread,
Mar 1, 2023, 2:38:52 AM3/1/23
to Jam.py Users Mailing List
Hi all,

created the App:

inspired from this article:

The only thing I changed from above was the CSV import, to allow numbered PK.

The App enables one to create the ML Model by selecting the number of rows or/and modifying the values in the model.

Then, one can predict the CO2 emissions by adding the value or editing the existing rows.

Enjoy

D.

Drazen D. Babic

unread,
Mar 1, 2023, 9:11:42 PM3/1/23
to Jam.py Users Mailing List
Hey, 
looks like some people tried the App, thanks!
I think the idea of using ML for predicting the values is using "huge" (define huge), data model.

When data Model has only a few rows, ie we select only one row and hit "Generate Model", the result
is based on single row of data! And thats not great!

So we can sort the data in the Model, and select for example "Acura" make of car hit the "Generate Model",
and then all prediction will be based on Acura make of cars! Or Audi, BMD, etc...

Or, we select ALL rows in the model, hit "Generate Model" which is then result irrelevant to a car model!
Bigger the data model, better results.
Hence, if Jam has enough data for ie sales, we can quickly make a ML example from this data. 
I'm not a data scientist though, just throwing ideas :) 

I am not sure how and where to add the information how big the CURRENT model is when generated, any ideas?

Drazen D. Babic

unread,
Mar 2, 2023, 3:01:07 AM3/2/23
to Jam.py Users Mailing List
So, now if we add this code into the Task/Server:

def on_ext_request(task, request, params):
    print(request, params)
    reqs = request.split('/')
    if reqs[2] == 'prediction':
        result = task.co2_emission_predicted.prediction(params)
        return result

We can do the API call to https://mlearning.pythonanywhere.com/ext
and get the nice result depending on data we sent and the Model.

For API, refer to /refs/server/task/on_ext_request.html

To try it, create a file with below and run:
#!/usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen
import json
import time

def send(url, request, params):
    a = urlopen(url + '/' + request, data=str.encode(json.dumps(params)))
    r = json.loads(a.read().decode())
    #print(b)
    return r['result']['data']

if __name__ == '__main__':
    url = 'https://mlearning.pythonanywhere.com/ext'
    result = send(url, 'prediction', [2.4, 8, 8.5])
    print(result)

Drazen D. Babic

unread,
Mar 3, 2023, 1:36:32 AM3/3/23
to Jam.py Users Mailing List
Or simply run:
curl -X POST https://mlearning.pythonanywhere.com/ext/prediction -d "[2.4, 8, 8.5]" -H  "Content-Type: application/json"
Reply all
Reply to author
Forward
0 new messages