<jupyter_start><jupyter_text>run train_lgen bat and test_leg bat<jupyter_code>snps = read.getListOfSnps()
ids = {} 


ids['patients'] = setIdToName(list(patients.keys()))
ids['snps'] = setIdToName(snps)


if os.path.exists(path + 'snpCode.txt'):
    print("mphka")
    X, Y = read.readSnpsCode(patients,ids)
    
else:
    
    write.writeSnpLog(read.getNumberOfPatients(),read.getNumberOfSnps(),chromosomes,read.getSnpsList(),patients)
    #X, Y = read.readSnpsCode(patients,ids)


#write.writeDf(read.getNumberOfPatients(),read.getNumberOfSnps(),chromosomes,ids,patients)

#patients = None
#X.shape
#X,Y =balancedData(X,Y)

xTraining, xTest, yTraining1, yTest = train_test_split(X, Y, test_size=0.1, random_state=randint(0,2018))
print("mergex = ",X.shape)
print("xTrain = ",xTraining.shape)
print("xTest = ",xTest.shape)
print("yTrain = ",yTraining1.shape)
print("yTest = ",yTest.shape)

snpReduc = {}<jupyter_output>mergex =  (4980, 7799)
xTrain =  (4482, 7799)
xTest =  (498, 7799)
yTrain =  (4482,)
yTest =  (498,)<jupyter_text>Correlation<jupyter_code>#xC = casePatient(X,Y)
#cor = Correlation(xC)

cor = Correlation(X)


#cor = RSquare(X)

#cor = Correlation(xTraining)
#cor = RSquare(xTraining)
snpReduc = {}<jupyter_output><empty_output><jupyter_text>Correlation high<jupyter_code>down = 40
up = 100 
threshold = 0.7
snpReduc['high'] = cor.getHighCorrelationSnps(threshold, down=down,up=up,c=-2)
write.writeSnpsUsed(snpReduc['high'],ids['snps']['idToName'],chromosomes,'high')<jupyter_output><empty_output><jupyter_text>Correlation Low<jupyter_code>down = 100
up = 100 
threshold = 0.7
#snpReduc['low'] = cor.getLowCorrelationSnps(threshold, down=down,up=up)
snpReduc['low100'] = cor.getLowCorrelationSnps(threshold, down=down,up=up,c=-2)
write.writeSnpsUsed(snpReduc['low100'],ids['snps']['idToName'],chromosomes,'100%')
'''

file = open(path+'0.01 cut of.txt','r')
snpReduc = {}
down = 100
up = 100 
threshold = 0.7
snpReduc['low100'] = []
for line in file:
    
    snpReduc['low100'].append(ids['snps']['nameToId'][line.split()[0].strip()])
'''   

snpReduc ={}
snpReduc['random'] =  selectRandomSnps(size = len(X.T))

#print(len(snpReduc['low97']))
print(len(snpReduc['random']))<jupyter_output><empty_output><jupyter_text>CRAMMER V<jupyter_code>df = open(path + 'cram.txt','r')

#columns = df.readline().split(',')[1:]

print('columns = ', columns)
lines = df.readlines()

table = np.zeros((len(columns),len(columns)), dtype = np.float32)

for i in range(len(lines)):
    
    row = lines[i].split(',')
    for j in range(1,len(row)):
        
        table[i,j-1] = float(row[j])
        
        
df.close()


table
np.median(table)
down = 100
up = 100 
thres = np.median(table)

snpReduc['crammer100'] = s.lowCorrelation(table, threshold=thres, up=up, down=down)#oso megalytero threshold toso perissotero omoia einai 
categories = []
#categories = ['low','high']
#categories = ['low']
#categories = ['low97','low100']
categories = ['crammer100']
writeResults2={}
writeResults={}<jupyter_output><empty_output><jupyter_text>RFR<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import BaggingClassifier

