sql - Azure 流分析 - 计算线性回归

标签 sql azure linear-regression azure-stream-analytics

我正在尝试使用 Azure 流分析计算一些线性回归。

设置: 有一个传感器向 IoT-Hub 发送温度和时间,Stream-Analytics 正在使用 SlidingWindow 监听 IoT-Hub,并且应该计算该窗口中的温度趋势(使用线性回归)。

事件如下所示:

{"deviceId":"sensor_1","temp":322.3376736446427,"time":1517500183940}

到目前为止我得到的SQL是这样的:

WITH Step1 AS
(
select time AS x, avg(time) over () AS x_bar,
       temp AS y, avg(temp) over () AS y_bar
FROM inputStream
GROUP BY SlidingWindow(second,10) 
),
Step2 AS (
    SELECT (sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar))) AS slope,
           max(x_bar) AS x_bar_max,
           max(y_bar) AS y_bar_max    
    FROM Step1
),
FinalStep AS (
    SELECT  slope, 
            y_bar_max - x_bar_max * slope AS intercept
    FROM Step2
)

SELECT * INTO outputEventHub FROM FinalStep

来自 here 的原始线性回归 SQL 模板看起来像这样:

select slope, 
       y_bar_max - x_bar_max * slope as intercept 
from (
    select sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope,
           max(x_bar) as x_bar_max,
           max(y_bar) as y_bar_max    
    from (
        select x, avg(x) over () as x_bar,
               y, avg(y) over () as y_bar
        from ols) s;
) 

如果您知道无需流分析即可实现此目的的更好方法,我也很好。请提前分享您的想法并表示感谢!

最佳答案

关于sql - Azure 流分析 - 计算线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567103/

相关文章:

mysql存在数据添加额外的小数字段以供自动添加

java - Oracle SQL - 在通过 JDBC 调用的查询上重用绑定(bind)变量

azure - 在缩小 Azure Kubernetes 服务 (AKS) 中的节点后如何重新安排我的 pod?

html - 我可以在自定义的 Azure AD B2C 用户界面中使用 SVG 吗?

azure - 错误 MSB4086 : A numeric comparison was attempted on "$(MSBuildVersion)" that evaluates to "" instead of a number

python - 线性回归(OLS): Confidence Intervals are not being calculated accurately using Statsmodel summary_Frame()

mysql - 具有相同多列的 SQL 搜索值

java - 提高 Primefaces 数据表性能

r - 在 R 中的多元回归中选择变量

scala - 从 FlinkML 多元线性回归中提取权重