TypeError:'numpy.float64'对象不能用spearmanr_coefficient调用

时间:2018-04-08 19:40:04

标签: python pandas numpy scipy

我正在尝试打印两个变量的Spearmanr系数。

drat = cars['drat']
carb = cars['carb']
spearmanr_coefficient, p_value = spearmanr(drat, carb)
print('spearman is %0.3f' % (spearmanr_coefficient))

drat和carb变量都是'float64'类型。

这是错误......

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-f64604af9a3c> in <module>()
      1 drat = cars['drat']
      2 carb = cars['carb']
----> 3 spearmanr_coefficient, p_value = spearmanr(drat, carb)
      4 print('spearman is %0.3f' % (spearmanr_coefficient))

TypeError: 'numpy.float64' object is not callable

我可以将变量转换为整数吗?这里有另一种解决方案吗?

我尝试过使用.astype(int).astype(np.float32)但没有运气。

Scipy版本:1.0.0

Numpy版本:1.14.0

每个变量的前10行(下)。

讨厌鬼

0     3.90
1     3.90
2     3.85
3     3.08
4     3.15
5     2.76
6     3.21
7     3.69
8     3.92
9     3.92
10    3.92

碳水化合物

0     4
1     4
2     1
3     1
4     2
5     1
6     4
7     2
8     2
9     4
10    4

1 个答案:

答案 0 :(得分:0)

我只是尝试,但无法重现您的错误。 你能发送一份数据样本吗? 谢谢!

from scipy import stats
import numpy as np
import pandas as pd
cars=pd.DataFrame({'drat':[1.2,2,3,4,5],'carb':[5,6.5,7,8,7]})
drat = cars['drat']
carb = cars['carb']
print drat.dtype, carb.dtype
spearmanr_coefficient, p_value =stats.spearmanr(drat, carb)
print('spearman is %0.3f' % (spearmanr_coefficient))

输出:

float64 float64
spearman is 0.821

更新数据:

cars=pd.DataFrame({'drat':[3.9,3.9,3.85,3.08,3.15,2.76,3.21,3.69,3.92,3.92,3.92],carb':[4,4,1,1,2,1,4,2,2,4,4]})

输出:

float64 int64
spearman is 0.620