Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create an automized heatmap given data lists and function

Given thre lists, e.g.

a = [0.4, 0.6, 0.8]
b = [0.3, 0.2, 0.5]
c = [0.1, 0.6, 0.12]

I want to generate a confusion matrix, which essentially applies a function (e.g. the correlation) between each of the combinations of the lists.

Essentially the calculations then look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

confusion_matrix = np.array([
    [1,
    scipy.stats.pearsonr(a, b)[0],
    scipy.stats.pearsonr(a, c)[0]],
    
    [scipy.stats.pearsonr(b, a)[0],
    1,
    scipy.stats.pearsonr(b, c)[0]],
    
    [scipy.stats.pearsonr(c, a)[0],
    scipy.stats.pearsonr(c, b)[0],
    1]
])

Does a Python function/ or a library exist, which is capable of generating such a matrix automatically, without me putting this manually together & maybe also directly generates an heatmap out of this?

>Solution :

You can write a list comprehension:

import numpy as np
from scipy.stats import pearsonr
from itertools import product

matrix = [a, b, c]
np.array([
    [1 if i1 == i2 else pearsonr(matrix[i1], matrix[i2])[0]
    for i2 in range(len(a))] for i1 in range(len(a))
])

This outputs:

[[ 1.          0.65465367  0.03532591]
 [ 0.65465367  1.         -0.73233089]
 [ 0.03532591 -0.73233089  1.        ]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading