«<» не поддерживается между экземплярами «float» и «str» Ошибка для теста Tukey HSD - PullRequest
0 голосов
/ 03 октября 2018

При запуске теста Тьюки я получаю странную ошибку.Я надеюсь, что кто-то сможет помочь мне с этим, поскольку я много пытался.Это мой фрейм данных:

    Name      Score
1   A         2.29
2   B         2.19  

Это мой тестовый код Тьюки:

#TUKEY HSD TEST

tukey = pairwise_tukeyhsd(endog=df['Score'].astype('float'),     
                          groups=df['Name'],                     
                          alpha=0.05)                          

tukey.plot_simultaneous()                                      
plt.vlines(x=49.57,ymin=-0.5,ymax=4.5, color="red")

tukey.summary() 

Это ошибка:

<ipython-input-12-3e12e78a002f> in <module>()
      2 tukey = pairwise_tukeyhsd(endog=df['Score'].astype('float'),     
      3                           groups=df['Name'],
----> 4                           alpha=0.05)                          
      5 
      6 tukey.plot_simultaneous()

/usr/local/lib/python3.6/dist-packages/statsmodels/stats/multicomp.py in pairwise_tukeyhsd(endog, groups, alpha)
     36     '''
     37 
---> 38     return MultiComparison(endog, groups).tukeyhsd(alpha=alpha)

/usr/local/lib/python3.6/dist-packages/statsmodels/sandbox/stats/multicomp.py in __init__(self, data, groups, group_order)
    794         if group_order is None:
    795             self.groupsunique, self.groupintlab = np.unique(groups,
--> 796                                                             return_inverse=True)
    797         else:
    798             #check if group_order has any names not in groups

/usr/local/lib/python3.6/dist-packages/numpy/lib/arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    221     ar = np.asanyarray(ar)
    222     if axis is None:
--> 223         return _unique1d(ar, return_index, return_inverse, return_counts)
    224     if not (-ar.ndim <= axis < ar.ndim):
    225         raise ValueError('Invalid axis kwarg specified for unique')

/usr/local/lib/python3.6/dist-packages/numpy/lib/arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    278 
    279     if optional_indices:
--> 280         perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
    281         aux = ar[perm]
    282     else:

**TypeError: '<' not supported between instances of 'float' and 'str'**

Как можно устранить эту ошибку?Заранее спасибо!

1 Ответ

0 голосов
/ 03 октября 2018

У вас проблема, потому что df['Name'] содержит как числа с плавающей запятой, так и строки И df['Name'] имеет тип pandas.core.series.Series.Эта комбинация приводит к ошибке с numpy.unique(), как видно из трассировки.Устранить проблему можно двумя способами.

tukey = pairwise_tukeyhsd(endog=df['Score'].astype('float'),
                          groups=list(df['Name']),  # list instead of a Series
                          alpha=0.05)

ИЛИ

Убедитесь, что df['Name'] содержит только цифры или только строки.

...