for i in categories:
    r1={}
    writeResults={}
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    clf = RandomForestClassifier()
    #clf.fit(xTraining1, yTraining1)
   
    param_grid = {
              "n_estimators" : [9, 18, 27, 36, 45, 54, 63],
              "min_samples_split": [2, 3, 10],
              "min_samples_leaf": [1, 3, 10],
              "bootstrap": [True, False],
              "criterion": ["gini", "entropy"],
               "warm_start":[True,False]}
    
    '''param_grid = { 
         'n_estimators':[2000,3000,5000], 
         'max_depth':[5,15,30], 
         'min_samples_split':[2,3], 
        'min_samples_leaf':[1,2],
        "warm_start":[True,False],
                "oob_score":[True,False]}
    '''
    
    # run grid search
    grid_search = GridSearchCV(estimator=clf, param_grid=param_grid,cv = 10,verbose=1)
    
    grid_search.fit(xTraining1, yTraining1)
    
    
    print()
    
    #yPredict4 = clf.predict(xTest1)
    yPredict4 = grid_search.predict(xTest1)

    print(metrics.accuracy_score(yTest,yPredict4))
    print(metrics.confusion_matrix(yTest,yPredict4))
    error4 = mean_squared_error(yTest, yPredict4)
    print("error 4 = ",error4)
    RMSE4 = mean_squared_error(yTest,yPredict4)**0.5
    print("RMSE4 = ",RMSE4)

    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict4)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict4))
    print("precision = ",metrics.precision_score(yTest,yPredict4))
    print("f1Score = ",f1_score(yTest, yPredict4, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict4)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict4)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict4)
    r1['precision'] = metrics.precision_score(yTest,yPredict4)
    r1['f1'] = f1_score(yTest, yPredict4, average='binary')
    writeResults2[i] = r1
    
    print()

    results = crossValidiation(XX, Y, k = 10, classifier = grid_search, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    results['len_snps'] = len(snpReduc[i])
   # results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    
    #writeResult(path,'rfr'+i,writeResults,i)
   # writeResultConf(path,'rfr'+i,writeResults2)
    
clf.fit(xTraining1, yTraining1)
yPredict4 = clf.predict(xTest1)


print(metrics.accuracy_score(yTest,yPredict4))
print(metrics.confusion_matrix(yTest,yPredict4))
error4 = mean_squared_error(yTest, yPredict4)
print("error 4 = ",error4)
RMSE4 = mean_squared_error(yTest,yPredict4)**0.5
print("RMSE4 = ",RMSE4)

fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict4)
print("AUC = ", metrics.auc(fpr,tpr))
print("recal = ",metrics.recall_score(yTest,yPredict4))
print("precision = ",metrics.precision_score(yTest,yPredict4))
print("f1Score = ",f1_score(yTest, yPredict4, average='binary'))<jupyter_output><empty_output><jupyter_text>SVM KERNEL<jupyter_code>from sklearn.svm import NuSVC

