def print_results_get_mean_acc(ds1, ds2):
    l1 = len(ds1)
    l2 = len(ds2)
    bias_acc = max(l1,l2) / (l1 + l2)
    print(f"bias_accuracy: {bias_acc : .3f}")
    X_set1, _ = prepare_full_dataset(ds1)
    X_set2, _ = prepare_full_dataset(ds2)
    
    y_set1 = np.zeros(l1)
    y_set2 = np.ones(l2)
    
    X_full = np.concatenate([X_set1, X_set2])
    y_full = np.concatenate([y_set1, y_set2])
    
    
    kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=1)
    rez = []
    for i, (train_index, test_index) in enumerate(kf.split(X_full, y_full)):
        X_train, X_test = X_full[train_index], X_full[test_index]
        y_train, y_test = y_full[train_index], y_full[test_index]
        rez.append(calc_results_simple(X_train, X_test, y_train, y_test, XGBClassifier()))
        
    print(list2d_to_4g_str_pm(rez))
    
    return np.mean(np.array(rez)[:,0])
Exemplo n.º 2
0
def print_results(dataset, set1, set2):
    genes_list = list(
        dataset.drop(
            columns=['study', 'patient_ID', 'pCR', 'RFS', 'DFS', 'posOutcome'
                     ]))
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    print("set1")
    print_mean_fold_importance(X_set1, y_set1, genes_list)

    #    print("set2")
    #    print_mean_fold_importance(X_set2,y_set2,genes_list)

    print("stack")
    print_mean_fold_importance(np.concatenate([X_set1, X_set2]),
                               np.concatenate([y_set1, y_set2]), genes_list)

    X_set1_wf = add_one_features_tail(X_set1, 0)
    X_set2_wf = add_one_features_tail(X_set2, 1)

    print("stack_with_set")
    print_mean_fold_importance(np.concatenate([X_set1_wf, X_set2_wf]),
                               np.concatenate([y_set1, y_set2]), genes_list)
def print_results_baseline(dataset, set1, set2):
    print("==> baseline: ")
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])
    _print_results_set1set2(X_set1, y_set1, X_set2, y_set2)
    print("")
Exemplo n.º 4
0
def print_results(dataset, set1, set2):
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    print("set1")
    print_counter(X_set1[:, 0], y_set1)
    print("set2")
    print_counter(X_set2[:, 0], y_set2)
def print_results(dataset, set1, set2):
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    #    X_set1 = np.random.rand(*X_set1.shape)
    #    X_set2 = np.random.rand(*X_set2.shape)

    X_set1_wf = add_one_features(X_set1, 0)
    X_set2_wf = add_one_features(X_set2, 1)

    X_genes = np.concatenate([X_set1, X_set2])
    X_genes_wf = np.concatenate([X_set1_wf, X_set2_wf])
    y_all = np.concatenate([y_set1, y_set2])

    Xs = X_genes_wf[:, :1]

    kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=10)
    print_order = [
        "genes", "genes_set", "genes_biased", "genes_double", "study"
    ]

    max_len_order = max(map(len, print_order))

    rez = defaultdict(list)

    for i, (train_index, test_index) in enumerate(kf.split(X_genes, y_all)):

        rez["genes"].append(
            calc_results_for_fold(X_genes, y_all, train_index, test_index,
                                  XGBClassifier()))
        rez["genes_set"].append(
            calc_results_for_fold(X_genes_wf, y_all, train_index, test_index,
                                  XGBClassifier()))
        rez["genes_biased"].append(
            calc_results_for_fold(X_genes_wf, y_all, train_index, test_index,
                                  BiasedXgboost()))
        rez["genes_double"].append(
            calc_results_for_fold(X_genes_wf, y_all, train_index, test_index,
                                  DoubleXgboost()))
        rez["study"].append(
            calc_results_for_fold(Xs, y_all, train_index, test_index,
                                  XGBClassifier()))

        for order in print_order:
            print(order, " " * (max_len_order - len(order)),
                  " y, y_maxi, y_prob, y_prob_maxi : ",
                  list_to_4g_str(rez[order][-1]))
        print("")

    for order in print_order:
        print("==> ", order, " " * (max_len_order - len(order)),
              " y, y_maxi, y_prob, y_prob_maxi : ",
              list_to_4g_str(np.mean(rez[order], axis=0)))
