Home > Mobile >  Create an automized heatmap given data lists and function
Create an automized heatmap given data lists and function

Time:06-19

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:

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?

CodePudding user response:

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.        ]]
  • Related