for i in categories:
    
    r1 = {}
    r1['down'] = down
    r1['up'] = up
    r1['thres'] = threshold
    writeResults={}
    print("Category = ",i)
    
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    '''  test = SelectKBest(score_func=chi2,k = 500)
    fit1 = test.fit(xTraining1, yTraining1)
    xTraining1 = fit1.transform(xTraining1)

    fit2 = test.fit(xTest1, yTest)
    xTest1 = fit2.transform(xTest1)

    fit3 = test.fit(XX, Y)
    XX = fit3.transform(XX)

    print("xtest", xTest1.shape)
    print("xtraining", xTraining1.shape)
    print("X", XX.shape)
    '''
    ''' enc = OneHotEncoder(n_values =3) 
    enc.fit(XX) 
    XX = enc.fit_transform(XX)
    XX = XX.toarray()
    print(XX.shape)

    enc.fit(xTraining1) 
    xTraining1 = enc.fit_transform(xTraining1)
    xTraining1 = xTraining1.toarray()
    print(xTraining1.shape)

    enc.fit(xTest1) 
    xTest1 = enc.fit_transform(xTest1)
    xTest1 = xTest1.toarray()
    print(xTest1.shape)'''
    
    
    clf = SVC(kernel='linear')
    #clf = NuSVC(kernel='rbf',nu=0.01)
    clf.fit(xTraining1, yTraining1)
    yPredict2 = clf.predict(xTest1)
    print(metrics.accuracy_score(yTest,yPredict2))
    print(metrics.confusion_matrix(yTest,yPredict2))
    error2 = mean_squared_error(yTest, yPredict2)
    print("error 2 = ",error2)
    RMSE2 = mean_squared_error(yTest,yPredict2)**0.5
    print("RMSE2 = ",RMSE2)
    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict2)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict2))
    print("precision = ",metrics.precision_score(yTest,yPredict2))
    print("f1Score = ",f1_score(yTest, yPredict2, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict2)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict2)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict2)
    r1['precision'] = metrics.precision_score(yTest,yPredict2)
    r1['f1'] = f1_score(yTest, yPredict2, average='binary')
    
    writeResults2[i] = r1
    print()

    results = crossValidiation(XX, Y, k = 10, classifier = clf, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    results['len_snps'] = len(snpReduc[i])
   # results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    writeResult(path,'svm'+i,writeResults,i)
    writeResultConf(path,'svm'+i,writeResults2)
for i in categories:
    
    r1 = {}
    r1['down'] = down
    r1['up'] = up
    r1['thres'] = threshold
    writeResults={}
    print("Category = ",i)
    
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    '''  test = SelectKBest(score_func=chi2,k = 500)
    fit1 = test.fit(xTraining1, yTraining1)
    xTraining1 = fit1.transform(xTraining1)

    fit2 = test.fit(xTest1, yTest)
    xTest1 = fit2.transform(xTest1)

    fit3 = test.fit(XX, Y)
    XX = fit3.transform(XX)

    print("xtest", xTest1.shape)
    print("xtraining", xTraining1.shape)
    print("X", XX.shape)
    '''
    '''enc = OneHotEncoder(n_values =3) 
    enc.fit(XX) 
    XX = enc.fit_transform(XX)
    XX = XX.toarray()
    print(XX.shape)

    enc.fit(xTraining1) 
    xTraining1 = enc.fit_transform(xTraining1)
    xTraining1 = xTraining1.toarray()
    print(xTraining1.shape)

    enc.fit(xTest1) 
    xTest1 = enc.fit_transform(xTest1)
    xTest1 = xTest1.toarray()
    print(xTest1.shape)'''
    
    
    clf = SVC()
    #clf = NuSVC(kernel='rbf',nu=0.01)
    clf.fit(xTraining1, yTraining1)
    yPredict2 = clf.predict(xTest1)
    print(metrics.accuracy_score(yTest,yPredict2))
    print(metrics.confusion_matrix(yTest,yPredict2))
    error2 = mean_squared_error(yTest, yPredict2)
    print("error 2 = ",error2)
    RMSE2 = mean_squared_error(yTest,yPredict2)**0.5
    print("RMSE2 = ",RMSE2)
    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict2)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict2))
    print("precision = ",metrics.precision_score(yTest,yPredict2))
    print("f1Score = ",f1_score(yTest, yPredict2, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict2)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict2)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict2)
    r1['precision'] = metrics.precision_score(yTest,yPredict2)
    r1['f1'] = f1_score(yTest, yPredict2, average='binary')
    
    writeResults2[i] = r1
    print()

    results = crossValidiation(XX, Y, k = 10, classifier = clf, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    results['len_snps'] = len(snpReduc[i])
   # results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    writeResult(path,'svmnonlinear'+i,writeResults,i)
    writeResultConf(path,'svmnonlinear'+i,writeResults2)
xTraining1.shape<jupyter_output><empty_output><jupyter_text>LINEAR LOGISTIC REGRESSION<jupyter_code>for i in categories:
    
    r1 = {}
    writeResults={}
    print("Category = ",i)
    
   
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    '''test = SelectKBest(score_func=chi2,k = 500)
    fit1 = test.fit(xTraining1, yTraining1)
    xTraining1 = fit1.transform(xTraining1)

    fit2 = test.fit(xTest1, yTest)
    xTest1 = fit2.transform(xTest1)

    fit3 = test.fit(XX, Y)
    XX = fit3.transform(XX)

    print("xtest", xTest1.shape)
    print("xtraining", xTraining1.shape)
    print("X", XX.shape)'''
   
    '''enc = OneHotEncoder(n_values =3) 
    enc.fit(XX) 
    XX = enc.fit_transform(XX)
    XX = XX.toarray()
    print(XX.shape)
    enc.fit(xTraining1) 
    xTraining1 = enc.fit_transform(xTraining1)
    xTraining1 = xTraining1.toarray()
    print(xTraining1.shape)

    enc.fit(xTest1) 
    xTest1 = enc.fit_transform(xTest1)
    xTest1 = xTest1.toarray()
    print(xTest1.shape)
    '''
     

    #lr_clf = linear_model.LogisticRegression()
 
    lr_clf = linear_model.LogisticRegressionCV()
    lr_clf.fit(xTraining1, yTraining1)
    
    yPredict4 = lr_clf.predict(xTest1)

    print(metrics.accuracy_score(yTest,yPredict4))
    print(metrics.confusion_matrix(yTest,yPredict4))
    error4 = mean_squared_error(yTest, yPredict4)
    print("error 4 = ",error4)
    RMSE4 = mean_squared_error(yTest,yPredict4)**0.5
    print("RMSE4 = ",RMSE4)

    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict4)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict4))
    print("precision = ",metrics.precision_score(yTest,yPredict4))
    print("f1Score = ",f1_score(yTest, yPredict4, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict4)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict4)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict4)
    r1['precision'] = metrics.precision_score(yTest,yPredict4)
    r1['f1'] = f1_score(yTest, yPredict4, average='binary')
    writeResults2[i] = r1
    
    print()

    results = crossValidiation(XX, Y, k = 10, classifier = lr_clf, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    results['len_snps'] = len(snpReduc[i])
   # results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    
    writeResult(path,'llrcv'+i,writeResults,i)
    writeResultConf(path,'llrcv'+i,writeResults2)
for iCat in categories:
    
    ids['coef']={}
    XX = createNewTable(snpReduc[iCat],X)

    lr_clf = linear_model.LogisticRegression()
    lr_clf.fit(XX, Y)

    coefs=lr_clf.coef_[0]
    print("before = ",len(set(coefs)))
    '''for i in range(len(coefs)):
        coefs[i] = abs(coefs[i])'''

    print("after = ",len(set(coefs)))

    idToName = {}
    nameToId = {}

    for i in range(len(coefs)):
        nameToId[coefs[i]] = []

    for i in range(len(coefs)):
        nameToId[coefs[i]].append(i)
        idToName[i] = coefs[i]


    ids['coef']['nameToId'] = nameToId
    ids['coef']['idToName'] = idToName


    sc = sorted(coefs,reverse=True)

    top_30 = []

    for i in range(30):
        snp = ids['coef']['nameToId'][sc[i]][0]
        ids['coef']['nameToId'][sc[i]].remove(snp)
        top_30.append(snpReduc[iCat][snp])
        #top_30.append(snp)
    print('len3 = ', top_30)
    #snpReduc['low'] = top_30
    writeCoef(path,top_30,sc,columns, 'not_mycutoffabs')
    #write.writeSnpsUsed(top_30,ids['snps']['idToName'],chromosomes,iCat+'_abs_top30')
   # write.writeSnpsUsed(top_30,ids['snps']['idToName'],chromosomes,'top_30_all_abs')

    #writeNSemanticSnps(path,metric = sc,columns = ids['snps']['idToName'], snps = top_30, name ='30semantic')
from sklearn.naive_bayes import GaussianNB

for i in categories:
    
    r1 = {}
    writeResults={}
    print("Category = ",i)
    
   
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    '''
      test = SelectKBest(score_func=chi2,k = 500)
    fit1 = test.fit(xTraining1, yTraining1)
    xTraining1 = fit1.transform(xTraining1)

    fit2 = test.fit(xTest1, yTest)
    xTest1 = fit2.transform(xTest1)

    fit3 = test.fit(XX, Y)
    XX = fit3.transform(XX)

    print("xtest", xTest1.shape)
    print("xtraining", xTraining1.shape)
    print("X", XX.shape)
    
    '''
    
    '''enc = OneHotEncoder(n_values =3) 
    enc.fit(XX) 
    XX = enc.fit_transform(XX)
    XX = XX.toarray()
    print(XX.shape)
    
    enc.fit(xTraining1) 
    xTraining1 = enc.fit_transform(xTraining1)
    xTraining1 = xTraining1.toarray()
    print(xTraining1.shape)

    enc.fit(xTest1) 
    xTest1 = enc.fit_transform(xTest1)
    xTest1 = xTest1.toarray()
    print(xTest1.shape)
    '''

    gnb = GaussianNB()
    gnb.fit(xTraining1, yTraining1)
    yPredict6 = gnb.predict(xTest1)
    print(metrics.accuracy_score(yTest,yPredict6))
    print(metrics.confusion_matrix(yTest,yPredict6))
    error6 = mean_squared_error(yTest, yPredict6)
    print("error 6 = ",error6)
    RMSE6 = mean_squared_error(yTest,yPredict6)**0.5
    print("RMSE6 = ",RMSE6)

    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict6)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict6))
    print("precision = ",metrics.precision_score(yTest,yPredict6))
    print("f1Score = ",f1_score(yTest, yPredict6, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict6)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict6)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict6)
    r1['precision'] = metrics.precision_score(yTest,yPredict6)
    r1['f1'] = f1_score(yTest, yPredict6, average='binary')
    
    writeResults2[i] = r1
    
    print()

    results = crossValidiation(XX, Y, k = 10,classifier = gnb, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    print()
    results['len_snps'] = len(snpReduc[i])
  #  results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    
    writeResult(path,'gnb'+i,writeResults,i)
    writeResultConf(path,'gnb'+i,writeResults2)
#xTraining.shape
yTraining1.shape<jupyter_output><empty_output><jupyter_text>BERNOULLI<jupyter_code>'''xTraining1 = createNewTable(snpReduc['low'],xTraining)
lr_clf = linear_model.LogisticRegressionCV()
lr_clf.fit(xTraining1, yTraining1)

coefs=lr_clf.coef_[0]
top_20 = np.argpartition(coefs, -50)[-50:]
#top_20 = np.argpartition(coefs, 50)[:50]
removedSnps = []
for i in range(len(snpReduc['low'])):
    if i not in top_20:
        removedSnps.append(snpReduc['low'][i])'''



for i in categories:
   
    r1 = {}
    writeResults={}
    print("Category = ",i)
    
   
   
  
    
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    '''
    test = SelectKBest(score_func=chi2,k = 500)
    fit1 = test.fit(xTraining1, yTraining1)
    xTraining1 = fit1.transform(xTraining1)

    fit2 = test.fit(xTest1, yTest)
    xTest1 = fit2.transform(xTest1)

    fit3 = test.fit(XX, Y)
    XX = fit3.transform(XX)
    print("xtest", xTest1.shape)
    print("xtraining", xTraining1.shape)
    print("X", XX.shape)

    
    '''
    
    '''enc = OneHotEncoder(n_values =3) 
    enc.fit(XX) 
    XX = enc.fit_transform(XX)
    XX = XX.toarray()
    print(XX.shape)
    
    enc.fit(xTraining1) 
    xTraining1 = enc.fit_transform(xTraining1)
    xTraining1 = xTraining1.toarray()
    print(xTraining1.shape)

    enc.fit(xTest1) 
    xTest1 = enc.fit_transform(xTest1)
    xTest1 = xTest1.toarray()
    print(xTest1.shape)'''
    

    bern = BernoulliNB()
    bern.fit(xTraining1, yTraining1)
    yPredict6 = bern.predict(xTest1)
    print(metrics.accuracy_score(yTest,yPredict6))
    print(metrics.confusion_matrix(yTest,yPredict6))
    error6 = mean_squared_error(yTest, yPredict6)
    print("error 6 = ",error6)
    RMSE6 = mean_squared_error(yTest,yPredict6)**0.5
    print("RMSE6 = ",RMSE6)

    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict6)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict6))
    print("precision = ",metrics.precision_score(yTest,yPredict6))
    print("f1Score = ",f1_score(yTest, yPredict6, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict6)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict6)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict6)
    r1['precision'] = metrics.precision_score(yTest,yPredict6)
    r1['f1'] = f1_score(yTest, yPredict6, average='binary')
    
    writeResults2[i] = r1
    
    print()

    results = crossValidiation(XX, Y, k = 10,classifier = bern, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    print()
    results['len_snps'] = len(snpReduc[i])
  #  results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    
    writeResult(path,'bernoulli'+i,writeResults,i)
    writeResultConf(path,'bernoulli'+i,writeResults2)
'''l= [5,6,7,2,3,4,10,6,7,9,8,5]
top_3 = np.argpartition(l,3)[:3]
print(top_3)
removedSnps=[]
for i in range(len(l)):
    if i not in top_3:
        removedSnps.append(l[i])
print(removedSnps)'''
'''XX = createNewTable(snpReduc['low'],X)
#enc = OneHotEncoder(n_values =3) 
#enc.fit(XX) 
#XX = enc.fit_transform(XX)
#XX = XX.toarray()
print(XX.shape)

re = cross_val_score(bern, XX, Y, cv=10)
print(sum(re)/10)

print()
XX = X
#enc = OneHotEncoder(n_values =3) 
#enc.fit(XX) 
#XX = enc.fit_transform(XX)
#XX = XX.toarray()
print(XX.shape)
re = cross_val_score(bern, XX, Y, cv=10)
print(sum(re)/10)
'''<jupyter_output><empty_output><jupyter_text>TREE<jupyter_code>from sklearn import tree
from sklearn.ensemble import BaggingClassifier
for i in categories:
    
    r1 = {}
    writeResults={}
    print("Category = ",i)
    
   
    XX = createNewTable(snpReduc[i],X)
    xTraining1 = createNewTable(snpReduc[i],xTraining)
    xTest1 = createNewTable(snpReduc[i],xTest)
    
    
    
    ''' test = SelectKBest(score_func=chi2,k = 500)
    fit1 = test.fit(xTraining1, yTraining1)
    xTraining1 = fit1.transform(xTraining1)

    fit2 = test.fit(xTest1, yTest)
    xTest1 = fit2.transform(xTest1)

    fit3 = test.fit(XX, Y)
    XX = fit3.transform(XX)
    print("xtest", xTest1.shape)
    print("xtraining", xTraining1.shape)
    print("X", XX.shape)
    '''
    
    '''enc = OneHotEncoder(n_values =3) 
    enc.fit(XX) 
    XX = enc.fit_transform(XX)
    XX = XX.toarray()
    print(XX.shape)

    enc.fit(xTraining1) 
    xTraining1 = enc.fit_transform(xTraining1)
    xTraining1 = xTraining1.toarray()
    print(xTraining1.shape)

    enc.fit(xTest1) 
    xTest1 = enc.fit_transform(xTest1)
    xTest1 = xTest1.toarray()
    print(xTest1.shape)
    '''
    

    clf= tree.DecisionTreeClassifier()
    
    #clf = BaggingClassifier(clf1, n_estimators=100, max_samples=2,
     #                   random_state=1)
    
    clf.fit(xTraining1, yTraining1)
    yPredict4 = clf.predict(xTest1)

    print(metrics.accuracy_score(yTest,yPredict4))
    print(metrics.confusion_matrix(yTest,yPredict4))
    error4 = mean_squared_error(yTest, yPredict4)
    print("error 4 = ",error4)
    RMSE4 = mean_squared_error(yTest,yPredict4)**0.5
    print("RMSE4 = ",RMSE4)

    fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict4)
    print("AUC = ", metrics.auc(fpr,tpr))
    print("recal = ",metrics.recall_score(yTest,yPredict4))
    print("precision = ",metrics.precision_score(yTest,yPredict4))
    print("f1Score = ",f1_score(yTest, yPredict4, average='binary'))
    
    r1['confu'] = metrics.confusion_matrix(yTest,yPredict4)
    r1['accu'] = metrics.accuracy_score(yTest,yPredict4)
    r1['auc'] = metrics.auc(fpr,tpr)
    r1['recal'] = metrics.recall_score(yTest,yPredict4)
    r1['precision'] = metrics.precision_score(yTest,yPredict4)
    r1['f1'] = f1_score(yTest, yPredict4, average='binary')
    writeResults2[i] = r1
    
    print()

    results = crossValidiation(XX, Y, k = 10, classifier = clf, continious = False)
    results['down'] = down
    results['up'] = up
    results['thres'] = threshold
    print("accuracy = ",results['accuracy'])
    print("AUC = ", results['auc'])
    print("recal = ",results['recall'])
    print("precision = ",results['precision'])
    print("f1 = ",results['f1'])
    results['len_snps'] = len(snpReduc[i])
   # results['len snps'] = 500
    writeResults[i] = results
    
    print()
    
    
    writeResult(path,'tree'+i,writeResults,i)
    writeResultConf(path,'tree'+i,writeResults2)
