将SQLite结果检索为ndarray

时间:2015-07-14 11:30:12

标签: python sqlite

我正在从sqlite数据库中检索一组纬度和纵向点,如下所示:

cur = con.execute("SELECT DISTINCT latitude, longitude FROM MessageType1 WHERE latitude>{bottomlat} AND latitude<={toplat} AND longitude>{bottomlong} AND longitude<={toplong}".format(bottomlat = bottomlat, toplat = toplat,  bottomlong = bottomlong, toplong = toplong))

但是,因为我应该创建一个具有ndarray作为输入的ConvexHull(http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html#scipy.spatial.ConvexHull),我需要将cur的结果保存为ndarray。我该怎么办?

现在我将值取为:

    positions = [[floats(x[0]) floats(x[1])]  for x in cur]

这是对的吗?

1 个答案:

答案 0 :(得分:2)

您无需将头寸转换为ndarray。 scipy.spatial.ConvexHull也可以接受列表列表:

import scipy.spatial as spatial
hull = spatial.ConvexHull(positions)

此外,如果您的MessageType1表具有float类型的纬度,经度字段,那么您不需要显式调用float。而不是

positions = [[floats(x[0]) floats(x[1])]  for x in cur]

你可以使用

positions = cur.fetchall()

请注意,如果您使用的是NumPy切片语法,例如positions[:, 0], 那么你需要将列表/元组列表转换为NumPy数组:

positions = np.array(cur.fetchall())