Exemplo n.º 6
0
def print_results(dataset, set1, set2):
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    X_set1_wf = add_one_features(X_set1, 0)
    X_set2_wf = add_one_features(X_set2, 1)

    X_full = np.concatenate([X_set1, X_set2])
    X_full_wf = np.concatenate([X_set1_wf, X_set2_wf])
    y_full = np.concatenate([y_set1, y_set2])

    Xs = X_full_wf[:, :1]

    kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=1)
    rez = []
    rez_wf = []
    rez_s = []
    print_order = ["base", "studyf", "studyf_l", "only_study"]

    max_len_order = max(map(len, print_order))

    rez = defaultdict(list)

    for i, (train_index, test_index) in enumerate(kf.split(X_full, y_full)):
        rez["base"].append(
            calc_results_for_fold(X_full, y_full, train_index, test_index,
                                  XGBClassifier()))
        rez["studyf"].append(
            calc_results_for_fold(X_full_wf, y_full, train_index, test_index,
                                  XGBClassifier()))
        rez["studyf_l"].append(
            calc_results_for_fold(X_full_wf, y_full, train_index, test_index,
                                  XGBClassifier("binary:logistic")))
        rez["only_study"].append(
            calc_results_for_fold(Xs, y_full, train_index, test_index,
                                  XGBClassifier()))

        for order in print_order:
            print(order, " " * (max_len_order - len(order)), ": ",
                  list_to_4g_str(rez[order][-1]))
        print("")

    for order in print_order:
        print("==> ", order, " " * (max_len_order - len(order)), ": ",
              list2d_to_4g_str_pm(rez[order]))
def print_results(dataset, set1, set2):
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    print("set1")
    clf = XGBClassifier()
    clf.fit(X_set1, y_set1)
    print(sorted(np.argsort(-1 * clf.feature_importances_)[:20]))
    #    print(np.sort(-1 * clf.feature_importances_)[:20])

    set1_set20 = set(np.argsort(-1 * clf.feature_importances_)[:20])

    print("set2")
    clf = XGBClassifier()
    clf.fit(X_set2, y_set2)
    print(sorted(np.argsort(-1 * clf.feature_importances_)[:20]))
    #    print(np.sort(-1 * clf.feature_importances_)[:20])
    set2_set20 = set(np.argsort(-1 * clf.feature_importances_)[:20])

    print("intersection set1 set2", set1_set20.intersection(set2_set20))

    print("stack")
    clf = XGBClassifier()
    clf.fit(np.concatenate([X_set1, X_set2]), np.concatenate([y_set1, y_set2]))
    print(sorted(np.argsort(-1 * clf.feature_importances_)[:20]))
    #    print(np.sort(-1 * clf.feature_importances_)[:20])

    stack_set20 = set(np.argsort(-1 * clf.feature_importances_)[:20])

    print("intersection set1 stack", set1_set20.intersection(stack_set20))
    print("intersection set2 stack", set2_set20.intersection(stack_set20))

    X_set1_wf = add_one_features_tail(X_set1, 0)
    X_set2_wf = add_one_features_tail(X_set2, 1)
    print("stack_with_set")
    clf = XGBClassifier()
    clf.fit(np.concatenate([X_set1_wf, X_set2_wf]),
            np.concatenate([y_set1, y_set2]))
    print(sorted(np.argsort(-1 * clf.feature_importances_)[:20]))
Exemplo n.º 8
0
def print_results(dataset, set1, set2):
    bias_acc = max(len(set1), len(set2)) / (len(set1) + len(set2))
    print(len(set1), len(set2))
    print(f"bias_accuracy: {bias_acc : .3f}")
    X_set1, _ = prepare_full_dataset(dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, _ = prepare_full_dataset(dataset.loc[dataset['patient_ID'].isin(set2)])
    
    y_set1 = np.zeros(len(set1))
    y_set2 = np.ones(len(set2))
    
    X_full = np.concatenate([X_set1, X_set2])
    y_full = np.concatenate([y_set1, y_set2])
    
    
    kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=10)
    rez = []
    for i, (train_index, test_index) in enumerate(kf.split(X_full, y_full)):
        X_train, X_test = X_full[train_index], X_full[test_index]
        y_train, y_test = y_full[train_index], y_full[test_index]
        rez.append(calc_results_simple(X_train, X_test, y_train, y_test, XGBClassifier()))
    print(list2d_to_4g_str_pm(rez))
def print_results(dataset, set1, set2):

    bias_acc = max(len(set1), len(set2)) / (len(set1) + len(set2))
    print(len(set1), len(set2))
    print(f"bias_accuracy: {bias_acc : .3f}")
    X_set1, _ = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, _ = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    y_set1 = np.zeros(len(set1))
    y_set2 = np.ones(len(set2))

    X_full = np.concatenate([X_set1, X_set2])
    y_full = np.concatenate([y_set1, y_set2])

    kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=1)
    rez = []
    for i, (train_index, test_index) in enumerate(kf.split(X_full, y_full)):
        X_train, X_test = X_full[train_index], X_full[test_index]
        y_train, y_test = y_full[train_index], y_full[test_index]
        clf = XGBClassifier()
        rez.append(calc_results_simple(X_train, X_test, y_train, y_test, clf))
        clf.fit(X_train, y_train)
        print(np.argsort(-1 * clf.feature_importances_)[:20])
        print(np.sort(-1 * clf.feature_importances_)[:20])

        for i in np.argsort(-1 * clf.feature_importances_)[:20]:
            print(
                list(
                    dataset.drop(columns=[
                        'study', 'patient_ID', 'pCR', 'RFS', 'DFS',
                        'posOutcome'
                    ]))[i])

    print(list2d_to_4g_str_pm(rez))
