如何从Django收集表单数据并将其传递给xgboost模型进行预测

时间:2018-12-11 19:59:43

标签: django xgboost

我有一个模型,我想将其用于使用pickle加载的预测中,并且有一个使用django创建的表单。但是,当用户提交表单时,我希望它以csv格式存储在变量中,因此我可以对用户填写的每个表单执行Xgboost预测,然后输出预测。可能是它没有得到任何输入。新手

from django.db import models
from django import forms
from django.core.validators import MaxValueValidator, MinValueValidator
# Create your models here.
type_loan=(("Cash loan","Cash loan"),
            ("Revolving loan","Revolving Loan"))
Gender=(("Male","Male"),
        ("Female","Female"))
Yes_NO=(("YES","YES"),("NO","NO"))
status=(("Single","Single"),
        ("Married","Married"),
        ("Widow","Widow"),
        ("Seprated","Divorce"))
Highest_Education=(("Secondary","Secondary"),
                    ("Incomplete Higher","Incomplete Higher"),
                    ("Lower Secondary","Lower Secondary"),
                    ("Academic Degree","Academic Degree"))
Income_type=(("Working","Working Class"),
            ("State Servant","Civil Servant"),
            ("Commercial Associate","Commercial Associate"),
            ("Pensioner","Pensioner"),
            ("Student","Student"),
            ("Businessman","Business Owner"))


class Applicant(models.Model):
    name=models.CharField(default="Jon Samuel",max_length=50,null="True")
    Birth_date=models.DateField(default="2018-03-12",blank=False, null=True)
    Status=models.CharField(choices=status,max_length=50)
    Children=models.IntegerField(default=0,validators=[MinValueValidator(0),MaxValueValidator(17)])
    Highest_Education=models.CharField(choices=Highest_Education,max_length=50)
    Gender=models.CharField(choices=Gender, max_length=50)
    loan_type=models.CharField(choices=type_loan, max_length=50)
    own_a_car=models.CharField(choices=Yes_NO,max_length=50)
    own_a_house=models.CharField(choices=Yes_NO,max_length=50)

    def __str__(self):
            return self.name

views.py

    from django.shortcuts import render
from .models import Applicant
from .forms import Applicant_form
from django.views.generic import ListView, CreateView, UpdateView
from django.core.cache import cache
import xgboost as xgb
import pickle
from sklearn.preprocessing import LabelEncoder

class CreateMyModelView(CreateView):
    model = Applicant
    form_class = Applicant_form
    template_name = 'loan/index.html'
    success_url = '/loan/results'
    context_object_name = 'name'

class MyModelListView(ListView):
    template_name = 'loan/result.html'
    context_object_name = 'Results'

    def get_queryset(self):
        queryset=Applicant.objects.all()
        with open('model/newloan_model','rb') as f:
            clf=pickle.load(f)
        le=LabelEncoder()
        le.fit(queryset)
        queryset=le.transform(queryset)
        d_test = xgb.DMatrix(queryset) 
        predict=clf.predict(d_test)
        return (predict)

回溯

File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/list.py", line 142, in get
    self.object_list = self.get_queryset()
  File "/home/david/loan60/loan/views.py", line 27, in get_queryset
    data=le.fit_transform(queryset)
  File "/home/david/.local/lib/python3.6/site-packages/sklearn/preprocessing/label.py", line 235, in fit_transform
    y = column_or_1d(y, warn=True)
  File "/home/david/.local/lib/python3.6/site-packages/sklearn/utils/validation.py", line 797, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (3, 10)
