Correlation analysis using seaborn : TypeError: 'float' object cannot be interpreted as an integer

995 views Asked by At

I have a dataframe V of the following form:

       ECON1     ECON2     ECON3     FOOD1     FOOD2     FOOD3      ENV1  \
28  0.310071  0.096913  0.228500  0.234986  0.260894  0.267858  0.489309   
28  0.353609  0.045075  0.222571  0.222803  0.248388  0.330560  0.060107   
28  0.280600  0.170201  0.232027  0.226792  0.233379  0.316765  0.114550   
28  0.299062  0.127866  0.198080  0.189948  0.222982  0.327082  0.052881   
28  0.346291  0.645534  0.371397  0.389068  0.380557  0.386004  0.186583   

        ENV2      HEA1      HEA2      HEA3     PERS1     PERS2     PERS3  \
28  0.206320  0.252537  0.266968  0.248452  0.184450  0.093345  0.173952   
28 -0.206570  0.263673  0.126182  0.265908  0.134481  0.191341  0.113324   
28  0.237818  0.257337  0.102037  0.214423  0.159002  0.321451  0.165960   
28  0.345857  0.272412  0.069192  0.251301  0.130606  0.132732  0.174925   
28  0.372713  0.382155  0.373531  0.468293  0.364305  0.299510  0.350822   

        COM1      COM2      POL1      POL2  
28  0.781430  0.487822  0.361886  0.233124  
28  0.083918  0.005381  0.266604  0.237078  
28  0.395897  0.257888  0.330607  0.229079  
28  0.000000  0.000000  0.307907  0.238908  
28  0.188402  0.101147  0.410619  0.385933  

I am trying to do a correlation analysis as follows:

import seaborn as sb
corr = V.corr()
ax = sb.heatmap(
    corr, 
    vmin=-1, vmax=1, center=0,
    cmap=sb.diverging_palette(20, 220, n=200),
    square=True
)
ax.set_xticklabels(
    ax.get_xticklabels(),
    rotation=45,
    horizontalalignment='right'
);

I get an error as follows:

TypeError: 'float' object cannot be interpreted as an integer

Not sure how to interpret this error. Can someone comment on this please?

additional information on the correlation matrix

print(corr)
          ECON1     ECON2     ECON3     FOOD1     FOOD2     FOOD3      ENV1  \
ECON1  1.000000  0.341774  0.498779  0.522529  0.588714  0.503961 -0.074569   
ECON2  0.341774  1.000000  0.964742  0.951770  0.932538  0.785570  0.003878   
ECON3  0.498779  0.964742  1.000000  0.998125  0.983827  0.734790  0.104750   
FOOD1  0.522529  0.951770  0.998125  1.000000  0.991068  0.703027  0.153686   
FOOD2  0.588714  0.932538  0.983827  0.991068  1.000000  0.683437  0.192360   
FOOD3  0.503961  0.785570  0.734790  0.703027  0.683437  1.000000 -0.580854   
ENV1  -0.074569  0.003878  0.104750  0.153686  0.192360 -0.580854  1.000000   
ENV2  -0.475257  0.564601  0.369804  0.349462  0.328092  0.202113  0.183099   
HEA1   0.518089  0.971623  0.948098  0.938049  0.941533  0.857258 -0.087292   
HEA2   0.492331  0.760587  0.854620  0.882823  0.909930  0.320175  0.579590   
HEA3   0.637159  0.933654  0.949112  0.949667  0.970425  0.798348  0.014684   
PERS1  0.435383  0.965742  0.986036  0.988420  0.983967  0.656593  0.222572   
PERS2  0.006943  0.584600  0.574856  0.535135  0.424989  0.647732 -0.423388   
PERS3  0.278137  0.981872  0.931251  0.923784  0.918289  0.687020  0.134177   
COM1  -0.323141 -0.151890 -0.047608 -0.009682 -0.018765 -0.702210  0.913371   
COM2  -0.405013 -0.141136 -0.060505 -0.026537 -0.041302 -0.702745  0.897211   
POL1   0.024764  0.819855  0.797155  0.804438  0.792430  0.291577  0.526974   
POL2   0.534160  0.974790  0.970808  0.965523  0.969961  0.813307 -0.000965   

           ENV2      HEA1      HEA2      HEA3     PERS1     PERS2     PERS3  \
