Home > Software engineering >  np.corrcoef returns only nan
np.corrcoef returns only nan

Time:10-12

i have an array

norm_array=np.array([[1, 133, 1, 5.73, 5.09, 11.12, 10.16, 3.38, 15, 3, 8, 7, 4, 5, 1,
    6, 1, 2, 18, 12, 48],
   [1, 185, 0, 4.34, 3.66, 18.23, 14.91, 0, 21, 15, 11, 2, 4, 4, 5,
    4, 9, 8, 27, 14, 47],
   [1, 133, 1, 5.92, 4.63, 9.66, 7.77, 7.18, 9, 20, 4, 11, 9, 1, 7,
    5, 3, 7, 29, 1, 45],
   [1, 165, 1, 4.26, 4.57, 16.05, 16.21, 8.81, 17, 17, 5, 10, 7, 7,
    6, 3, 8, 6, 29, 7, 40],
   [1, 48, 11, 7.13, 8.51, 15.43, 17.74, 11.49, 20, 14, 6, 12, 6, 7,
    6, 8, 8, 6, 39, 16, 38]])

i want to calculate a correlation, but np.corrcoef returns nan

cor=[]
f=norm_array[:, 0]
for j in range(norm_array.shape[1]):
      s=norm_array[:, j]
      cor.append(np.corrcoef(f.astype(np.float32), s.astype(np.float32))[0, 1])

screenshot

CodePudding user response:

You are computing the Pearson correlation between two vectors f and s. This needs the value for the standard deviation of both vectors. Since f consists of only ones, the standard deviation is 0 and because you need to divide by this value, it gives you nan's.

When printing f:

>>> f
array([1., 1., 1., 1., 1.])

Changing the first value of norm_array to for example 2 will give 'normal' results.

  • Related