在django框架中加载XGBoost模型

时间:2018-04-13 15:54:40

标签: python django django-forms xgboost

我正在尝试从Django模型实体的FileField加载xgboost模型。它不起作用。

def do(execution):
    dataFile = execution.dataFile
    np = genfromtxt(dataFile, delimiter=',')

    import xgboost as xgb
    bst = xgb.Booster({'nthread': 4})  # init model
    # bst.load_model('testModel.bin') "Working when used instead of following line"
    bst.load_model(execution.modelFile)

    data = xgb.DMatrix(np)

    preds = bst.predict(data)

    print(preds)

我收到此错误:

  

TypeError:memoryview:需要类似字节的对象,而不是'FieldFile'

完整追踪:

Traceback (most recent call last):
  File "/home/andrea/environments/new_env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/andrea/environments/new_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/andrea/environments/new_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/andrea/Documents/python/DataProject/DataProject/views.py", line 19, in run
    ModelService.do(e)
  File "/home/andrea/Documents/python/DataProject/executions/ModelService.py", line 10, in do
    bst.load_model(execution.modelFile)
  File "/home/andrea/environments/new_env/lib/python3.5/site-packages/xgboost/core.py", line 1106, in load_model
    ptr = (ctypes.c_char * len(buf)).from_buffer(buf)
TypeError: memoryview: a bytes-like object is required, not 'FieldFile'

执行模式:

class Execution(models.Model):
    title = models.CharField(max_length=100)
    date = models.DateTimeField(auto_now_add=True)
    dataFile = models.FileField(upload_to="dataset", null=True)
    modelFile = models.FileField(upload_to="model", null=True)
    def __str__(self):
        return self.title

我不知道如何继续,BinaryField,自定义xgboost对象字段,还是有一个更简单的解决方案。非常感谢

0 个答案:

没有答案