ECON1 -0.475257  0.518089  0.492331  0.637159  0.435383  0.006943  0.278137   
ECON2  0.564601  0.971623  0.760587  0.933654  0.965742  0.584600  0.981872   
ECON3  0.369804  0.948098  0.854620  0.949112  0.986036  0.574856  0.931251   
FOOD1  0.349462  0.938049  0.882823  0.949667  0.988420  0.535135  0.923784   
FOOD2  0.328092  0.941533  0.909930  0.970425  0.983967  0.424989  0.918289   
FOOD3  0.202113  0.857258  0.320175  0.798348  0.656593  0.647732  0.687020   
ENV1   0.183099 -0.087292  0.579590  0.014684  0.222572 -0.423388  0.134177   
ENV2   1.000000  0.440689  0.309514  0.335610  0.473824  0.161769  0.664619   
HEA1   0.440689  1.000000  0.738058  0.982913  0.938014  0.492595  0.945905   
HEA2   0.309514  0.738058  1.000000  0.811820  0.901376  0.157842  0.801182   
HEA3   0.335610  0.982913  0.811820  1.000000  0.942849  0.387558  0.915053   
PERS1  0.473824  0.938014  0.901376  0.942849  1.000000  0.472608  0.963744   
PERS2  0.161769  0.492595  0.157842  0.387558  0.472608  1.000000  0.447905   
PERS3  0.664619  0.945905  0.801182  0.915053  0.963744  0.447905  1.000000   
COM1   0.106662 -0.304509  0.362041 -0.239231  0.040733 -0.226292 -0.058492   
COM2   0.185397 -0.307438  0.332330 -0.257903  0.034155 -0.208413 -0.041872   
POL1   0.727061  0.707109  0.856765  0.695222  0.875352  0.297894  0.887691   
POL2   0.424788  0.995079  0.798982  0.989886  0.965114  0.480057  0.953595   

           COM1      COM2      POL1      POL2  
ECON1 -0.323141 -0.405013  0.024764  0.534160  
ECON2 -0.151890 -0.141136  0.819855  0.974790  
ECON3 -0.047608 -0.060505  0.797155  0.970808  
FOOD1 -0.009682 -0.026537  0.804438  0.965523  
FOOD2 -0.018765 -0.041302  0.792430  0.969961  
FOOD3 -0.702210 -0.702745  0.291577  0.813307  
ENV1   0.913371  0.897211  0.526974 -0.000965  
ENV2   0.106662  0.185397  0.727061  0.424788  
HEA1  -0.304509 -0.307438  0.707109  0.995079  
HEA2   0.362041  0.332330  0.856765  0.798982  
HEA3  -0.239231 -0.257903  0.695222  0.989886  
PERS1  0.040733  0.034155  0.875352  0.965114  
PERS2 -0.226292 -0.208413  0.297894  0.480057  
PERS3 -0.058492 -0.041872  0.887691  0.953595  
COM1   1.000000  0.995152  0.397261 -0.217775  
COM2   0.995152  1.000000  0.419501 -0.225367  
POL1   0.397261  0.419501  1.000000  0.748706  
POL2  -0.217775 -0.225367  0.748706  1.000000  
1

There are 1 answers

2
talismanbrandi On BEST ANSWER

You might want to look into this issue: https://github.com/mwaskom/seaborn/issues/1907

The issue seems to come from cmap=sns.diverging_palette(20, 220, n=200), You might need to update your seaborn installation

Works just fine for me.

import seaborn as sns  # sns version '0.11.0'

corr = df_P5p5.corr()
ax = sns.heatmap(
    corr, 
    vmin=-1, vmax=1, center=0,
    cmap=sns.diverging_palette(20, 220, n=200),
    square=True
)
ax.set_xticklabels(
    ax.get_xticklabels(),
    rotation=45,
    horizontalalignment='right'
);

Dataframe:

           absh_0    absh_p    absh_m  absh_0_1  absh_p_1  absh_m_1  \
