计算Spearmanr系数时遇TypeError:numpy.float64对象不可调用
Hey there! Let's figure out why you're hitting this error and get your Spearman correlation calculation working smoothly.
What's Going Wrong?
That error pops up because you've accidentally overwritten the spearmanr function (I'm guessing you imported it from scipy.stats) with a numpy float variable that has the exact same name. When you try to run spearmanr(drat, carb), Python thinks you're trying to treat a plain number like a function—which doesn't make sense, hence the "not callable" message.
How to Fix It
Here's what you need to do:
Hunt for variable name conflicts
Scroll back through your code to see if you have a line like this somewhere before the calculation:spearmanr = some_float_value # This replaces the function with a number!If you find it, rename that variable to something unique—like
spearmanr_prevormy_spearman_score.Re-import the function (if needed)
If you've already overwritten the function in your current notebook/session, re-import it to get the original function reference back:from scipy.stats import spearmanrRun your original code again
Now your calculation should work without issues:drat = cars['drat'] carb = cars['carb'] spearmanr_coefficient, p_value = spearmanr(drat, carb) print('spearman is %0.3f' % (spearmanr_coefficient))
A Quick Example of the Conflict
Let's say you had this line earlier in your code:
# Oops! This overwrites the spearmanr function spearmanr = 0.621
When you later try to call spearmanr(drat, carb), Python sees spearmanr as the float 0.621 instead of the function you intended to use—hence the TypeError.
内容的提问来源于stack exchange,提问作者Josh Bennett

