Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 加速Django数据库函数以进行缺失值的地理插值_Python_Django_Postgresql_Pandas_Geodjango - Fatal编程技术网

Python 加速Django数据库函数以进行缺失值的地理插值

Python 加速Django数据库函数以进行缺失值的地理插值,python,django,postgresql,pandas,geodjango,Python,Django,Postgresql,Pandas,Geodjango,我有一个大型的商业地产地址数据库(大约500万行),其中20万缺少楼层面积。这些房产是按行业分类的,我知道每个行业的租金 我对缺失楼层面积进行插值的方法是在未知楼层面积的物业的指定半径内过滤类似分类的物业,然后根据附近物业的成本/平方米中位数计算楼层面积 最初,我是使用的,但随着数据集变得越来越大(甚至使用groupby),这就成了问题。它经常超过可用内存,并停止。当它工作时,大约需要3个小时才能完成 我正在测试是否可以在数据库中执行相同的任务。我为径向填充编写的函数如下: def _radia

我有一个大型的商业地产地址数据库(大约500万行),其中20万缺少楼层面积。这些房产是按行业分类的,我知道每个行业的租金

我对缺失楼层面积进行插值的方法是在未知楼层面积的物业的指定半径内过滤类似分类的物业,然后根据附近物业的成本/平方米中位数计算楼层面积

最初,我是使用的,但随着数据集变得越来越大(甚至使用
groupby
),这就成了问题。它经常超过可用内存,并停止。当它工作时,大约需要3个小时才能完成

我正在测试是否可以在数据库中执行相同的任务。我为径向填充编写的函数如下:

def _radial_fill(self):
    # Initial query selecting all latest locations, and excluding null rental valuations
    q = Location.objects.order_by("locode","-update_cycle") \
                        .distinct("locode")
    # Chained Q objects to use in filter
    f = Q(rental_valuation__isnull=False) & \
        Q(use_category__grouped_by__isnull=False) & \
        Q(pc__isnull=False)
    # All property categories at subgroup level
    for c in LocationCategory.objects.filter(use_category="SGP").all():
        # Start looking for appropriate interpolation locations
        fc = f & Q(use_category__grouped_by=c)
        for l in q.filter(fc & Q(floor_area__isnull=True)).all():
            r_degree = 0
            while True:
                # Default Distance is metres, so multiply accordingly
                r = (constants.BOUNDS**r_degree)*1000 # metres
                ql = q.annotate(distance=Distance("pc__point", l.pc.point)) \
                      .filter(fc & Q(floor_area__isnull=False) & Q(distance__lte=r)) \
                      .values("rental_valuation", "floor_area")
                if len(ql) < constants.LOWER_RANGE:
                    if r > constants.UPPER_RADIUS*1000:
                        # Further than the longest possible distance
                        break
                    r_degree += 1
                else:
                    m = median([x["rental_valuation"]/x["floor_area"]
                                for x in ql if x["floor_area"] > 0.0])
                    l.floor_area = l.rental_valuation / m
                    l.save()
                    break
class LocationCategory(models.Model):
    # Category types
    GRP = "GRP"
    SGP = "SGP"
    UST = "UST"
    CATEGORIES = (
        (GRP, "Group"),
        (SGP, "Sub-group"),
        (UST, "Use type"),
    )
    slug = models.CharField(max_length=24, primary_key=True, unique=True)
    usecode = models.CharField(max_length=14, db_index=True)
    use_category = models.CharField(max_length=3, choices=CATEGORIES,
                                    db_index=True, default=UST)
    grouped_by = models.ForeignKey("self", null=True, blank=True,
                                   on_delete=models.SET_NULL,
                                   related_name="category_by_group")

class Location(models.Model):
    # Hereditament identity and location
    slug = models.CharField(max_length=24, db_index=True)
    locode = models.CharField(max_length=14, db_index=True)
    pc = models.ForeignKey(Postcode, null=True, blank=True,
                           on_delete=models.SET_NULL,
                           related_name="locations_by_pc")
    use_category = models.ForeignKey(LocationCategory, null=True, blank=True,
                                     on_delete=models.SET_NULL,
                                     related_name="locations_by_category")
    # History fields
    update_cycle = models.CharField(max_length=14, db_index=True)
    # Location-specific econometric data
    floor_area = models.FloatField(blank=True, null=True)
    rental_valuation = models.FloatField(blank=True, null=True)

class Postcode(models.Model):
    pc = models.CharField(max_length=7, primary_key=True, unique=True) # Postcode excl space
    pcs = models.CharField(max_length=8, unique=True)                  # Postcode incl space
    # http://spatialreference.org/ref/epsg/osgb-1936-british-national-grid/
    point = models.PointField(srid=4326)
使用Django 2.0和Postgresql 10

更新

通过以下代码更改,我在运行时实现了35%的改进:

# Initial query selecting all latest locations, and excluding null rental valuations
q = Location.objects.order_by("slug","-update_cycle") \
                    .distinct("slug")