[11/Dec/2018 18:46:28] "GET /loan/results HTTP/1.1" 500 92945
Not Found: /favicon.ico
[11/Dec/2018 18:46:28] "GET /favicon.ico HTTP/1.1" 404 2078
[11/Dec/2018 18:46:35] "GET /admin/ HTTP/1.1" 200 6230
[11/Dec/2018 18:46:35] "GET /static/admin/css/dashboard.css HTTP/1.1" 304 0
[11/Dec/2018 18:46:35] "GET /static/admin/css/responsive.css HTTP/1.1" 200 17976
[11/Dec/2018 18:46:35] "GET /static/admin/css/base.css HTTP/1.1" 304 0
[11/Dec/2018 18:46:35] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
[11/Dec/2018 18:46:35] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
[11/Dec/2018 18:46:35] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[11/Dec/2018 18:46:35] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[11/Dec/2018 18:46:35] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 304 0
[11/Dec/2018 18:46:35] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 304 0
[11/Dec/2018 18:46:35] "GET /static/admin/img/icon-deletelink.svg HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /admin/loan/applicant/ HTTP/1.1" 200 4808
[11/Dec/2018 18:46:38] "GET /static/admin/css/changelists.css HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /static/admin/js/jquery.init.js HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /static/admin/js/core.js HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /static/admin/js/admin/RelatedObjectLookups.js HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /static/admin/js/urlify.js HTTP/1.1" 200 8972
[11/Dec/2018 18:46:38] "GET /admin/jsi18n/ HTTP/1.1" 200 3185
[11/Dec/2018 18:46:38] "GET /static/admin/js/prepopulate.js HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /static/admin/js/actions.js HTTP/1.1" 304 0
[11/Dec/2018 18:46:38] "GET /static/admin/js/vendor/xregexp/xregexp.js HTTP/1.1" 200 128820
[11/Dec/2018 18:46:38] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 200 271751
[11/Dec/2018 18:46:38] "GET /static/admin/img/tooltag-add.svg HTTP/1.1" 304 0
[11/Dec/2018 18:46:48] "POST /admin/loan/applicant/ HTTP/1.1" 200 3636
[11/Dec/2018 18:46:48] "GET /static/admin/js/cancel.js HTTP/1.1" 304 0
[11/Dec/2018 18:46:50] "POST /admin/loan/applicant/ HTTP/1.1" 302 0
[11/Dec/2018 18:46:50] "GET /admin/loan/applicant/ HTTP/1.1" 200 3310
[11/Dec/2018 18:46:50] "GET /admin/jsi18n/ HTTP/1.1" 200 3185
[11/Dec/2018 18:46:50] "GET /static/admin/img/icon-yes.svg HTTP/1.1" 304 0
Internal Server Error: /loan/results
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/list.py", line 142, in get
    self.object_list = self.get_queryset()
  File "/home/david/loan60/loan/views.py", line 28, in get_queryset
    d_test = xgb.DMatrix(data) #changing the data to matrix form
  File "/home/david/.local/lib/python3.6/site-packages/xgboost/core.py", line 382, in __init__
    self._init_from_npy2d(data, missing, nthread)
  File "/home/david/.local/lib/python3.6/site-packages/xgboost/core.py", line 450, in _init_from_npy2d
    raise ValueError('Input numpy.ndarray must be 2 dimensional')
ValueError: Input numpy.ndarray must be 2 dimensional
[11/Dec/2018 18:46:57] "GET /loan/results HTTP/1.1" 500 93391
[11/Dec/2018 18:47:04] "GET /loan/ HTTP/1.1" 200 2494
[11/Dec/2018 18:47:09] "POST /loan/ HTTP/1.1" 302 0
Internal Server Error: /loan/results
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/david/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/david/.local/lib/python3.6/site-packages/django/views/generic/list.py", line 142, in get
    self.object_list = self.get_queryset()
  File "/home/david/loan60/loan/views.py", line 27, in get_queryset
    data=le.fit_transform(queryset)
  File "/home/david/.local/lib/python3.6/site-packages/sklearn/preprocessing/label.py", line 235, in fit_transform
    y = column_or_1d(y, warn=True)
  File "/home/david/.local/lib/python3.6/site-packages/sklearn/utils/validation.py", line 797, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (1, 10)

0 个答案:

没有答案