Skip to content
Advertisement

How to slice and calculate the pearson correlation coefficient between one big and small array with “overlapping” windows arrays

Suppose I have two very simple arrays with numpy:

JavaScript

I would like to find which slice of array reference has the highest pearson’s correlation coefficient with array probe. To do that, I would like to slice the array reference using some sort of sub-arrays that are overlapped in a for loop, which means I shift one element at a time of reference, and compare it against array probe. I did the slicing using the non elegant code below:

JavaScript

I have one issues and one question about such a code:

1-once I run the code, I have the error below:

JavaScript

2- is there a more elegant way of doing such slicing with python?

Advertisement

Answer

You can use sliding_window_view to get the successive values, for a vectorized computation of the correlation, use a custom function:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement