如何检测我的数据库中的异常数据点
How to detect outlier data points on my database
我目前有一个数据库,其中有一些标错了价格。
示例数据:
Product - Price - SalesDate
ProdA - 10 - 1/1/2016
ProdB - 20 - 1/2/2016
ProdA - 100 - 1/3/2016
ProdB - 20 - 1/4/2016
ProdB - 21 - 1/5/2016
ProdA - 11 - 1/6/2016
在此数据集上,记录 "ProdA - 100 - 1/3/2016" 是有错误的记录。一定是输入价格的人打错了。此外,不同日期的 ProdA 可以改变它的价格,这使得这个问题很有趣。
什么样的工具可以帮助我识别这类记录? SQL 可以帮助我检测异常值数据点吗?我应该为此开始研究机器学习吗?
这有点主观,但您可以确定值与平均值最远的行。我会通过计算 z 分数并查看 largest/smallest z 分数来做到这一点。
z 分数是减去平均值除以标准差的值。以下是计算示例:
select t.*,
(price - avg_price) / nullif(std_price, 0) as z_price
from t join
(select product, avg(price) as avg_price, stdev(price) as std_price
from t
group by product
) tt
on t.product = tt.product
order by abs(z_price) desc;
标准差函数可能会有所不同,具体取决于您使用的数据库,但大多数数据库都支持此类函数。
我目前有一个数据库,其中有一些标错了价格。
示例数据:
Product - Price - SalesDate
ProdA - 10 - 1/1/2016
ProdB - 20 - 1/2/2016
ProdA - 100 - 1/3/2016
ProdB - 20 - 1/4/2016
ProdB - 21 - 1/5/2016
ProdA - 11 - 1/6/2016
在此数据集上,记录 "ProdA - 100 - 1/3/2016" 是有错误的记录。一定是输入价格的人打错了。此外,不同日期的 ProdA 可以改变它的价格,这使得这个问题很有趣。
什么样的工具可以帮助我识别这类记录? SQL 可以帮助我检测异常值数据点吗?我应该为此开始研究机器学习吗?
这有点主观,但您可以确定值与平均值最远的行。我会通过计算 z 分数并查看 largest/smallest z 分数来做到这一点。
z 分数是减去平均值除以标准差的值。以下是计算示例:
select t.*,
(price - avg_price) / nullif(std_price, 0) as z_price
from t join
(select product, avg(price) as avg_price, stdev(price) as std_price
from t
group by product
) tt
on t.product = tt.product
order by abs(z_price) desc;
标准差函数可能会有所不同,具体取决于您使用的数据库,但大多数数据库都支持此类函数。