599866   0.000177  0.000004  0.021056  0.000035  0.000013  1.484300   
427635   0.000309  0.000006  0.017254  0.000074  0.000021  1.881500   
1833852  0.000032  0.000005  0.016329  0.000006  0.000065  0.244173   
690821   0.000040  0.000035  0.030917  0.000064  0.000101  1.063950   
1098632  0.000327  0.000002  0.017160  0.000056  0.000016  0.005837   
...           ...       ...       ...       ...       ...       ...   
617761   0.000341  0.000007  0.009808  0.000061  0.000011  0.491265   
204467   0.000437  0.000021  0.012242  0.000019  0.000059  1.515220   
932372   0.000036  0.000022  0.035300  0.000028  0.000073  1.089760   
1584400  0.000023  0.000005  0.014236  0.000001  0.000059  1.662190   
139578   0.000212  0.000005  0.015321  0.000009  0.000087  0.741250   

             absh_p_2     absh_m_2;  CQe_2322  CLQ1_2223  P_5p_LQ5_LHCb  
599866   2.283040e-07  1.085160e-05 -0.162193   0.670895      -0.570265  
427635   2.789950e-08  1.895490e-05 -0.204701   0.742776      -0.577415  
1833852  3.995840e-06  7.339210e-06  0.658051   0.984142      -0.560692  
690821   1.061680e-05  2.352440e-05 -0.566117   0.734917      -0.622448  
1098632  1.566270e-06  2.679160e-05 -0.376097   0.649306      -0.585776  
...               ...           ...       ...        ...            ...  
617761   2.077150e-06  3.115670e-05  0.079413   0.568284      -0.494631  
204467   1.397560e-06  1.893320e-05  0.104801   0.618369      -0.565908  
932372   1.335230e-05  6.610640e-08  0.225752   1.086700      -0.622773  
1584400  1.609910e-05  3.062290e-06 -0.110411   0.635001      -0.560502  
139578   9.748530e-06  7.800600e-06 -0.122956   0.679417      -0.628816  

[100000 rows x 11 columns]

Correlation matrix

                absh_0    absh_p    absh_m  absh_0_1  absh_p_1  absh_m_1  \
absh_0         1.000000  0.034578  0.097363  0.095424 -0.091923  0.195620   
absh_p         0.034578  1.000000  0.403523 -0.001839  0.047511  0.136713   
absh_m         0.097363  0.403523  1.000000 -0.004896 -0.005719  0.340453   
absh_0_1       0.095424 -0.001839 -0.004896  1.000000  0.049618 -0.001182   
absh_p_1      -0.091923  0.047511 -0.005719  0.049618  1.000000 -0.069683   
absh_m_1       0.195620  0.136713  0.340453 -0.001182 -0.069683  1.000000   
absh_p_2      -0.050823  0.021416 -0.000820  0.180376  0.496774 -0.023613   
absh_m_2;      0.167619 -0.019075 -0.059183  0.246883 -0.032227 -0.219272   
CQe_2322      -0.103104 -0.071771 -0.153265 -0.106895  0.028045 -0.412171   
CLQ1_2223     -0.125011 -0.036544 -0.068685 -0.011385  0.030520 -0.253707   
P_5p_LQ5_LHCb  0.001508 -0.006292 -0.020996  0.163136  0.122048 -0.121965   

               absh_p_2  absh_m_2;  CQe_2322  CLQ1_2223  P_5p_LQ5_LHCb  
absh_0        -0.050823   0.167619 -0.103104  -0.125011       0.001508  
absh_p         0.021416  -0.019075 -0.071771  -0.036544      -0.006292  
absh_m        -0.000820  -0.059183 -0.153265  -0.068685      -0.020996  
absh_0_1       0.180376   0.246883 -0.106895  -0.011385       0.163136  
absh_p_1       0.496774  -0.032227  0.028045   0.030520       0.122048  
absh_m_1      -0.023613  -0.219272 -0.412171  -0.253707      -0.121965  
absh_p_2       1.000000  -0.099868 -0.020970   0.023178       0.274502  
absh_m_2;     -0.099868   1.000000 -0.194679  -0.109361       0.122653  
CQe_2322      -0.020970  -0.194679  1.000000   0.651148       0.006585  
CLQ1_2223      0.023178  -0.109361  0.651148   1.000000      -0.003935  
P_5p_LQ5_LHCb  0.274502   0.122653  0.006585  -0.003935       1.000000

Plot: enter image description here