-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanicor.py
More file actions
33 lines (23 loc) · 762 Bytes
/
anicor.py
File metadata and controls
33 lines (23 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 12 10:13:21 2018
@author: Anibal Solon
"""
import numpy as np
def zscore(data, axis):
data -= data.mean(axis=axis, keepdims=True)
data /= data.std(axis=axis, keepdims=True)
return np.nan_to_num(data, copy=False)
def correlation(matrix1, matrix2):
d1 = matrix1.shape[-1]
d2 = matrix2.shape[-1]
assert d1 == d2
assert matrix1.ndim <= 2
assert matrix2.ndim <= 2
matrix1 = zscore(matrix1.astype(float), matrix1.ndim - 1) / np.sqrt(d1)
matrix2 = zscore(matrix2.astype(float), matrix2.ndim - 1) / np.sqrt(d2)
if matrix1.ndim >= matrix2.ndim:
return np.dot(matrix1, matrix2.T)
else:
return np.dot(matrix2, matrix1.T)