How do I implement a machine learning model trained by python to a webpage?

Viewed 946

In spyder, I have trained a Naive Bayes classifier for predicting whether or not a school will be canceled base on the amount of snow and other features. So, my question is two parts. First, how do I make a website so that a user can input the snow and temperature, and when the user clicks a button, the classifier (which is in python) can spit out a response? Do I have to use Django for that (so that everything is in python and would maybe make stuff easier)? Second, How do I save the classifier so that the model doesn’t have to be trained every time?

2 Answers

I can help you with this task at discord https://discord.gg/Jgutu2w .

Stack overflow ain't too good to ask as many questions in a single thread. Please reconsider splitting your question into more simple questions each having a single thread.

Your question is quite specific, so I doubt you will get a response. Django is kinda to powerful for this task, but I personally do not know other solution.

https://docs.djangoproject.com/en/2.1/

https://www.youtube.com/watch?v=n-FTlQ7Djqc&list=PL4cUxeGkcC9ib4HsrXEYpQnTOTZE1x0uc

First, how do I make a website so that a user can input the snow and temperature

  1. pip install django

  2. django-admin startproject weatherpredictor

  3. cd weatherpredictor

  4. python manage.py makemigrations

  5. python manage.py migrate

  6. python manage.py runserver

model.py

from django.db import models
    class WeatherStateModel(models.Model):
        WEATHER_OPTION = (
            ('N', 'Bad weather'),
            ('B', 'Good weather'),
            ('M', 'Meh weather'),
        )
        SNOW_OPTION = (
            (2, 'A lot snow'),
            (1, 'Not that much'),
            (0, 'No snow'),
        )
        weather = models.CharField(max_length=1, choices=WEATHER_OPTION)
        temperature= models.IntegerField()
        snow = models.IntegerField(choices=SNOW_OPTION)

urls.py

from django.urls import path
from . import views

app_name = 'weatherpredictor'
urlpatterns = [
    path('',
         views.predictweather.as_view(), name='predictweather'),

]

views.py

from .models import WeatherStateModel
from django.views.generic import CreateView
class leave_feedback(CreateView):
    model = WeatherStateModel
    fields = ['weather', 'temperature', 'snow']
    success_url = '/'

in templates/weatherpredictor/weatherstatemodel_form.html

<form enctype="multipart/form-data" action="" method="post">
  {% csrf_token %} {{ form }} <input type="submit" value="Save" />
</form>

The user clicks a button, the classifier (which is in python) can spit out a response?

CANT HELP WITHOUT ML CODE

Second, How do I save the classifier so that the model doesn’t have to be trained every time?

Depends on which format you keep classifier, what module do you import, etc.

CANT HELP WITHOUT ML CODE

To answer the first part of my question: You would simply make a django model that has 2 fields and then make a form that makes users fill out those two. When form is submitted, a model object will be created and then sent to the database.

To answer the second part of my question: Use a class name joblib in your python IDE(or something like cpickle whatever), joblib or cpickle should have a built in method called save classifier or something. This way, you can save the classifier actually as a pkg file. Then, drag that into your django folder, and when you need to use that classifier, import joblib/cpickle again, and use the built in method "load". Now, you have a classifier to deploy. If you trained your classifier with scikit learn, all classifiers have the built in method "predict", so access the data stored in the user's database, and then do classifier.predict(the data accessed) to get a prediction.

One of those simple stuff that just requires knowledge on multiple platforms.

Related