import graphviz 

for iCat in categories:
    featuresName = []
    for i in range(len(snpReduc[iCat])):
        snp = snpReduc[iCat][i]
        featuresName.append(ids['snps']['idToName'][snp])
    xTraining1 = createNewTable(snpReduc[iCat],xTraining)
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(xTraining1, yTraining1)
    treeee = open(path+iCat+'_balancedTree.dot','w')
    dot_data = tree.export_graphviz(clf, out_file=treeee, 
                            feature_names=featuresName,leaves_parallel =True,max_depth = 6)  
    graph = graphviz.Source(dot_data)<jupyter_output><empty_output><jupyter_text>ALL<jupyter_code>r1 = {}
writeResults2={}
writeResults={}

'''enc = OneHotEncoder(n_values =3) 
enc.fit(X) 
XX = enc.fit_transform(X)
XX = XX.toarray()
print(XX.shape)

enc.fit(xTraining) 
xTraining1 = enc.fit_transform(xTraining)
xTraining1 = xTraining1.toarray()
print(xTraining1.shape)

enc.fit(xTest) 
xTest1 = enc.fit_transform(xTest)
xTest1 = xTest1.toarray()
print(xTest1.shape)'''
    

bern = BernoulliNB()
bern.fit(xTraining, yTraining1)
yPredict6 = bern.predict(xTest)
print(metrics.accuracy_score(yTest,yPredict6))
print(metrics.confusion_matrix(yTest,yPredict6))
error6 = mean_squared_error(yTest, yPredict6)
print("error 6 = ",error6)
RMSE6 = mean_squared_error(yTest,yPredict6)**0.5
print("RMSE6 = ",RMSE6)

fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict6)
print("AUC = ", metrics.auc(fpr,tpr))
print("recal = ",metrics.recall_score(yTest,yPredict6))
print("precision = ",metrics.precision_score(yTest,yPredict6))
print("f1Score = ",f1_score(yTest, yPredict6, average='binary'))

r1['confu'] = metrics.confusion_matrix(yTest,yPredict6)
r1['accu'] = metrics.accuracy_score(yTest,yPredict6)
r1['auc'] = metrics.auc(fpr,tpr)
r1['recal'] = metrics.recall_score(yTest,yPredict6)
r1['precision'] = metrics.precision_score(yTest,yPredict6)
r1['f1'] = f1_score(yTest, yPredict6, average='binary')


writeResults2['all'] = r1
    
print()

results = crossValidiation(X, Y, k = 10,classifier = bern, continious = False)
results['down'] = down
results['up'] = up
results['thres'] = threshold    
print("accuracy = ",results['accuracy'])
print("AUC = ", results['auc'])
print("recal = ",results['recall'])
print("precision = ",results['precision'])
print("f1 = ",results['f1'])


results['len_snps'] = len(X.T)
writeResults['all'] = results
    
print()
    
    
writeResult(path,'bernoulli_all',writeResults,'all')
#writeResultConf(path,'bernoulli_all',writeResults2)

