# -*- coding: utf-8 -*- """ Created on Mon Dec 20 16:53:39 2021 modified: https://gist.github.com/fabianp/9396204419c7b638d38f @author: pan """
import numpy as np from numpy.linalg import inv from osgeo import gdal, gdal_array import os, time,glob from sklearn import linear_model from sklearn import preprocessing from matplotlib import pyplot as plt
defpartial_corr(C): """ Returns the sample linear partial correlation coefficients between pairs of variables in C, controlling for the remaining variables in C. Parameters ---------- C : array-like, shape (n, p) Array with the different variables. Each column of C is taken as a variable Returns ------- P_corr : array-like, shape (p, p) P_corr[i, j] contains the partial correlation of C[:, i] and C[:, j] controlling for the remaining variables in C. """ C = np.asarray(C) p = C.shape[1] P_corr = np.zeros((p, p)) # sample linear partial correlation coefficients corr = np.corrcoef(C,rowvar=False) # Pearson product-moment correlation coefficients. corr_inv = inv(corr) # the (multiplicative) inverse of a matrix. for i inrange(p): P_corr[i, i] = 1 for j inrange(i+1, p): pcorr_ij = -corr_inv[i,j]/(np.sqrt(corr_inv[i,i]*corr_inv[j,j])) P_corr[i,j]=pcorr_ij P_corr[j,i]=pcorr_ij return P_corr