Sdílet prostřednictvím


series_fit_poly_fl()

Funkce series_fit_poly_fl() je uživatelem definovaná funkce( UDF), která používá polynomické regrese na řadu. Tato funkce přebírá tabulku obsahující více řad (dynamická číselná pole) a pomocí polynomické regrese vygeneruje pro každou řadu polynomický polynom nejvhodnější polynom. Tato funkce vrací jak polynomické koeficienty, tak interpolovaný polynom v rozsahu řady.

Poznámka

  • Místo funkce popsané v tomto dokumentu použijte nativní funkci series_fit_poly( ). Nativní funkce poskytuje stejné funkce a je lepší z hlediska výkonu a škálovatelnosti. Tento dokument je k dispozici pouze pro referenční účely.
  • Pro lineární regresi rovnoměrně rozmístěné řady vytvořené operátorem make-series použijte nativní funkci series_fit_line().

Požadavky

  • Modul plug-in Pythonu musí být v clusteru povolený. To se vyžaduje pro vložený Python použitý ve funkci .
  • V databázi musí být povolený modul plug-in Pythonu. To se vyžaduje pro vložený Python použitý ve funkci .

Syntax

T | invoke series_fit_poly_fl(, y_series, y_fit_series, fit_coeffStupeň, [ x_series ], [ x_istime ])

Přečtěte si další informace o konvencích syntaxe.

Parametry

Název Typ Vyžadováno Popis
y_series string ✔️ Název sloupce vstupní tabulky, který obsahuje závislou proměnnou. To znamená, že řada se vejde.
y_fit_series string ✔️ Název sloupce pro uložení nejvhodnější řady.
fit_coeff string ✔️ Název sloupce pro uložení nejlepších polynomních koeficientů.
Stupeň int ✔️ Požadované pořadí polynomu k přizpůsobení. Například 1 pro lineární regresi, 2 pro kvadratickou regresi atd.
x_series string Název sloupce obsahujícího nezávislou proměnnou, tj. x nebo časovou osu. Tento parametr je volitelný a vyžaduje se pouze pro nerovnoměrně rozmístěné řady. Výchozí hodnota je prázdný řetězec, protože x je redundantní pro regresi rovnoměrně rozmístěné řady.
x_istime bool Tento parametr je potřebný jenom v případě, že je zadaný x_series a jedná se o vektor datetime.

Definice funkce

Funkci můžete definovat vložením jejího kódu jako funkce definované dotazem nebo jejím vytvořením jako uložené funkce v databázi, a to následujícím způsobem:

Definujte funkci pomocí následujícího příkazu let. Nejsou vyžadována žádná oprávnění.

Důležité

Příkaz let nelze spustit samostatně. Musí za ním následovat příkaz tabulkového výrazu. Pokud chcete spustit funkční příklad , podívejte se na series_fit_poly_fl()příklady.

let series_fit_poly_fl=(tbl:(*), y_series:string, y_fit_series:string, fit_coeff:string, degree:int, x_series:string='', x_istime:bool=False)
{
    let kwargs = bag_pack('y_series', y_series, 'y_fit_series', y_fit_series, 'fit_coeff', fit_coeff, 'degree', degree, 'x_series', x_series, 'x_istime', x_istime);
    let code = ```if 1:
        y_series = kargs["y_series"]
        y_fit_series = kargs["y_fit_series"]
        fit_coeff = kargs["fit_coeff"]
        degree = kargs["degree"]
        x_series = kargs["x_series"]
        x_istime = kargs["x_istime"]

        def fit(ts_row, x_col, y_col, deg):
            y = ts_row[y_col]
            if x_col == "": # If there is no x column creates sequential range [1, len(y)]
               x = np.arange(len(y)) + 1
            else: # if x column exists check whether its a time column. If so, normalize it to the [1, len(y)] range, else take it as is.
               if x_istime: 
                   x = pd.to_numeric(pd.to_datetime(ts_row[x_col]))
                   x = x - x.min()
                   x = x / x.max()
                   x = x * (len(x) - 1) + 1
               else:
                   x = ts_row[x_col]
            coeff = np.polyfit(x, y, deg)
            p = np.poly1d(coeff)
            z = p(x)
            return z, coeff

        result = df
        if len(df):
           result[[y_fit_series, fit_coeff]] = df.apply(fit, axis=1, args=(x_series, y_series, degree,), result_type="expand")
    ```;
    tbl
     | evaluate python(typeof(*), code, kwargs)
};
// Write your query to use the function here.