r1 = {}
writeResults2={}
writeResults={}

'''enc = OneHotEncoder(n_values =3) 
enc.fit(X) 
XX = enc.fit_transform(X)
XX = XX.toarray()
print(XX.shape)

enc.fit(xTraining) 
xTraining1 = enc.fit_transform(xTraining)
xTraining1 = xTraining1.toarray()
print(xTraining1.shape)

enc.fit(xTest) 
xTest1 = enc.fit_transform(xTest)
xTest1 = xTest1.toarray()
print(xTest1.shape)'''
    

bern = SVC(kernel='linear')
bern.fit(xTraining, yTraining1)
yPredict6 = bern.predict(xTest)
print(metrics.accuracy_score(yTest,yPredict6))
print(metrics.confusion_matrix(yTest,yPredict6))
error6 = mean_squared_error(yTest, yPredict6)
print("error 6 = ",error6)
RMSE6 = mean_squared_error(yTest,yPredict6)**0.5
print("RMSE6 = ",RMSE6)

fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict6)
print("AUC = ", metrics.auc(fpr,tpr))
print("recal = ",metrics.recall_score(yTest,yPredict6))
print("precision = ",metrics.precision_score(yTest,yPredict6))
print("f1Score = ",f1_score(yTest, yPredict6, average='binary'))

