Share via


binomial_test_fl()

La funzione è una funzione UDF (funzione definita dall'utente) che esegue il test binomiale.binomial_test_fl()

Prerequisiti

  • Il plug-in Python deve essere abilitato nel cluster. Questa operazione è necessaria per Python inline usata nella funzione.
  • Il plug-in Python deve essere abilitato nel database. Questa operazione è necessaria per Python inline usata nella funzione.

Sintassi

T | invoke binomial_test_fl(Successi,versioni di valutazione [success_prob [,,alt_hypotheis ]])

Altre informazioni sulle convenzioni di sintassi.

Parametri

Nome Tipo Obbligatoria Descrizione
Successi string ✔️ Nome della colonna contenente il numero di risultati riusciti.
Prove string ✔️ Nome della colonna contenente il numero totale di versioni di valutazione.
p_value string ✔️ Nome della colonna per archiviare i risultati.
success_prob real Probabilità di successo. Il valore predefinito è 0.5.
alt_hypotheis string L'ipotesi alternativa può essere two-sided, greatero less. Il valore predefinito è two-sided.

Definizione di funzione

È possibile definire la funzione incorporando il codice come funzione definita da query o creandola come funzione archiviata nel database, come segue:

Definire la funzione usando l'istruzione let seguente. Non sono necessarie autorizzazioni.

Importante

Un'istruzione let non può essere eseguita autonomamente. Deve essere seguito da un'istruzione espressione tabulare. Per eseguire un esempio funzionante di binomial_test_fl(), vedere Esempio.

let binomial_test_fl = (tbl:(*), successes:string, trials:string, p_value:string, success_prob:real=0.5, alt_hypotheis:string='two-sided')
{
    let kwargs = bag_pack('successes', successes, 'trials', trials, 'p_value', p_value, 'success_prob', success_prob, 'alt_hypotheis', alt_hypotheis);
    let code = ```if 1:
        from scipy import stats

        successes = kargs["successes"]
        trials = kargs["trials"]
        p_value = kargs["p_value"]
        success_prob = kargs["success_prob"]
        alt_hypotheis = kargs["alt_hypotheis"]

        def func(row, prob, h1):
            pv = stats.binom_test(row[successes], row[trials], p=prob, alternative=h1)
            return pv
        result = df
        result[p_value] = df.apply(func, axis=1, args=(success_prob, alt_hypotheis), result_type="expand")
    ```;
    tbl
    | evaluate python(typeof(*), code, kwargs)
};
// Write your query to use the function here.

Esempio

Nell'esempio seguente viene usato l'operatore invoke per eseguire la funzione.

Per usare una funzione definita da query, richiamarla dopo la definizione di funzione incorporata.

let binomial_test_fl = (tbl:(*), successes:string, trials:string, p_value:string, success_prob:real=0.5, alt_hypotheis:string='two-sided')
{
    let kwargs = bag_pack('successes', successes, 'trials', trials, 'p_value', p_value, 'success_prob', success_prob, 'alt_hypotheis', alt_hypotheis);
    let code = ```if 1:
        from scipy import stats

        successes = kargs["successes"]
        trials = kargs["trials"]
        p_value = kargs["p_value"]
        success_prob = kargs["success_prob"]
        alt_hypotheis = kargs["alt_hypotheis"]

        def func(row, prob, h1):
            pv = stats.binom_test(row[successes], row[trials], p=prob, alternative=h1)
            return pv
        result = df
        result[p_value] = df.apply(func, axis=1, args=(success_prob, alt_hypotheis), result_type="expand")
    ```;
    tbl
    | evaluate python(typeof(*), code, kwargs)
};
datatable(id:string, x:int, n:int) [
'Test #1', 3, 5,
'Test #2', 5, 5,
'Test #3', 3, 15
]
| extend p_val=0.0
| invoke binomial_test_fl('x', 'n', 'p_val', success_prob=0.2, alt_hypotheis='greater')

Output

id x n p_val
Test #1 3 5 0.05792
Test #2 5 5 0.00032
Test #3 3 15 0.601976790745087

Questa funzionalità non è supportata.