Search code examples
pythonscipycorrelationscipy-spatial

Issue : Correlation always gives nan values


I am a newbie in python and trying to execute the below code :

from scipy.spatial.distance import correlation
u1=np.array([10])
u2=np.array([20])
correlation(u1,u2)

But I am getting nan, why?

RuntimeWarning: invalid value encountered in double_scalars
dist = 1.0 - np.dot(um, vm) / (norm(um) * norm(vm))
output : nan

Please help me with this.


Solution

  • I think you misinterpreted the concept of correlation.

    Correlation indicates the extent to which two or more variables fluctuate together.

    You are passing only single value so it won't be able to determine correlation. You need to pass lists with multiple values to be abled to find correlation between them.

    from scipy.spatial.distance import correlation
    
    u1=np.array([10,14,17])
    u2=np.array([20,18,12])
    
    print(correlation(u1,u2))
    

    Output:

    1.934719542804484