我正在尝试使用 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;
)
如果您知道无需流分析即可实现此目的的更好方法,我也很好。请提前分享您的想法并表示感谢!
最佳答案
ASA 文档有一个二变量回归的示例。 https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-high-frequency-trading
关于sql - Azure 流分析 - 计算线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567103/