<jupyter_start><jupyter_text>https://github.com/sajit9285/DNA-Classification-Project/blob/master/DNA%20Classification%20Code.ipynb \https://www.kaggle.com/thomasnelson/working-with-dna-sequence-data-for-ml \https://github.com/SunHaozhe/Kernel-method-DNA-sequence \<jupyter_code>!cat /proc/meminfo | grep Mem
import pandas as pd
from pandas.api.types import CategoricalDtype 
import numpy as np
import random
#Importing different classifier from sklearn
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import svm
from sklearn.naive_bayes import GaussianNB
from sklearn.gaussian_process.kernels import RBF
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.metrics import classification_report, accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
orig_path = "/content/drive/MyDrive/Kernel_Challenge/kernel_data/"<jupyter_output><empty_output><jupyter_text>Load & Merge Data<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
def merge_X_y(path_X, path_Y):
  X = pd.read_csv(orig_path + path_X)
  y = pd.read_csv(orig_path + path_Y)
  data = pd.merge(X, y, on=['Id']).drop('Id', axis=1).rename(columns={'Bound':'target'})
  return data
data_0 = merge_X_y('Xtr0.csv', 'Ytr0.csv')
data_1 = merge_X_y('Xtr1.csv', 'Ytr1.csv')
data_2 = merge_X_y('Xtr2.csv', 'Ytr2.csv')<jupyter_output><empty_output><jupyter_text>Information<jupyter_code>data_0.describe()
data_1.describe()
data_2.describe()<jupyter_output><empty_output><jupyter_text>All three of the dataset seem to be balanced :)<jupyter_code>seq_length_0 = []
for x in data_0.seq:
  seq_length_0.append(len(x))
print(f'The minimum sequence of data_0 is {min(seq_length_0)}')
print(f'The maximum sequence of data_0 is {max(seq_length_0)}')
seq_length_1 = []
for x in data_1.seq:
  seq_length_1.append(len(x))
print(f'The minimum sequence of data_1 is {min(seq_length_1)}')
print(f'The maximum sequence of data_1 is {max(seq_length_1)}')
seq_length_2 = []
for x in data_2.seq:
  seq_length_2.append(len(x))
print(f'The minimum sequence of data_2 is {min(seq_length_2)}')
print(f'The maximum sequence of data_2 is {max(seq_length_2)}')<jupyter_output>The minimum sequence of data_0 is 101
The maximum sequence of data_0 is 101
The minimum sequence of data_1 is 101
The maximum sequence of data_1 is 101
The minimum sequence of data_2 is 101
The maximum sequence of data_2 is 101<jupyter_text>Features Engineering  Simple HotEncode<jupyter_code>def transform_data(X):
  splitted_sequences = []
  for i, seq in enumerate(X.seq):
    splitted_sequences.append([nucleo for nucleo in seq])
  mapping = {num : sequences for num, sequences in enumerate(splitted_sequences)}
  features_data = pd.DataFrame(mapping).T
  for i in range(len(features_data.columns)):
    features_data[i] = features_data[i].astype(CategoricalDtype(['A', 'C', 'G', 'T']))
  encoded_data = pd.get_dummies(features_data)
  return encoded_data
data_0_encoded = transform_data(data_0)
data_1_encoded = transform_data(data_1)
data_2_encoded = transform_data(data_2)
#define a seed for reproducibility
seed = 1
X_0 = data_0_encoded.values
y_0 = data_0['target'].values
# Splitting data into training and testing data
X_train_0, X_test_0, y_train_0, y_test_0 = train_test_split(X_0, y_0, test_size = 0.25, random_state = seed)
scoring = 'accuracy'
# Model building to train
names = ['K Nearest Neighbors', 'Gaussian Process', 'Decision Tree', 'Random Forest', 'Neural Net', 'AddaBoost', 'Naive Bayes', 'SVM Linear', 'SVM RBF', 'SVM Sigmoid']
Classifiers = [
    KNeighborsClassifier(n_neighbors = 3),
    GaussianProcessClassifier(1.0*RBF(1.0)),
    DecisionTreeClassifier(max_depth = 5),
    RandomForestClassifier(max_depth = 5, n_estimators = 10, max_features = 1 ),
    MLPClassifier(alpha = 1),
    AdaBoostClassifier(),
    GaussianNB(),
    svm.SVC(kernel = 'linear'),
    svm.SVC(kernel = 'rbf'),
    svm.SVC(kernel = 'sigmoid')
    
    ]
