Ejemplo n.º 1
0
def MSE_compute_1_5(model, n, ITER):
    res1 = np.zeros(ITER)
    res2 = np.zeros(ITER)
    for j in range(ITER):
        random.seed(j)
        if model == 1:
            cov = [[1, 0.9], [0.9, 1]]
            beta = 0.9
            p_con, p_dis = 0.5, 0.5
            gt = -p_con * 0.5 * log(np.linalg.det(cov)) + p_dis * (
                log(2) + beta * log(beta) + (1 - beta) *
                log(1 - beta)) - p_con * log(p_con) - p_dis * log(p_dis)
            x_con, y_con = nr.multivariate_normal([0, 0], cov,
                                                  int(n * p_con)).T
            x_dis = nr.binomial(1, 0.5, int(n * p_dis))
            y_dis = (x_dis + nr.binomial(1, 1 - beta, int(n * p_dis))) % 2
            x_dis, y_dis = 2 * x_dis - np.ones(int(
                n * p_dis)), 2 * y_dis - np.ones(int(n * p_dis))
            x = np.concatenate((x_con, x_dis)).reshape((n, 1))
            y = np.concatenate((y_con, y_dis)).reshape((n, 1))
        if model == 2:
            m = 5
            gt = log(m) - 0.8 * log(2)
            x = nr.randint(m, size=n)
            z = nr.uniform(0, 2, size=n)
            y = x + z
        if model == 3:
            m = 5
            gt = 5 * (log(m) - 0.8 * log(2))
            x = nr.randint(m, size=5 * n)
            z = nr.uniform(0, 2, size=5 * n)
            y = x + z
            x = x.reshape((n, 5))
            y = y.reshape((n, 5))
        if model == 4:
            p = 0.15
            gt = (1 - p) * 0.3012
            x = nr.exponential(1, size=n)
            z = nr.binomial(1, 1 - p, size=n)
            w = nr.poisson(x, size=n)
            y = z * w
        if model == 5:
            p = 0
            gt = (1 - p) * 0.3012
            x = nr.exponential(1, size=n)
            z = nr.binomial(1, 1 - p, size=n)
            w = nr.poisson(x, size=n)
            y = z * w
        res1[j] = (mixed.Mixed_KSG(x, y) - gt) * (mixed.Mixed_KSG(x, y) - gt)
        res2[j] = (mixed.KSG(x, y) - gt) * (mixed.KSG(x, y) - gt)
    MSE_Mixed_KSG = np.mean(res1)
    MSE_copulaksg = np.mean(res2)
    return [MSE_Mixed_KSG, MSE_copulaksg]
Ejemplo n.º 2
0
def estimate_mi(X, y, label, est_H_Y, norm_factor):

    if label == "IRF":
        frac_eval = 0.3
        irf = CalibratedClassifierCV(
            base_estimator=RandomForestClassifier(n_estimators=60),
            method='isotonic',
            cv=5)
        # X_train, y_train, X_eval, y_eval = split_train_eval(X, y, frac_eval)
        X_train, X_eval, y_train, y_eval = train_test_split(
            X, y, test_size=frac_eval)
        irf.fit(X_train, y_train)
        p = irf.predict_proba(X_eval)
        return (est_H_Y - np.mean(entropy(p.T, base=np.exp(1)))) / norm_factor
    elif label == "UF":
        return (est_H_Y - uf(np.array(X), y)) / norm_factor
    elif label == "KSG":
        return mixed.KSG(X, y.reshape(-1, 1)) / norm_factor
    elif label == "Mixed KSG":
        return mixed.Mixed_KSG(X, y.reshape(-1, 1)) / norm_factor
    elif label == "UF2":
        return (est_H_Y - uf2(np.array(X), y)) / norm_factor
    elif label == "UF3":
        return UncertaintyForest().fit(X,
                                       y).estimate_mutual_info() / norm_factor
    else:
        raise ValueError("Unrecognized Label!")
Ejemplo n.º 3
0
def main():
	n = 1000
	cov = [[1,0.9],[0.9,1]]
	beta = 0.9
	p_con, p_dis = 0.5, 0.5
	gt = -p_con*0.5*log(np.linalg.det(cov))+p_dis*(log(2)+beta*log(beta)+(1-beta)*log(1-beta))-p_con*log(p_con)-p_dis*log(p_dis)

	x_con,y_con = nr.multivariate_normal([0,0],cov,int(n*p_con)).T
	x_dis = nr.binomial(1,0.5,int(n*p_dis))
	y_dis = (x_dis + nr.binomial(1,1-beta,int(n*p_dis)))%2
	x_dis, y_dis = 2*x_dis-np.ones(int(n*p_dis)), 2*y_dis-np.ones(int(n*p_dis))
	x = np.concatenate((x_con,x_dis)).reshape((n,1))
	y = np.concatenate((y_con,y_dis)).reshape((n,1))

	print "Ground Truth = ", gt
	print "Mixed KSG: I(X:Y) = ", mixed.Mixed_KSG(x,y)
	print "Partitioning: I(X:Y) = ", mixed.Partitioning(x,y)
	print "Noisy KSG: I(X:Y) = ", mixed.Noisy_KSG(x,y)
	print "KSG: I(X:Y) = ", mixed.KSG(x,y)
def get_MI_between_status_and_each_three_table(cli_table, gene_table):

    tumor_status = cli_table[1, ].astype(float)
    print(tumor_status.shape)
    gene_list = gene_table[:, 0:1]
    MI_list = []
    i = 0
    value_table = gene_table[1:, 1:]
    size = np.shape(value_table)
    print(size)
    row = size[0]
    column = size[1]
    # < column maybe wront
    while i < column:
        row_value = value_table[i].astype(float)
        ''' HERE IS THE CHANGE and different from the previous code'''
        mi_score = mixed.Mixed_KSG(row_value, tumor_status, k=2)
        MI_list.append(mi_score)
        i += 1
        ''' ========================================================='''
    print('Finish MI score.')
    return gene_list, MI_list
Ejemplo n.º 5
0
def main(sample_num, node_num, d, method='KSG'):

    mat = np.zeros((trans, all_gene))
    value = []
    for i in range(trans):
        for j in range(all_gene):
            if i == j:
                continue
            if method == 'KSG':
                mat[i, j] = mixed.KSG(X[:, i], X[:, j])
            elif method == 'Noisy_KSG':
                mat[i, j] = mixed.Noisy_KSG(X[:, i], X[:, j])
            elif method == 'Partitioning':
                mat[i, j] = mixed.Partitioning(X[:, i], X[:, j])
            elif method == 'Mixed_KSG':
                mat[i, j] = mixed.Mixed_KSG(X[:, i], X[:, j])
            elif method == 'mutual_kde':
                mat[i, j] = mutual_kde(X[:, i], X[:, j])
            value += [mat[i, j]]

    #test(method,node_num,mat)
    return cal_auc(method, np.array(value))
def trinity_MI_between_status_and_all_three(final_cli, final_mut, final_mRNA,
                                            final_met):
    tumor_status = final_cli[1, ].astype(float)
    gene_list = final_mut[:, 0:1]
    MI_list = []
    b = final_mut[1:, 1:].astype(float)
    c = final_mRNA[1:, 1:].astype(float)
    d = final_met[1:, 1:].astype(float)
    x = np.dstack((b, c, d))
    size = np.shape(x)
    print(
        'finish computing final table, combined 3 databases into one, final table dimension is: ',
        size)
    row = size[0]
    i = 0
    while i < row:
        row_value = x[i].astype(float)
        ''' HERE IS THE CHANGE and different from the previous code'''
        mi_score = mixed.Mixed_KSG(row_value, tumor_status, k=1)
        MI_list.append(mi_score)
        i += 1
    print('mutual information score finished.')
    return gene_list, MI_list
Ejemplo n.º 7
0
            acc = data[rows,:][0];acc = acc.split(',')
            for item in range(0,len(acc)):
                if acc[item] == '?':
                    temp_arr.append(new_data[rows-1,item])
                else:
                    temp_arr.append(float(acc[item]))
    
            new_data = np.vstack(( new_data,  np.array(temp_arr).reshape(1,len(temp_arr) )  ))
            temp_arr = []
    return new_data
        
 #######################################################################################
if __name__ == '__main__':
    data = preprocess(data);acc = [];plt.figure(figsize=(35,15))
    input = data[:,0:data.shape[1]-1]
    target_labels = data[:,data.shape[1]-1 ]
    names=['Age', 'Sex','Chest pain type','Resting blood pressure','Serum cholestoral in mg/dl ','Fasting blood sugar','Restecg (resting electrocardiographic results )','thatach (maximum heart rate achieved)','exang (exercise induced angina)','Oldpeak(depression induced by exercise relative to rest)','the slope of the peak exercise ST segment','ca(number of major vessels (0-3) colored by flourosopy)','thal']

    
    
    for col in range(0, input.shape[1]):
        MI = mixed.Mixed_KSG(input[:,col],target_labels)
        acc.append(MI)
        print('Mutual information for',names[col],'is', MI)
    sns.barplot(x=acc, y= names, dodge=False,
                label = names, color="b")
    plt.xlabel('Mutual information')
    plt.savefig('mutualinfo.eps',format='eps')
  

Ejemplo n.º 8
0
import xlsxwriter
import pandas as pd
import numpy as np
import mixed
confidence = 0.95
ca= 0
vd = np.zeros((1,10))


data = np.array(pd.read_excel('pige2.xls',header=None))
print(data.shape)
for j in range(0,9):
    ca= 0
    for m in range(0,9):
     
        MI = mixed.Mixed_KSG(data[:,j],data[:,m])
        ca = np.hstack((ca,MI));
        print(ca)
    
    vd = np.vstack((vd,ca));


vd = np.delete(vd,(0),axis = 1);
vd = np.delete(vd,(0),axis = 0);


workbook = xlsxwriter.Workbook('MI.xlsx')
worksheet = workbook.add_worksheet()
row = 0
for col, data in enumerate(vd.T):
    worksheet.write_column(row, col, data)