def _generate_structure_K(self, X):
        lasso = GraphLassoCV(alphas=20)

        lasso.fit(X.T)
        K_structure = lasso.get_precision()

        if (hasattr(lasso, 'alpha_')):
            print('alpha=', lasso.alpha_)

        return K_structure
def computePartialCorrelationsCV(coupling_data):

    # standardize
    coupling_data -= coupling_data.mean(axis=0)
    coupling_data /= coupling_data.std(axis=0)


    estimator = GraphLassoCV(alphas=10)
    estimator.fit(coupling_data)
    prec = estimator.get_precision()
    reg_alpha = estimator.alpha_


    #### partial correlations: rho_ij = - p_ij/ sqrt(p_ii * p_jj)
    #diagonal of precision matrix
    prec_diag = np.diag(prec)
    partial_correlations = -prec / np.sqrt(np.outer(prec_diag, prec_diag))

    # set lower half to zero
    partial_correlations[np.tril_indices(400)] = 0

    return estimator.get_precision(), partial_correlations, reg_alpha
예제 #3
0
    def _generate_structure_K(self, X):
        # lasso = GraphLasso(alpha=0.012)
        lasso = GraphLassoCV(alphas=20)

        lasso.fit(X.T)
        K_structure = lasso.get_precision()

        if (hasattr(lasso, 'alpha_')):
            print('alpha=', lasso.alpha_)

        M = (np.abs(K_structure) > 1e-10)
        if (M == np.eye(M.shape[0], dtype=bool)).all():
            print('Got identity structure')
        # K_structure = np.ones(K_lasso.shape)

        return K_structure
def matrix_analysis(argv):
    x = pd.read_csv(argv[1], sep=',', header=None)
    features = x.iloc[:, 2:66].astype(np.float64)

    features = features.subtract(features.mean(axis=0), axis=1)
    features = features.divide(features.std(axis=0), axis=1)

    model = GraphLassoCV()
    model.fit(features)
    prec = model.get_precision()
    corr = features.corr()

    norm = SymLogNorm(linthresh=0.03, linscale=0.03, vmin=-1.0, vmax=1.0)

    cmap = plt.get_cmap('PuOr')

    plt.imshow(corr, cmap=cmap, norm=norm)
    plt.colorbar()
    plt.xticks([])
    plt.yticks([])

    plt.show()

    # plot precision matrix
    norm = SymLogNorm(linthresh=0.03, linscale=0.03, vmin=-1.0, vmax=1.0)

    cmap = plt.get_cmap('PuOr')

    plt.imshow(prec, cmap=cmap, norm=norm)
    plt.colorbar()
    plt.xticks([])
    plt.yticks([])

    plt.show()

    g = nx.Graph(prec)
    nx.draw_circular(g, with_labels=True)
    plt.show()

    ##########################################
    # Rearrange the colombs and plot again
    perm = [5, 6, 7, 8, 9]
    perm += [25, 26, 29, 30, 31, 34, 35, 36, 39]
    perm += [27, 28, 32, 33, 37, 38]
    perm += [61, 62, 63]
    perm += [20, 24, 40, 41]

    perm += [i for i in xrange(64) if i not in perm]
    features = features.iloc[:, perm]
    # print perm[27]
    model = GraphLassoCV()
    model.fit(features)
    prec = model.get_precision()
    corr = features.corr()

    #########cov
    features = features.iloc[:, perm]
    norm = SymLogNorm(linthresh=0.03, linscale=0.03, vmin=-1.0, vmax=1.0)

    cmap = plt.get_cmap('PuOr')

    plt.imshow(corr, cmap=cmap, norm=norm)
    plt.colorbar()
    plt.xticks([])
    plt.yticks([])
    plt.show()
    ############prec
    features = features.iloc[:, perm]
    norm = SymLogNorm(linthresh=0.03, linscale=0.03, vmin=-1.0, vmax=1.0)

    cmap = plt.get_cmap('PuOr')

    plt.imshow(prec, cmap=cmap, norm=norm)
    plt.colorbar()
    plt.xticks([])
    plt.show()