models = zip(names, Classifiers)
# import KFold
from sklearn.model_selection import KFold, cross_val_score

names = []
result = []
for name, model in models:
    kfold = KFold(n_splits = 10, random_state = 1)
    cv_results = cross_val_score(model, X_train_0, y_train_0, cv = kfold, scoring = 'accuracy')
    result.append(cv_results)
    names.append(name)
    msg = "{0}: {1} ({2})".format(name, cv_results.mean(), cv_results.std())
    print(msg)<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_split.py:296: FutureWarning: Setting a random_state has no effect since shuffle is False. This will raise an error in 0.24. You should leave random_state to its default (None), or set shuffle=True.
  FutureWarning<jupyter_text>Kmers  CountVectorizer<jupyter_code>def getKmers(sequence, size, overlap=1):
    return [sequence[x:x+size].lower() for x in np.arange(0, len(sequence) - size + 1, overlap)] 
def transform_data(X):
  sequences = []
  for seq in X.seq:
    kmers = getKmers(seq, 6)
    sentence = ' '.join(kmers)
    sequences.append(sentence)
  cv = CountVectorizer()
  encoded = cv.fit_transform(sequences)
  return encoded.toarray()
data_0_encoded = transform_data(data_0)
data_1_encoded = transform_data(data_1)
data_2_encoded = transform_data(data_2)
#define a seed for reproducibility
seed = 42
X_0 = data_0_encoded
y_0 = data_0['target'].values
# Splitting data into training and testing data
X_train_0, X_test_0, y_train_0, y_test_0 = train_test_split(X_0, y_0, test_size = 0.20, random_state = seed)
scoring = 'accuracy'
# Model building to train
names = ['K Nearest Neighbors', 'Gaussian Process', 'Random Forest', 'AddaBoost', 'SVM Linear', 'SVM RBF', 'SVM Sigmoid']
Classifiers = [
    KNeighborsClassifier(n_neighbors = 3),
    GaussianProcessClassifier(1.0*RBF(1.0)),
    RandomForestClassifier(max_depth = 5, n_estimators = 100, max_features = 1 ),
    AdaBoostClassifier(),
    svm.SVC(kernel = 'linear'),
    svm.SVC(kernel = 'rbf'),
    svm.SVC(kernel = 'sigmoid')
    ]
models = zip(names, Classifiers)
# import KFold
from sklearn.model_selection import KFold, cross_val_score

names = []
result = []
for name, model in models:
    kfold = KFold(n_splits = 10, random_state = 1)
    cv_results = cross_val_score(model, X_train_0, y_train_0, cv = kfold, scoring = 'accuracy')
    result.append(cv_results)
    names.append(name)
    msg = "{0}: {1} ({2})".format(name, cv_results.mean(), cv_results.std())
    print(msg)<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_split.py:296: FutureWarning: Setting a random_state has no effect since shuffle is False. This will raise an error in 0.24. You should leave random_state to its default (None), or set shuffle=True.
  FutureWarning<jupyter_text>TFIDF<jupyter_code>def getKmers(sequence, size, overlap=1):
    return [sequence[x:x+size].lower() for x in np.arange(0, len(sequence) - size + 1, overlap)] 
def transform_data(X):
  sequences = []
  for seq in X.seq:
    kmers = getKmers(seq, 6)
    sentence = ' '.join(kmers)
    sequences.append(sentence)
  tfidf = TfidfVectorizer()
  encoded = tfidf.fit_transform(sequences)
  return encoded.toarray()