import pandas as pd
import os
import numpy as np
import random
import math
from funs_common import read_mike_dataset, drop_trea, prepare_full_dataset
from sklearn.manifold import TSNE
import pickle

dataset = read_mike_dataset()

notrea_dataset = drop_trea(dataset)
X, y = prepare_full_dataset(notrea_dataset, y_field='study')

X_embedded = TSNE(n_components=2).fit_transform(X)

pickle.dump(X_embedded, open("experement24_studytest_tsne.p", "wb"))
Exemplo n.º 11
0
def print_results(dataset, set1, set2):
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)])
    X_set2, y_set2 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set2)])

    #    X_set1 = np.random.rand(*X_set1.shape)
    #    X_set2 = np.random.rand(*X_set2.shape)

    X_set1_wf = add_one_features(X_set1, 0)
    X_set2_wf = add_one_features(X_set2, 1)

    X_genes_wf = np.concatenate([X_set1_wf, X_set2_wf])
    y_all = np.concatenate([y_set1, y_set2])

    kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=10)
    print_order = [
        "genes", "genes_set", "genes_biased", "genes_double", "study"
    ]

    max_len_order = max(map(len, print_order))

    rez = defaultdict(list)

    for i, (train_index, test_index) in enumerate(kf.split(X_genes_wf, y_all)):
        X_genes_wf_train, X_genes_wf_test = X_genes_wf[
            train_index], X_genes_wf[test_index]
        y_train, y_test = y_all[train_index], y_all[test_index]

        print("before balanced")
        print_count_two_sets(X_genes_wf_train[:, 0], y_train)
        print_count_two_sets(X_genes_wf_test[:, 0], y_test)
        #        print("counter before balance", Counter(X_genes_wf_train[:,0]), Counter(X_genes_wf_test[:,0]), Counter(y_train), Counter(y_test))
        X_genes_wf_train, y_train = random_upsample_balance(
            X_genes_wf_train, y_train)
        X_genes_wf_test, y_test = random_upsample_balance(
            X_genes_wf_test, y_test)
        #        print("counter after balance", Counter(X_genes_wf_train[:,0]), Counter(X_genes_wf_test[:,0]), Counter(y_train), Counter(y_test))
        print("after balanced")
        print_count_two_sets(X_genes_wf_train[:, 0], y_train)
        print_count_two_sets(X_genes_wf_test[:, 0], y_test)

        X_genes_train = X_genes_wf_train[:, 1:]
        X_genes_test = X_genes_wf_test[:, 1:]

        Xs_train = X_genes_wf_train[:, :1]
        Xs_test = X_genes_wf_test[:, :1]

        rez["genes"].append(
            calc_results_simple(X_genes_train, X_genes_test, y_train, y_test,
                                XGBClassifier()))
        rez["genes_set"].append(
            calc_results_simple(X_genes_wf_train, X_genes_wf_test, y_train,
                                y_test, XGBClassifier()))
        rez["genes_biased"].append(
            calc_results_simple(X_genes_wf_train, X_genes_wf_test, y_train,
                                y_test, BiasedXgboost()))
        rez["genes_double"].append(
            calc_results_simple(X_genes_wf_train, X_genes_wf_test, y_train,
                                y_test, DoubleXgboost()))
        rez["study"].append(
            calc_results_simple(Xs_train, Xs_test, y_train, y_test,
                                XGBClassifier()))

        for order in print_order:
            print(order, " " * (max_len_order - len(order)), ": ",
                  list_to_4g_str(rez[order][-1]))
        print("")

    for order in print_order:
        print("==> ", order, " " * (max_len_order - len(order)), ": ",
              list2d_to_4g_str_pm(rez[order]))
Exemplo n.º 12
0
    print("==> DFS", prefix)
    print_results_for_field(dataset, Xt_full_dict, "DFS", prefix)
    print("")
    print("")

    print("==> posOutcome", prefix)
    print_results_for_field(dataset, Xt_full_dict, "posOutcome", prefix)
    print("")
    print("")


treat_dataset = read_treat_dataset()
combat_dataset = read_combat_dataset()
pam_types_cat_dataset = read_pam_types_cat_dataset()

X_full, _ = prepare_full_dataset(drop_trea(combat_dataset))

assert all(pam_types_cat_dataset['patient_ID'] == combat_dataset['patient_ID'])

#print(list(pam_types_cat_dataset))
#print_results(pam_types_cat_dataset, "old")

for n_cluster in [1, 5, 10, 20, 100, 200]:
    lda = LatentDirichletAllocation(n_components=n_cluster)
    Xt_full = lda.fit_transform(X_full - np.min(X_full))
    Xt_full_dict = {
        i: x
        for i, x in zip(combat_dataset['patient_ID'], Xt_full)
    }

    print_results(pam_types_cat_dataset, Xt_full_dict, "nc" + str(n_cluster))
Exemplo n.º 13
0
def print_set(dataset, set1, y_field):
    X_set1, y_set1 = prepare_full_dataset(
        dataset.loc[dataset['patient_ID'].isin(set1)], y_field)
    print_counter(X_set1[:, 0], y_set1)