SQLite 的标准差
我搜索了 SQLite 文档,但找不到任何内容,但我也在 Google 上进行了搜索,出现了一些结果。
SQLite 有内置的标准差函数吗?
I've searched the SQLite docs and couldn't find anything, but I've also searched on Google and a few results appeared.
Does SQLite have any built-in Standard Deviation function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不,我搜索了同样的问题,最后不得不用我的应用程序(PHP)进行计算
No, I searched this same issue, and ended having to do the calculations with my application (PHP)
在 python 函数中添加了一些错误检测
added some error detection in the python functions
您没有说明要计算哪个版本的标准差,但可以使用 sum() 和 count() 聚合函数的组合来计算任一版本的方差(标准差平方)。
仍然需要取这些值的平方根来获得标准差。
You don't state which version of standard deviation you wish to calculate but variances (standard deviation squared) for either version can be calculated using a combination of the sum() and count() aggregate functions.
It will still be necessary to take the square root of these to obtain the standard deviation.
您可以在 SQL 中计算方差:
但是,您仍然需要计算平方根才能获得标准差。
You can calculate the variance in SQL:
However, you still have to calculate the square root to get the standard deviation.
SQLite支持的聚合函数在这里:
http://www.sqlite.org/lang_aggfunc.html
STDEV 不在列表中。
但是,此页面<中的模块
extension-functions.c
/strong> 包含 STDEV 函数。The aggregate functions supported by SQLite are here:
http://www.sqlite.org/lang_aggfunc.html
STDEV is not in the list.
However, the module
extension-functions.c
in this page contains a STDEV function.sqlite中仍然没有内置的stdev函数。但是,您可以定义(正如 Alix 所做的那样)用户定义的聚合器函数。这是 Python 中的完整示例:
这将打印:
Compare with MySQL: http://sqlfiddle .com/#!2/ad42f3/3/0
There is still no built-in stdev function in sqlite. However, you can define (as Alix has done) a user-defined aggregator function. Here is a complete example in Python:
This will print:
Compare with MySQL: http://sqlfiddle.com/#!2/ad42f3/3/0
使用方差公式 V(X) = E(X^2) - E(X)^2。在 SQL sqlite 中
要获得标准差,您需要取平方根 V(X)^(1/2)
Use variance formula V(X) = E(X^2) - E(X)^2. In SQL sqlite
To get standard deviation you need to take the square root V(X)^(1/2)
我实现了 Welford 的方法(与
extension-functions.c
) 作为 SQLite UDF:这是 PHP 中的 (
$db
是 PDO 对象),但移植到另一种语言应该很简单。SQLite 太酷了。 <3
I implemented the Welford's method (the same as
extension-functions.c
) as a SQLite UDF:That's in PHP (
$db
is the PDO object) but it should be trivial to port to another language.SQLite is soooo cool. <3
一个小技巧
,那么剩下的唯一的事情就是在外面计算 sqrt 。
a little trick
then the only thing left is to calculate sqrt outside.