gpt4 book ai didi

django - 如何在 Django 中创建一个 numpy 数组字段?

转载 作者:行者123 更新时间:2023-12-03 14:39:39 25 4
gpt4 key购买 nike

我想要 Django 中的一个 numpy 数组字段,以便我可以做这样的事情

from example.models import Series
import numpy as np
array = np.array([1, 2, 3])
model = Series.objects.create(id=1, array=array)
model = Series.objects.get(id=1)
assert np.array_equal(array, model.array)

本质上,该字段应该将 numpy 数组序列化为二进制并自动反序列化。目前,我只是这样做:
import base64
import numpy as np
from django.db import models

class Series(models.Model):
id = models.IntegerField(primary_key=True, unique=True)
array = models.BinaryField()

def get_array():
return np.frombuffer(base64.decodebytes(self.array), dtype=np.float32)

def set_array(array):
self.array = base64.b64encode(array)

如果这是一个可重用的字段,我更喜欢它,因为我有很多模型需要存储一个 numpy 数组。例如:
class Series(models.Model):
array = NumpyArrayField(dtype=np.float32)

那么,我该如何写一个 NumpyArrayField完成这个的类?

我尝试执行以下操作(复制 BinaryField 的源代码)
import base64

import numpy as np
from django.db import models

class NumpyArrayField(models.Field):
empty_values = [None]

def __init__(self, dtype, *args, **kwargs):
self.dtype = dtype
super(NumpyArrayField, self).__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super(NumpyArrayField, self).deconstruct()
kwargs['dtype'] = self.dtype
return name, path, args, kwargs

def get_internal_type(self):
return 'NumpyArrayField'

def get_placeholder(self, value, compiler, connection):
return connection.ops.binary_placeholder_sql(value)

def get_default(self):
if self.has_default() and not callable(self.default):
return self.default
default = super(NumpyArrayField, self).get_default()
if default == '':
return b''
return default

def get_db_prep_value(self, value, connection, prepared=False):
value = super(NumpyArrayField, self).get_db_prep_value(value, connection, prepared)
value = base64.b64encode(value)
if value is not None:
return connection.Database.Binary(value)
return value

def value_to_string(self, obj):
return base64.b64encode(obj).decode('ascii')

def to_python(self, value):
return np.frombuffer(base64.decodebytes(value), dtype=self.dtype)


class Series(models.Model):
id = models.IntegerField(primary_key=True, unique=True)
array = NumpyArrayField(dtype=np.int32)

迁移运行良好,但我得到了 django.db.utils.OperationalError: table example_series has no column named array错误。

最佳答案

我用 MySQL 将 numpy 数组保存到 Django 模型,这里是:

  • 将字段设置为 Django BinaryField

  • from django.db import models

    np_field = models.BinaryField()


  • 使用泡菜转储将 numpy 数组转换为 python 字节,然后由 base64 编码

  • np_bytes = pickle.dumps(np_array)

    np_base64 = base64.b64encode(np_bytes)

    model.np_field = np_base64


  • 从 Django 模型中获取 numpy 数组

  • np_bytes = base64.b64decode(model.np_field)

    np_array = pickle.loads(np_bytes)

    关于django - 如何在 Django 中创建一个 numpy 数组字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46699238/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com