<jupyter_start><jupyter_text>Objective: Classify echo curves based on Stencil type  Loading Data<jupyter_code>import os, sys
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns ; sns.set()

from google.colab import drive
drive.mount('/content/drive')

sys.path.append("/content/drive/MyDrive/GSOC-NMR-project/Work/Notebooks")
from auxillary_functions import *
from polynomial_featextract import poly_featextract
# import raw data and params.txt file

datadir_path = "/content/drive/MyDrive/GSOC-NMR-project/Work/Data/2021-06-21_classify_datagen_all_funcs"
raw_data = load_data(path=datadir_path,as_df=False)
print("Shape of Raw Data:",raw_data.shape)
params_data = load_params(path=datadir_path)
# Stencil type : {'0' : 'Gaussian', '1' : 'Power Law', '2' : 'RKKY'}
### Selecting a time-window of 150 steps around the echo-pulse

offset = 150
shifted_data, center = get_window(raw_data,2/3,width=offset)
print("The Echo pulse occurs at timestep:",center)

# Rescaled data
rscl_data = shifted_data / np.max(shifted_data,axis=1,keepdims=True)<jupyter_output>The Echo pulse occurs at timestep: 628<jupyter_text>Machine LearningHere we use 5-fold cross-validation for each model<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PowerTransformer, QuantileTransformer
from sklearn.pipeline import Pipeline

# Linear Models

from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV, SGDClassifier
from sklearn.naive_bayes import GaussianNB, BernoulliNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.gaussian_process import GaussianProcessClassifier
# Tree models
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, ExtraTreesClassifier
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score, cross_validate<jupyter_output><empty_output><jupyter_text>Linear Models<jupyter_code># Setup Pipeline

def linear_pipeline(model):
    pipe = Pipeline([
            ('scaler', QuantileTransformer(output_distribution='normal')),
            ('model', model)     
    ])
    return pipe

def tree_pipeline(model):
    return Pipeline([('model', model)])

def get_model_statistics(model,X,y, linear=True):
    """For the given model (linear or tree type) computes the 5-fold CV metrics 
    and returns a dataframe object with metric statistics"""
    modelname = str(model).split('(')[0]
    print(f"Running CV for model: {modelname}")
    
    scores = ['accuracy','precision_weighted','recall_weighted','f1_weighted','roc_auc_ovr_weighted']

    if linear == True:
        results_kf = cross_validate(estimator=linear_pipeline(model), 
                                    verbose=True, X=X, y=y,
                                    scoring=scores, cv=5, n_jobs=-1)
        results_kf_df = pd.DataFrame(results_kf)
    
    if linear == False:
        results_kf = cross_validate(estimator=tree_pipeline(model), verbose=True,
                            X=X, y=y, scoring=scores, cv=5, n_jobs=-1)
        results_kf_df = pd.DataFrame(results_kf)


    return {f'{str(modelname)}_Mean': dict(results_kf_df.mean()), 
            f'{str(modelname)}_Std': dict(results_kf_df.std()),
            f'{str(modelname)}_params' : str(model)}
X, y = rscl_data, params_data['stencil_type'].values
# Try out for SVC 

svm = LinearSVC(multi_class='ovr',random_state=0)
res = get_model_statistics(svm, X,y, linear=True)
res
# Try out ovevsrest SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
res2 = get_model_statistics(OneVsRestClassifier(SVC()), X,y, linear=True)
res2
    
Linearmodels = [KNeighborsClassifier(n_neighbors=3, n_jobs=-1),
                LogisticRegression(multi_class='ovr',n_jobs=-1, max_iter=800),
                LogisticRegression(multi_class='multinomial',n_jobs=-1, max_iter=800),
                SGDClassifier(loss='modified_huber',shuffle=True, random_state=0), 
                GaussianProcessClassifier(random_state=0, multi_class='ovr',n_jobs=-1),
                GaussianNB(), BernoulliNB()]
LinearResults = []
for model in Linearmodels:
    LinearResults.append(get_model_statistics(model, X,y, linear=True))
    print()
# path = "/content/drive/MyDrive/GSOC-NMR-project/Work/Notebooks"
with open("/content/drive/MyDrive/GSOC-NMR-project/Work/Notebooks/linearmodel_classification_results.txt", 'w') as wf:
    wf.write(str(LinearResults))<jupyter_output><empty_output><jupyter_text>Tree based models<jupyter_code>treemodels = [DecisionTreeClassifier(), 
              ExtraTreesClassifier(), 
              #GradientBoostingClassifier(), 
              RandomForestClassifier()]
# Try GradientBoostingClassifier later
get_model_statistics(RandomForestClassifier(), X,y, linear=False)
TreeResults = []
for model in treemodels:
    TreeResults.append(get_model_statistics(model, X,y, linear=False))
    print()
with open("/content/drive/MyDrive/GSOC-NMR-project/Work/Notebooks/treemodel_classification_results_ts.txt", 'w') as wf:
    wf.write(str(TreeResults))<jupyter_output><empty_output><jupyter_text>Classification metricsAll metrics are somehow associated and can be derived from the confusion matrix. 1. Precision2. Recall3. F1-score4. ROC-AUC score (AUROC)5. Cohen kappa score6. Matthew's correlaton coefficient7. Log loss<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10562488695966721, 'fraction_numerical': 0.009947549285585097, 'mean_word_length': 4.965480043149946, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '193966', 'license': 'MIT', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1786, 'n_words': 360, 'path': 'Week3/Jun26_classification_tsdata.ipynb', 'repo_name': 'Anantha-Rao12/NMR-quantumstates-GSOC21', 'size': '5529'}