跳转至

foundryts.functions.linear_regression(foundryts.functions.linear_regression)

foundryts.functions.linear_regression(include_intercept=True, time_unit='ns', start=None, end=None)

Returns a function that performs linear regression on a single time series.

Linear regression finds the parameters of the best-fit line over points of the input time series. Linear regression is expressed as y = Ax + B, where A is the slope of the line and B is the y-intercept. The returned function will provide the parameters A and B.

Linear regression is useful when you need to identify and quantify a linear trend in your time series data.

  • Parameters:
  • time_unit (str , optional) – The time unit of the coefficients, must be one of “s”, “ms”, “us”, “ns” (default is “ns”).
  • start (str | int | datetime.datetime , optional) – Starting point (inclusive) of the time series for computing the linear regression.
  • end (str | int | datetime.datetime , optional) – End point (exclusive) of the time series for computing the linear regression.
  • Returns: A function that accepts a single time series and returns parameters for the best-fit line for the points in the time series using linear regression.
  • Return type: (FunctionNode) -> SummarizerNode

Dataframe schema

Column name Type Description
max_bounds.first_value float Maximum value of the slope (A) in y=Ax+B.
max_bounds.second_value float Maximum value of the intercept (B) in y=Ax+B.
min_bounds.first_value float Minimum value of the slope (A) in y=Ax+B.
min_bounds.second_value float Minimum value of the intercept (B) in y=Ax+B.
regression_fit_function.
linear_regression_fit.
slope
float Parameter ‘A’ (slope) of the linear regression fit in
y=Ax+B.
regression_fit_function.
linear_regression_fit.
intercept
float Parameter ‘B’ (intercept) of the linear regression fit in
y=Ax+B.
regression_fit_function.
linear_regression_fit.
statistics.rsquared
float R-squared value indicating the goodness of fit of the
linear regression.

:::callout{theme="success" title="See Also"} exponential_regression(), polynomial_regression() :::

:::callout{theme="warning" title="Note"} This function is only applicable to numeric series. :::

Examples

>>> series = F.points(
...     (10, 6.0), (20, 12.0), (30, 24.0), (40, 48.0), (50, 96.0), name="series"
... )
>>> series.to_pandas()
                      timestamp  value
0 1970-01-01 00:00:00.000000010    6.0
1 1970-01-01 00:00:00.000000020   12.0
2 1970-01-01 00:00:00.000000030   24.0
3 1970-01-01 00:00:00.000000040   48.0
4 1970-01-01 00:00:00.000000050   96.0
>>> lin_regr = F.linear_regression()(series)
>>> lin_regr.to_pandas()
   max_bounds.first_value  max_bounds.second_value  min_bounds.first_value  min_bounds.second_value  regression_fit_function.linear_regression_fit.intercept  regression_fit_function.linear_regression_fit.slope  regression_fit_function.linear_regression_fit.statistics.rsquared
0                    50.0                     96.0                    10.0                      6.0                                              -27.6                                                     2.16                                             0.870968

中文翻译

foundryts.functions.linear_regression

foundryts.functions.linear_regression(include_intercept=True, time_unit='ns', start=None, end=None)

返回一个对单条时间序列执行线性回归的函数。

线性回归用于寻找输入时间序列各点最佳拟合线的参数。线性回归表示为 y = Ax + B,其中 A 是直线的斜率,B 是 y 轴截距。返回的函数将提供参数 AB

当您需要识别和量化时间序列数据中的线性趋势时,线性回归非常有用。

  • 参数:
  • time_unit (str , 可选) – 系数的时间单位,必须为 "s"、"ms"、"us"、"ns" 之一(默认为 "ns")。
  • start (str | int | datetime.datetime , 可选) – 计算线性回归的时间序列起始点(包含)。
  • end (str | int | datetime.datetime , 可选) – 计算线性回归的时间序列结束点(不包含)。
  • 返回值: 一个函数,接受单条时间序列并返回使用线性回归对该时间序列中各点进行最佳拟合的线参数。
  • 返回类型: (FunctionNode) -> SummarizerNode

数据框模式(Dataframe schema)

列名 类型 描述
max_bounds.first_value float y=Ax+B 中斜率 (A) 的最大值。
max_bounds.second_value float y=Ax+B 中截距 (B) 的最大值。
min_bounds.first_value float y=Ax+B 中斜率 (A) 的最小值。
min_bounds.second_value float y=Ax+B 中截距 (B) 的最小值。
regression_fit_function.
linear_regression_fit.
slope
float y=Ax+B 中线性回归拟合的参数 'A'(斜率)。
regression_fit_function.
linear_regression_fit.
intercept
float y=Ax+B 中线性回归拟合的参数 'B'(截距)。
regression_fit_function.
linear_regression_fit.
statistics.rsquared
float 表示线性回归拟合优度的 R 平方值。

:::callout{theme="success" title="另请参阅(See Also)"} exponential_regression(), polynomial_regression() :::

:::callout{theme="warning" title="注意(Note)"} 此函数仅适用于数值型序列。 :::

示例(Examples)

>>> series = F.points(
...     (10, 6.0), (20, 12.0), (30, 24.0), (40, 48.0), (50, 96.0), name="series"
... )
>>> series.to_pandas()
                      timestamp  value
0 1970-01-01 00:00:00.000000010    6.0
1 1970-01-01 00:00:00.000000020   12.0
2 1970-01-01 00:00:00.000000030   24.0
3 1970-01-01 00:00:00.000000040   48.0
4 1970-01-01 00:00:00.000000050   96.0
>>> lin_regr = F.linear_regression()(series)
>>> lin_regr.to_pandas()
   max_bounds.first_value  max_bounds.second_value  min_bounds.first_value  min_bounds.second_value  regression_fit_function.linear_regression_fit.intercept  regression_fit_function.linear_regression_fit.slope  regression_fit_function.linear_regression_fit.statistics.rsquared
0                    50.0                     96.0                    10.0                      6.0                                              -27.6                                                     2.16                                             0.870968