Correlation Of Vectors Contained In Nested Dictionaries
I have a nested dict with the next structure:
{Cell_name_1 : {KPI_name_1: [value1, value2, ..., valueN],
KPI_name_2: [value1, value2, ..., valueN],
...,
KPI_name_N: [value1, value2, ..., valueN]},
Cell_name_2 : {KPI_name_1: [value1, value2, ..., valueN], ...},
Cell_name_N : {....}}
I want to check the correlation (I have this method defined already, so it's an auxiliaryfunction) between the vectos contained in the different cells. Let's say:
vector_1 = [64.0, 66.0, 53.5, 52.1, 54.0] #[values from KPI_name_1 from Cell_name_1]
vector_2 = [84.0, 86.0, 63.5, 72.1, 24.0] #[values from KPI_name_2 from Cell_name_2]
correlation(vector_1, vector_2)
I have tried different ways of looping over the dictionaries (normal for loops, classic loops with while and conditions, etc), but I do not find the way to get what I need.
As an example, the code is something like this:
dic_sem = {'16895555': {'KPI_name_1': [64.0, 66.0, 53.5, 52.1, 54.0],
'KPI_name_2': [54.0, 56.0, 23.5, 32.1, 84.0]},
'16894444': {'KPI_name_1': [84.0, 86.0, 63.5, 72.1, 24.0],
'KPI_name_2': [24.0, 26.0, 63.5, 92.1, 84.0]}}
'16895555'
and '16894444'
are the different Cell_name's
.
Answer
You can iterate over the dictionary and create a dictionary of the cellnames e.g. KPI_name_1
to a list of lists which contains your vectors
from collections import defaultdict
vectors = defaultdict(list)
#Iterate over the values
for value in dic_sem.values():
#Create your vectors dictionary
for k, v in value.items():
vectors[k].append(v)
print(dict(vectors))
The output will be
{'KPI_name_1': [[64.0, 66.0, 53.5, 52.1, 54.0], [84.0, 86.0, 63.5, 72.1, 24.0]],
'KPI_name_2': [[54.0, 56.0, 23.5, 32.1, 84.0], [24.0, 26.0, 63.5, 92.1, 84.0]]}
You can then iterate over the values of this dictionary and call correlation
accordingly
for value in vectors.values():
print(value[0], value[1])
#correlation(*value)
The output here will be
[64.0, 66.0, 53.5, 52.1, 54.0] [84.0, 86.0, 63.5, 72.1, 24.0]
[54.0, 56.0, 23.5, 32.1, 84.0] [24.0, 26.0, 63.5, 92.1, 84.0]
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module