r1['confu'] = metrics.confusion_matrix(yTest,yPredict6)
r1['accu'] = metrics.accuracy_score(yTest,yPredict6)
r1['auc'] = metrics.auc(fpr,tpr)
r1['recal'] = metrics.recall_score(yTest,yPredict6)
r1['precision'] = metrics.precision_score(yTest,yPredict6)
r1['f1'] = f1_score(yTest, yPredict6, average='binary')
   
writeResults2['all'] = r1
    
print()

results = crossValidiation(X, Y, k = 10,classifier = bern, continious = False)
results['down'] = down
results['up'] = up
results['thres'] = threshold  
print("accuracy = ",results['accuracy'])
print("AUC = ", results['auc'])
print("recal = ",results['recall'])
print("precision = ",results['precision'])
print("f1 = ",results['f1'])


results['len_snps'] = len(X.T)
writeResults['all'] = results
    
print()
    
    
writeResult(path,'svm_all',writeResults,'all')
#writeResultConf(path,'svm_all',writeResults2)
r1 = {}
writeResults2={}
writeResults={}

'''enc = OneHotEncoder(n_values =3) 
enc.fit(X) 
XX = enc.fit_transform(X)
XX = XX.toarray()
print(XX.shape)

enc.fit(xTraining) 
xTraining1 = enc.fit_transform(xTraining)
xTraining1 = xTraining1.toarray()
print(xTraining1.shape)

enc.fit(xTest) 
xTest1 = enc.fit_transform(xTest)
xTest1 = xTest1.toarray()
print(xTest1.shape)'''
    

bern = linear_model.LogisticRegressionCV()
bern.fit(xTraining, yTraining1)
yPredict6 = bern.predict(xTest)
print(metrics.accuracy_score(yTest,yPredict6))
print(metrics.confusion_matrix(yTest,yPredict6))
error6 = mean_squared_error(yTest, yPredict6)
print("error 6 = ",error6)
RMSE6 = mean_squared_error(yTest,yPredict6)**0.5
print("RMSE6 = ",RMSE6)

fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict6)
print("AUC = ", metrics.auc(fpr,tpr))
print("recal = ",metrics.recall_score(yTest,yPredict6))
print("precision = ",metrics.precision_score(yTest,yPredict6))
print("f1Score = ",f1_score(yTest, yPredict6, average='binary'))

r1['confu'] = metrics.confusion_matrix(yTest,yPredict6)
r1['accu'] = metrics.accuracy_score(yTest,yPredict6)
r1['auc'] = metrics.auc(fpr,tpr)
r1['recal'] = metrics.recall_score(yTest,yPredict6)
r1['precision'] = metrics.precision_score(yTest,yPredict6)
r1['f1'] = f1_score(yTest, yPredict6, average='binary')
   
#writeResults2['all'] = r1

print()

results = crossValidiation(X, Y, k = 10,classifier = bern, continious = False)
results['down'] = down
results['up'] = up
results['thres'] = threshold    
print("accuracy = ",results['accuracy'])
print("AUC = ", results['auc'])
print("recal = ",results['recall'])
print("precision = ",results['precision'])
print("f1 = ",results['f1'])


results['len_snps'] = len(X.T)
writeResults['all'] = results
    
print()
    
    
writeResult(path,'llr_all_CV',writeResults,'all')
#writeResultConf(path,'llr_all_CV',writeResults2)