Příklady

V následujících příkladech se ke spuštění funkce používá operátor invoke .

Přizpůsobení polynomu pátého řádu na běžnou časovou řadu

Pokud chcete použít funkci definovanou dotazem, vyvolejte ji po definici vložené funkce.

let series_fit_poly_fl=(tbl:(*), y_series:string, y_fit_series:string, fit_coeff:string, degree:int, x_series:string='', x_istime:bool=False)
{
    let kwargs = bag_pack('y_series', y_series, 'y_fit_series', y_fit_series, 'fit_coeff', fit_coeff, 'degree', degree, 'x_series', x_series, 'x_istime', x_istime);
    let code = ```if 1:
        y_series = kargs["y_series"]
        y_fit_series = kargs["y_fit_series"]
        fit_coeff = kargs["fit_coeff"]
        degree = kargs["degree"]
        x_series = kargs["x_series"]
        x_istime = kargs["x_istime"]

        def fit(ts_row, x_col, y_col, deg):
            y = ts_row[y_col]
            if x_col == "": # If there is no x column creates sequential range [1, len(y)]
               x = np.arange(len(y)) + 1
            else: # if x column exists check whether its a time column. If so, normalize it to the [1, len(y)] range, else take it as is.
               if x_istime: 
                   x = pd.to_numeric(pd.to_datetime(ts_row[x_col]))
                   x = x - x.min()
                   x = x / x.max()
                   x = x * (len(x) - 1) + 1
               else:
                   x = ts_row[x_col]
            coeff = np.polyfit(x, y, deg)
            p = np.poly1d(coeff)
            z = p(x)
            return z, coeff

        result = df
        if len(df):
           result[[y_fit_series, fit_coeff]] = df.apply(fit, axis=1, args=(x_series, y_series, degree,), result_type="expand")
    ```;
    tbl
     | evaluate python(typeof(*), code, kwargs)
};
//
// Fit fifth order polynomial to a regular (evenly spaced) time series, created with make-series
//
let max_t = datetime(2016-09-03);
demo_make_series1
| make-series num=count() on TimeStamp from max_t-1d to max_t step 5m by OsVer
| extend fnum = dynamic(null), coeff=dynamic(null), fnum1 = dynamic(null), coeff1=dynamic(null)
| invoke series_fit_poly_fl('num', 'fnum', 'coeff', 5)
| render timechart with(ycolumns=num, fnum)

Výstup

Graf znázorňující přizpůsobení polynomu pátého řádu na běžnou časovou řadu

Test nepravidelných časových řad

Pokud chcete použít funkci definovanou dotazem, vyvolejte ji po definici vložené funkce.

