import numpy as np

def pearson_affinity(M):
    return 1 - np.array([[pearsonr(a, b)[0] for a in M] for b in M])
How to use Pearson Correlation as distance metric in Scikit-learn Agglomerative clustering