r1 = {}
writeResults2={}
writeResults={}

#enc = OneHotEncoder(n_values =3) 
#enc.fit(X) 
#XX = enc.fit_transform(X)
#XX = XX.toarray()
#print(XX.shape)

#enc.fit(xTraining) 
#xTraining1 = enc.fit_transform(xTraining)
#xTraining1 = xTraining1.toarray()
#print(xTraining1.shape)

#enc.fit(xTest) 
#xTest1 = enc.fit_transform(xTest)
#xTest1 = xTest1.toarray()
#print(xTest1.shape)
    

bern = tree.DecisionTreeClassifier()
bern.fit(xTraining, yTraining1)
yPredict6 = bern.predict(xTest)
print(metrics.accuracy_score(yTest,yPredict6))
print(metrics.confusion_matrix(yTest,yPredict6))
error6 = mean_squared_error(yTest, yPredict6)
print("error 6 = ",error6)
RMSE6 = mean_squared_error(yTest,yPredict6)**0.5
print("RMSE6 = ",RMSE6)

fpr, tpr, thresholds = metrics.roc_curve(yTest,yPredict6)
print("AUC = ", metrics.auc(fpr,tpr))
print("recal = ",metrics.recall_score(yTest,yPredict6))
print("precision = ",metrics.precision_score(yTest,yPredict6))
print("f1Score = ",f1_score(yTest, yPredict6, average='binary'))

r1['confu'] = metrics.confusion_matrix(yTest,yPredict6)
r1['accu'] = metrics.accuracy_score(yTest,yPredict6)
r1['auc'] = metrics.auc(fpr,tpr)
r1['recal'] = metrics.recall_score(yTest,yPredict6)
r1['precision'] = metrics.precision_score(yTest,yPredict6)
r1['f1'] = f1_score(yTest, yPredict6, average='binary')
   
writeResults2['all'] = r1

print()

results = crossValidiation(X, Y, k = 10,classifier = bern, continious = False)
results['down'] = down
results['up'] = up
results['thres'] = threshold    
print("accuracy = ",results['accuracy'])
print("AUC = ", results['auc'])
print("recal = ",results['recall'])
print("precision = ",results['precision'])
print("f1 = ",results['f1'])


results['len_snps'] = len(X.T)
writeResults['all'] = results
    
print()
    
    
writeResult(path,'tree_all',writeResults,'all')
#writeResultConf(path,'tree_all',writeResults2)
featuresName = []
for i in range(len(ids['snps']['idToName'])):
   # snp = ids['snps']['idToName'][i]
    featuresName.append(ids['snps']['idToName'][i])
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
treeee = open(path+'all_balancedTree.dot','w')
dot_data = tree.export_graphviz(clf, out_file=treeee, 
                            feature_names=featuresName,leaves_parallel =True,max_depth = 6)  
graph = graphviz.Source(dot_data)
r1={}
writeResults={}

    
    
    
clf = RandomForestClassifier()
#clf.fit(xTraining1, yTraining1)
   
param_grid = {
              "n_estimators" : [9, 18, 27, 36, 45, 54, 63],
              "min_samples_split": [2, 3, 10],
              "min_samples_leaf": [1, 3, 10],
              "bootstrap": [True, False],
              "criterion": ["gini", "entropy"],
               "warm_start":[True,False]}
    
'''param_grid = { 
         'n_estimators':[2000,3000,5000], 
         'max_depth':[5,15,30], 
         'min_samples_split':[2,3], 
        'min_samples_leaf':[1,2],
        "warm_start":[True,False],
                "oob_score":[True,False]}
'''
    
    # run grid search
grid_search = GridSearchCV(estimator=clf, param_grid=param_grid,cv = 10,verbose=1)
    


results = crossValidiation(X, Y, k = 10, classifier = grid_search, continious = False)
results['down'] = down
results['up'] = up
results['thres'] = threshold
print("accuracy = ",results['accuracy'])
print("AUC = ", results['auc'])
print("recal = ",results['recall'])
print("precision = ",results['precision'])
print("f1 = ",results['f1'])
results['len_snps'] = len(X.T)
   # results['len snps'] = 500
writeResults['all'] = results
    
print()
    
    
writeResult(path,'rfrall',writeResults,'all')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16294153984388163, 'fraction_numerical': 0.029932049194137136, 'mean_word_length': 4.39457740078764, 'pattern_counts': {'":': 14, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '349156', 'license': 'MIT', 'n_tokens_mistral': 14400, 'n_tokens_neox': 13639, 'n_words': 1951, 'path': 'code/.ipynb_checkpoints/melanoma-checkpoint.ipynb', 'repo_name': 'niksell/phenotypes-prediction-using-genotypes-Master-Thesis', 'size': '35614'}