Azure流分析 - 计算线性回归

时间:2018-02-01 16:10:11

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

我正在尝试使用Azure Stream Analytics计算一些线性回归。

设置: 传感器向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;
) 

如果您在没有Stream Analytics的情况下知道更好的方法,我也很好。请提前分享您的想法和感谢!

1 个答案:

答案 0 :(得分:1)