data_0_encoded = transform_data(data_0)
data_1_encoded = transform_data(data_1)
data_2_encoded = transform_data(data_2)
#define a seed for reproducibility
seed = 42
X_0 = data_0_encoded
y_0 = data_0['target'].values
# Splitting data into training and testing data
X_train_0, X_test_0, y_train_0, y_test_0 = train_test_split(X_0, y_0, test_size = 0.20, random_state = seed)
scoring = 'accuracy'
# Model building to train
names = ['K Nearest Neighbors', 'Gaussian Process', 'Random Forest', 'AddaBoost', 'SVM Linear', 'SVM RBF', 'SVM Sigmoid']
Classifiers = [
    KNeighborsClassifier(n_neighbors = 3),
    GaussianProcessClassifier(1.0*RBF(1.0)),
    RandomForestClassifier(max_depth = 5, n_estimators = 100, max_features = 1 ),
    AdaBoostClassifier(),
    svm.SVC(kernel = 'linear'),
    svm.SVC(kernel = 'rbf'),
    svm.SVC(kernel = 'sigmoid')
    ]
models = zip(names, Classifiers)
# import KFold
from sklearn.model_selection import KFold, cross_val_score

names = []
result = []
for name, model in models:
    kfold = KFold(n_splits = 10, random_state = 1)
    cv_results = cross_val_score(model, X_train_0, y_train_0, cv = kfold, scoring = 'accuracy')
    result.append(cv_results)
    names.append(name)
    msg = "{0}: {1} ({2})".format(name, cv_results.mean(), cv_results.std())
    print(msg)
def getKmers(sequence, size, overlap=1):
    return [sequence[x:x+size].lower() for x in np.arange(0, len(sequence) - size + 1, overlap)] 
def transform_data(X):
  sequences = []
  for seq in X.seq:
    kmers = getKmers(seq, 6)
    sentence = ' '.join(kmers)
    sequences.append(sentence)
  cv = CountVectorizer()
  encoded = cv.fit_transform(sequences)
  return encoded.toarray()
data_0_encoded = transform_data(data_0)
data_1_encoded = transform_data(data_1)
data_2_encoded = transform_data(data_2)
#define a seed for reproducibility
seed = 42
X_1 = data_1_encoded
y_1 = data_1['target'].values
# Splitting data into training and testing data
X_train_1, X_test_1, y_train_1, y_test_1 = train_test_split(X_1, y_1, test_size = 0.20, random_state = seed)
scoring = 'accuracy'
# Model building to train
names = ['K Nearest Neighbors', 'Gaussian Process', 'SVM Linear', 'SVM RBF', 'SVM Sigmoid']
Classifiers = [
    KNeighborsClassifier(n_neighbors = 3),
    GaussianProcessClassifier(1.0*RBF(1.0)),
    svm.SVC(kernel = 'linear'),
    svm.SVC(kernel = 'rbf'),
    svm.SVC(kernel = 'sigmoid')
    ]
models = zip(names, Classifiers)
# import KFold
from sklearn.model_selection import KFold, cross_val_score

names = []
result = []
for name, model in models:
    kfold = KFold(n_splits = 10, random_state = 1)
    cv_results = cross_val_score(model, X_train_1, y_train_1, cv = kfold, scoring = 'accuracy')
    result.append(cv_results)
    names.append(name)
    msg = "{0}: {1} ({2})".format(name, cv_results.mean(), cv_results.std())
    print(msg)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12048780487804878, 'fraction_numerical': 0.023902439024390244, 'mean_word_length': 5.367080745341615, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '477637', 'license': 'Apache-2.0', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3662, 'n_words': 858, 'path': 'notebook_experiment/Draft.ipynb', 'repo_name': 'omarsou/kernel_method_kaggle_challenge', 'size': '10250'}