let series_fit_poly_fl=(tbl:(*), y_series:string, y_fit_series:string, fit_coeff:string, degree:int, x_series:string='', x_istime:bool=False)
{
    let kwargs = bag_pack('y_series', y_series, 'y_fit_series', y_fit_series, 'fit_coeff', fit_coeff, 'degree', degree, 'x_series', x_series, 'x_istime', x_istime);
    let code = ```if 1:
        y_series = kargs["y_series"]
        y_fit_series = kargs["y_fit_series"]
        fit_coeff = kargs["fit_coeff"]
        degree = kargs["degree"]
        x_series = kargs["x_series"]
        x_istime = kargs["x_istime"]

        def fit(ts_row, x_col, y_col, deg):
            y = ts_row[y_col]
            if x_col == "": # If there is no x column creates sequential range [1, len(y)]
               x = np.arange(len(y)) + 1
            else: # if x column exists check whether its a time column. If so, normalize it to the [1, len(y)] range, else take it as is.
               if x_istime: 
                   x = pd.to_numeric(pd.to_datetime(ts_row[x_col]))
                   x = x - x.min()
                   x = x / x.max()
                   x = x * (len(x) - 1) + 1
               else:
                   x = ts_row[x_col]
            coeff = np.polyfit(x, y, deg)
            p = np.poly1d(coeff)
            z = p(x)
            return z, coeff

        result = df
        if len(df):
           result[[y_fit_series, fit_coeff]] = df.apply(fit, axis=1, args=(x_series, y_series, degree,), result_type="expand")
    ```;
    tbl
     | evaluate python(typeof(*), code, kwargs)
};
let max_t = datetime(2016-09-03);
demo_make_series1
| where TimeStamp between ((max_t-2d)..max_t)
| summarize num=count() by bin(TimeStamp, 5m), OsVer
| order by TimeStamp asc
| where hourofday(TimeStamp) % 6 != 0   //  delete every 6th hour to create unevenly spaced time series
| summarize TimeStamp=make_list(TimeStamp), num=make_list(num) by OsVer
| extend fnum = dynamic(null), coeff=dynamic(null)
| invoke series_fit_poly_fl('num', 'fnum', 'coeff', 8, 'TimeStamp', True)
| render timechart with(ycolumns=num, fnum)

Výstup

Graf znázorňující přizpůsobení polynomu osmého řádu nepravidelné časové řadě

Polynom pátého řádu s šumem na x osách & y

Pokud chcete použít funkci definovanou dotazem, vyvolejte ji po definici vložené funkce.

let series_fit_poly_fl=(tbl:(*), y_series:string, y_fit_series:string, fit_coeff:string, degree:int, x_series:string='', x_istime:bool=False)
{
    let kwargs = bag_pack('y_series', y_series, 'y_fit_series', y_fit_series, 'fit_coeff', fit_coeff, 'degree', degree, 'x_series', x_series, 'x_istime', x_istime);
    let code = ```if 1:
        y_series = kargs["y_series"]
        y_fit_series = kargs["y_fit_series"]
        fit_coeff = kargs["fit_coeff"]
        degree = kargs["degree"]
        x_series = kargs["x_series"]
        x_istime = kargs["x_istime"]

        def fit(ts_row, x_col, y_col, deg):
            y = ts_row[y_col]
            if x_col == "": # If there is no x column creates sequential range [1, len(y)]
               x = np.arange(len(y)) + 1
            else: # if x column exists check whether its a time column. If so, normalize it to the [1, len(y)] range, else take it as is.
               if x_istime: 
                   x = pd.to_numeric(pd.to_datetime(ts_row[x_col]))
                   x = x - x.min()
                   x = x / x.max()
                   x = x * (len(x) - 1) + 1
               else:
                   x = ts_row[x_col]
            coeff = np.polyfit(x, y, deg)
            p = np.poly1d(coeff)
            z = p(x)
            return z, coeff

        result = df
        if len(df):
           result[[y_fit_series, fit_coeff]] = df.apply(fit, axis=1, args=(x_series, y_series, degree,), result_type="expand")
    ```;
    tbl
     | evaluate python(typeof(*), code, kwargs)
};
range x from 1 to 200 step 1
| project x = rand()*5 - 2.3
| extend y = pow(x, 5)-8*pow(x, 3)+10*x+6
| extend y = y + (rand() - 0.5)*0.5*y
| summarize x=make_list(x), y=make_list(y)
| extend y_fit = dynamic(null), coeff=dynamic(null)
| invoke series_fit_poly_fl('y', 'y_fit', 'coeff', 5, 'x')
|fork (project-away coeff) (project coeff | mv-expand coeff)
| render linechart

Výstup

Graf fitu polynomu pátého řádu s šumem na x osách & y

Koeficienty fit polynomu pátého řádu s šumem.

Tato funkce není podporovaná.