1

I am attempting to apply statistical tests to some datasets with variable numbers of groups. This causes a problem when I try to perform a log transformation for said groups while maintaining the ability to perform the test function (in this case scipy's kruskal()), which takes a variable number of arguments, one for each group of data.

The code below is an idea of what I want. Naturally stats.kruskal([np.log(i) for i in args]) does not work, as kruskal() does not expect a list of arrays, but one argument for each array. How do I perform log transformation (or any kind of alteration, really), while still being able to use the function?

import scipy.stats as stats
import numpy as np


def t(*args):
    test = stats.kruskal([np.log(i) for i in args])
    return test


a = [11, 12, 4, 42, 12, 1, 21, 12, 6]
b = [1, 12, 4, 3, 14, 8, 8, 6]
c = [2, 2,  3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8]

print(t(a, b, c))
Tim Stack
  • 3,209
  • 3
  • 18
  • 39

1 Answers1

2

IIUC, * in front of the list you are forming while calling kruskal should do the trick:

test = stats.kruskal(*[np.log(i) for i in args])

Asterisk unpacks the list and passes each entry of the list as arguments to the function being called i.e. kruskal here.

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38