Arslan
Arslan

Reputation: 37

How to perform Mann-Whitney U test in python with cycle?

I have a loop that gives new values k1 and k2 each time, but the problem is that in my dataset there are cases where all values are zero in both k1 and k2. When the program comes to them, it just throws an error and does not complete the loop, and there is still a lot of calculations. How can I make such cases just be signed, like NA or something else, and the cycle goes on?

python3 

import pandas

from scipy.stats import mannwhitneyu

print(mannwhitneyu(k1, k2))

I conduct this Mann-Whitney U test for different observations and I want the cycle not to stop at an error, but simply to note that it is impossible here

Error example(line 3, above are normal):

MannwhitneyuResult(statistic=3240.0, pvalue=0.16166098643677973)
MannwhitneyuResult(statistic=2958.5, pvalue=0.008850960706454409)
Traceback (most recent call last):
  File "ars1", line 95, in <module>
    print(mannwhitneyu(k1, k2))
  File "/storage/software/python-3.6.0/lib/python3.6/site-packages/scipy/stats/stats.py", line 4883, in mannwhitneyu
    raise ValueError('All numbers are identical in mannwhitneyu')
ValueError: All numbers are identical in mannwhitneyu

Upvotes: 3

Views: 5185

Answers (2)

Ashmit Ayush
Ashmit Ayush

Reputation: 31

  • I tried it in loop and also saved it in csv file in a folder

  • convert your series in the list and check for the equality it will work

      for y in df.columns:
          target = df[y]
          list_mann_white = []
    
    
          for x in df.columns:
              if list(target) == list(df[x]):
                  pass
              else:
                  list_mann_white.append([stats.mannwhitneyu(target,df[x])[1],x])
    
          list_mann_white.sort()
          mann_csv = pd.DataFrame(chi_list)
          mann_csv.to_csv('Mann/target_{}.csv'.format(y))
    

Upvotes: 0

msi_gerva
msi_gerva

Reputation: 2078

You can continue with loop if 2 arrays are equal. For instance, if:

k1 = [0,0,0,0,0];
k2 = [0,0,0,0,0];

then you can check whether k1 == k2. If it is true, just use continue for your loop. Like this:

if ( k1 == k2 ) == True: continue

just before you call mannwhitneyu(k1, k2)

Upvotes: 2

Related Questions