def sanity_matrix2doutputExample(self): """ Sanity of matrix2doutput. """ from PyAstronomy import pyaC as PC import numpy as np m = np.zeros((4, 3)) colNames = ["first", "second", "third"] rowNames = ["1", "2", "3", "4"] for j in range(4): for i in range(3): m[j, i] = (i + 1) * 10**(j + 1) * np.sin(i * j) PC.matrix2doutput(m, colNames=colNames, rowNames=rowNames) print() PC.matrix2doutput(m, rowNames=rowNames) print() PC.matrix2doutput(m, colsep=" ") print() PC.matrix2doutput(m, oformat="% 12.5f", colNames=colNames, rowNames=rowNames) print() PC.matrix2doutput(m, oformat=["% 12.5f", "% 6.3f", "% e"], colNames=colNames)
def correlationMatrix(self, toScreen=True, method="pearson", parList=None, covariance=False): """ Calculates the correlation or covariance matrix. Parameters ---------- parList : list of strings, optional The list of parameters used in the calculation. If not given, all available parameters will be used. toScreen : boolean, optional If True, the result will be printed to stdout method : string, {"pearson", "spearman"} The correlation coefficient to be used. covariance : boolean, optional If True, the covariance will be returned instead of the correlation. The default is False. Returns ------- Parlist : list Parameter names in the order used in the calculation. Correlation matrix : 2d array The correlation matrix lines : list of strings Formatted version of the correlation matrix in the form of a list of strings. """ if parList is None: parList = self.availableParameters() corFunc = None if method == "pearson": corFunc = self.pearsonr if method == "spearman": corFunc = self.spearmanr if corFunc is None: raise(PE.PyAValError("The method "+str(method)+" is currently not supported.", \ solution="Change method argument e.g. to 'pearson'.")) for p in parList: self._parmCheck(p) if covariance: # The covariance is requested. In this case, the # correlation coefficient has to be multiplied by the # standard deviation(s). stds = {} for p in parList: stds[p] = self.std(p) # Calculate the matrix n = len(parList) matrix = np.zeros( (n, n) ) for i in xrange(n): for j in xrange(n): matrix[i, j] = corFunc(parList[i], parList[j])[0] if covariance: matrix[i, j] *= (stds[parList[i]] * stds[parList[j]]) # Format the output lines = PC.matrix2doutput(matrix, colNames=parList, rowNames=parList, toScreen=toScreen) return parList, matrix, lines
def correlationMatrix(self, toScreen=True, method="pearson", parList=None, covariance=False): """ Calculates the correlation or covariance matrix. Parameters ---------- parList : list of strings, optional The list of parameters used in the calculation. If not given, all available parameters will be used. toScreen : boolean, optional If True, the result will be printed to stdout method : string, {"pearson", "spearman"} The correlation coefficient to be used. covariance : boolean, optional If True, the covariance will be returned instead of the correlation. The default is False. Returns ------- Parlist : list Parameter names in the order used in the calculation. Correlation matrix : 2d array The correlation matrix lines : list of strings Formatted version of the correlation matrix in the form of a list of strings. """ if parList is None: parList = self.availableParameters() corFunc = None if method == "pearson": corFunc = self.pearsonr if method == "spearman": corFunc = self.spearmanr if corFunc is None: raise(PE.PyAValError("The method "+str(method)+" is currently not supported.", \ solution="Change method argument e.g. to 'pearson'.")) for p in parList: self._parmCheck(p) if covariance: # The covariance is requested. In this case, the # correlation coefficient has to be multiplied by the # standard deviation(s). stds = {} for p in parList: stds[p] = self.std(p) # Calculate the matrix n = len(parList) matrix = np.zeros( (n, n) ) for i in smo.range(n): for j in smo.range(n): matrix[i, j] = corFunc(parList[i], parList[j])[0] if covariance: matrix[i, j] *= (stds[parList[i]] * stds[parList[j]]) # Format the output lines = PC.matrix2doutput(matrix, colNames=parList, rowNames=parList, toScreen=toScreen) return parList, matrix, lines
def sanity_matrix2doutputExample(self): """ Sanity of matrix2doutput. """ from PyAstronomy import pyaC as PC import numpy as np m = np.zeros((4,3)) colNames = ["first", "second", "third"] rowNames = ["1", "2", "3", "4"] for j in range(4): for i in range(3): m[j,i] = (i+1) * 10**(j+1) * np.sin(i*j) PC.matrix2doutput(m, colNames=colNames, rowNames=rowNames) print() PC.matrix2doutput(m, rowNames=rowNames) print() PC.matrix2doutput(m, colsep=" ") print() PC.matrix2doutput(m, oformat="% 12.5f", colNames=colNames, rowNames=rowNames) print() PC.matrix2doutput(m, oformat=["% 12.5f", "% 6.3f", "% e"], colNames=colNames)