【问题标题】:Trouble datamining with Django使用 Django 进行数据挖掘时遇到问题
【发布时间】:2011-07-12 06:03:17
【问题描述】:

我已经建立了一个 Django 项目,我在其中创建了随机点。这些随机点存储在数据库(sqlite)中(我可以通过管理网站查看它们并更改值,所以这是可行的)。 如果我写一个脚本,我可以访问这些点并将它们打印在一个情节中。请参阅下面的代码。

但是,如果我想挖掘这些点以对它们进行排序或仅绘制数据集的选择,我似乎遇到了麻烦。如果我读出它们不再连接的值并且对 x 进行排序会混淆点集。 有没有办法将数据集排序为在这种情况下为 X 的最小值并对值进行排序并打印集合? (保持点的所有 x、y、z 和名称值不变?)(请参阅下面的答案,point in Point3D.objects.all().order_by('x'):

如果我现在想让 x 的值介于 x = 12 和 x = 30 之间?如何添加这个额外的过滤器?

我的代码如下: 模型.py:

class Point3D(models.Model):
name = models.CharField(max_length = 10)
x = models.DecimalField(max_digits=5, decimal_places=2)
y = models.DecimalField(max_digits=5, decimal_places=2)
z = models.DecimalField(max_digits=5, decimal_places=2)

生成点:

from books.models import Point3D

def points():
    for i in range(20):
        x = random.randint(0,100)
        y = random.randint(0,100)
        z = random.randint(0,100)

        p = Point3D(name = x , x = x ,y = y,z = z)
#        print 'test'
        p.save()
#    

points()

在views.py中:

def ThreeGraphs(request):
    fig = Figure()
    fig.suptitle('2D-punten')
    ax = fig.add_subplot(111)

    for point in Point3D.objects.all():
        print point
        name = int(point.name)
        xs = int(point.x)
        ys = int(point.y)
        zs = int(point.z)
        print (xs, ys, zs)
        ax.plot(xs, ys, 'bo' )

    HttpResponse(mimetype="image/png")
    FigureCanvas(fig)
    fig.savefig('template/images/testing.png')
    picture = "testing.png"
    return render_to_response('Test.html', {'picture': picture}, RequestContext(request))

希望有人知道如何解决我的问题。

非常感谢! 提耶尔

【问题讨论】:

    标签: database django data-mining points


    【解决方案1】:

    你需要这样做:

        for point in Point3D.objects.all().order_by('x'):
    

    这将按“x”字段的排序顺序返回点。你可以说 order_by('-x') 来反转排序顺序。

    【讨论】:

    • 是否可以在对列表进行排序后指定您只想选择 10:30?我知道 Point3D.object.all()[10:30] 有效,但你还能在排序后添加过滤器吗?
    • 我的意思是我可以按 x 排序然后只选择 x = 12 到 x = 30 的范围吗?
    • 你想要 .filter(x__gte=12, x__lte=30) 而不是 .all() - 还有docs.djangoproject.com/en/dev/topics/db/queries
    猜你喜欢
    • 2015-05-15
    • 2018-10-23
    • 2011-07-28
    • 2011-07-25
    • 2012-08-04
    • 1970-01-01
    • 2012-06-21
    • 2016-06-26
    • 1970-01-01
    相关资源
    最近更新 更多