【问题标题】:Finding 'representative' group of data in database在数据库中查找“代表性”数据组
【发布时间】:2017-04-12 06:56:24
【问题描述】:

我在数据库中有这些数据,看起来或多或少像这样:

id (int) not null unique | measurement_id (int) not null | range_id (int) not null unique | temperature (int) | TimeOfDay (string) either Dawn or Day or Night | Weather.Clear (Boolean) either true or null | Weather.Cloudy (Boolean) either true or null | Weather.Fog (Boolean) either true or null | Weather.Snow (Boolean) either true or null | Area.City (Boolean) either true ot null | Area.Country (Boolean) either true or null | etc

有数十万行,假设有人对这些数据进行了统计,例如 40% 的行在(TimeOfDay(字符串)Dawn 或 Day 或 Night)列中有 Day,65% 是在 (Weather.Clear (Boolean) true 或 null) 列等中为 true。此外,通常的逻辑适用,因此如果 Weather.Clear 设置为 true,则 Weather.Cloudy 为 null 等。

我的工作是找到“代表”组,比如说 1000 行数据。因此,我需要 1000 = 400 行中的 40% 在 (TimeOfDay (string) Dawn 或 Day or Night) 列中具有 Day,其中 65% (650) 在 (Weather.Clear (Boolean) 中具有“真”或真或 null) 列等。

我意识到仅使用 SQL (oracle) 查询来实现非常困难(或者我可能错了),所以我应该在这里使用什么样的方法来使用像 python 这样的通用编程语言来获得我需要的结果?这有什么算法吗?

问候。

【问题讨论】:

  • 为了便于理解简化您的场景。给出几行的样本数据和基于此的预期输出。
  • Oracle提供统计功能。看看thisthis
  • Oracle 没有boolean 数据类型,那么这些值是如何表示的呢?
  • @Utsav 示例,以防它可以更好地理解问题:[示例数据][1] [1]:i.stack.imgur.com/Nfo9q.png 当然,静态是在如下列上完成的:temperature |时间 |天气。清除 |天气。多云 |天气。雾 |天气.雪 |地区.城市 |地区.国家
  • 你说“当然”,但这并没有什么当然,这不是一个普通的数据库表。大概是在ETL过程中没有太多转换就加载的文件。

标签: python sql oracle algorithm


【解决方案1】:

统计中用于获取“代表性”数据组的一种方法random sampling

SQL中最简单的可能实现如下

1) 为表中的每一行分配一个介于 0 和 1 之间的随机值

2) 对随机列上的数据进行排序

3) 获取定义顺序的前N行

SELECT id
FROM
  (SELECT id,
    rnd
  FROM
    ( SELECT id, dbms_random.value rnd FROM t
    )
  ORDER BY rnd
  )
WHERE rownum <= 1000;

【讨论】:

  • ORDER BY dbms_random.value 可以工作,而且打字更少,
  • @APC 我故意输入更多内容以使代码与上面的描述保持同步:)。但是在实际使用中当然会用到你的快捷方式!
  • 在不了解用例的情况下,也没有定义“代表性”阈值的情况下,我认为随机子集应该足够好。
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多