“申请人”和“申请人”的实例之间不支持“ <”

时间:2018-12-07 11:29:20

标签: django csv 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)

1 个答案:

答案 0 :(得分:0)

完成此操作的一种方法是使用称为django-import-export的Django程序包。这样,您可以将模型中的每个对象导出到一个csv文件中。

https://django-import-export.readthedocs.io/en/latest/