# Chained Q objects to use in filter
f = Q(rental_valuation__isnull=False) & \
    Q(pc__isnull=False) & \
    Q(use_category__grouped_by_id=category_id)
# All property categories at subgroup level
# Start looking for appropriate interpolation locations
for l in q.filter(f & Q(floor_area__isnull=True)).all().iterator():
    r = q.filter(f & Q(floor_area__isnull=False) & ~Q(floor_area=0.0))
    rl = Location.objects.filter(id__in = r).annotate(distance=D("pc__point", l.pc.point)) \
                                            .order_by("distance")[:constants.LOWER_RANGE] \
                                            .annotate(floor_ratio = F("rental_valuation")/
                                                                    F("floor_area")) \
                                            .values("floor_ratio")
    if len(rl) == constants.LOWER_RANGE:
        m = median([h["floor_ratio"] for h in rl])
        l.floor_area = l.rental_valuation / m
        l.save()
id\uu in=r
效率低下,但在添加和排序新注释时,它似乎是维护
distinct
queryset的唯一方法。考虑到在
r
查询中可以返回大约100000行,因此在那里应用的任何注释,以及随后按距离排序,都可能需要非常长的时间

然而。。。我在尝试实现子查询功能时遇到了许多问题
AttributeError:'ResolveDoUserRef'对象没有属性“\u output\u field\u或\u none”
,我认为这与注释有关,但我找不到太多

相关重组代码为:

rl = Location.objects.filter(id__in = r).annotate(distance=D("pc__point", OuterRef('pc__point'))) \
                                        .order_by("distance")[:constants.LOWER_RANGE] \
                                        .annotate(floor_ratio = F("rental_valuation")/
                                                                F("floor_area")) \
                                        .distinct("floor_ratio")
以及:


我可以看出,这种方法应该非常有效,但正确地使用它似乎远远超出了我的技能水平。

您可以使用(大部分)Django的内置查询方法来简化您的方法,这些方法都经过了优化。更具体地说,我们将使用:

  • 和方法(对于版本>=1.11)
  • 来自的
    注释
    平均值
  • 查找
  • 表达式(可以在我的QA样式示例中找到
    F()
    的详细用例:

我们将创建一个定制的聚合类来应用我们的
AVG
函数(受这个优秀答案启发的方法:)

我们将使用它来计算以下平均值:

for location in Location.objects.filter(rental_valuation__isnull=True):
    location.update(
        rental_valuation=CustomAVG(
            Subquery(
                Location.objects.filter(
                    pc__point__dwithin=(OuterRef('pc__point'), D(m=1000)),
                    rental_valuation__isnull=False
                ).annotate(area_value=F('rental_valuation')/F('floor_area'))
                .distinct('area_value')
            )
        )
    )
以上各项的细分:

  • 我们收集所有
    位置
    对象,但没有
    租赁估价
    ,我们“通过”列表
  • 子查询:我们从当前位置点选择
    位置
    半径=1000m的圆内的
    对象(根据需要更改),并
    在其上注释成本/m2计算(使用
    F()
    将每个对象的
    租金(估价
    楼层(面积)
    列的值作为名为
    面积(估价)
    的列。为获得更准确的结果,我们仅选择此列的不同值
  • 我们将我们的
    CustomAVG
    应用于
    子查询
    ,并更新我们当前的位置
    租金估价

谢谢。我还尝试将单个循环拆分为芹菜流程,看看是否有任何效果。一旦我采纳了您的建议,我将尝试发布一些更新的时间……一个问题是需要通过将计算的
租金估价限制为其中位置计数为>
常量。较低的_范围
。即,仅使用1或2个其他位置来推断“面积值”是有风险的,我希望使用尽可能最小的面积来获得能够产生该平均值的属性计数。有什么想法吗?@Turukawa对于第一部分,您可以使用条件表达式:。对于第二部分,根据您的数据集,您需要定义尽可能小的范围。@Turukawa您觉得这个答案有用吗?老实说,我还在想如何实现它……到目前为止,我已经实现了35%的时间改进(6到4天),但没有子查询。我遇到的问题是在应用
distinct
(这似乎需要第二次查询)后对注释进行排序,然后找出如何获得中值,而不是
AVG
,因为我需要丢弃异常值(尚未解决)…仍在处理此问题,几天后将返回给您。
class CustomAVG(Subquery):
    template = "(SELECT AVG(area_value) FROM (%(subquery)s))"
    output_field = models.FloatField()
for location in Location.objects.filter(rental_valuation__isnull=True):
    location.update(
        rental_valuation=CustomAVG(
            Subquery(
                Location.objects.filter(
                    pc__point__dwithin=(OuterRef('pc__point'), D(m=1000)),
                    rental_valuation__isnull=False
                ).annotate(area_value=F('rental_valuation')/F('floor_area'))
                .distinct('area_value')
            )
        )
    )