<jupyter_start><jupyter_text>Classificação de Vetores de Caracteríticas de Janelas<jupyter_code>import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn
import os 
PATH = os.getcwd().replace('Notebooks','')

eeg = np.load(PATH+'vars/feature_windows_eeg.npy')
ecg = np.load(PATH+'vars/feature_windows_ecg.npy')
emg = np.load(PATH+'vars/feature_windows_emg.npy')<jupyter_output><empty_output><jupyter_text>Funções Auxiliares<jupyter_code>from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix, auc, roc_curve
from sklearn import preprocessing

def org(data):
    le = preprocessing.LabelEncoder()
    X = data[:,:-1]
    y = le.fit(data[:,-1]).transform(data[:,-1])
    return X,y

def conf_matrix(y, y_pred):
    m = confusion_matrix(y, y_pred)
    return m.astype('float')/m.sum(axis=1)[:, np.newaxis]

def plot_cm(cm):
    con_df = pd.DataFrame(data=cm, columns=['No', 'Yes'])
    sns.heatmap(con_df, annot=True, cmap=plt.cm.Blues)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

def metrics(cm):
    VP, FN, FP, VN = cm.ravel()
    names = ["ACU: ","SENS: ","ESP: "]
    return np.array([np.round(100*(VP+VN)/cm.sum(),decimals=4),
            np.round((100*VP/(VP+FN)), decimals=4), 
          np.round((100*VN/(VN+FP)), decimals=4)])
    # print(names[0], 
        #   np.round(100*(VP+VN)/cm.sum(), 
                #    decimals=4))
    # print(names[1], 
        #   np.round((100*VP/(VP+FN)), decimals=4))
    # print(names[2], 
        #   np.round((100*VN/(VN+FP)), decimals=4))<jupyter_output><empty_output><jupyter_text>Classificadores<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB


def KNN(X,y):
    knn_ = KNeighborsClassifier(n_neighbors=5)
    y_pred = cross_val_predict(knn_, X, y, cv=10)
    # y_prob = cross_val_predict(knn_, X, y, cv=10, method='predict_proba')
    cm = conf_matrix(y, y_pred)
    return metrics(cm)

def svm_rbf(X,y):
    svc_ = SVC(kernel='rbf',probability=True)
    y_pred = cross_val_predict(svc_, X, y, cv=10)
    # y_prob = cross_val_predict(svc_, X, y, cv=10,method='predict_proba')
    cm = conf_matrix(y, y_pred)
    return metrics(cm)


def svm_poly(X,y):
    svc_ = SVC(kernel='poly',probability=True)
    y_pred = cross_val_predict(svc_, X, y, cv=10)
    # y_prob = cross_val_predict(svc_, X, y, cv=10,method='predict_proba')
    cm = conf_matrix(y, y_pred)
    return metrics(cm)

def nb(X,y):
    nb_ = GaussianNB()
    y_pred = cross_val_predict(nb_, X, y, cv=10)
    # y_prob = cross_val_predict(nb_, .X, .y, cv=10,method='predict_proba')
    cm = conf_matrix(y, y_pred)
    return metrics(cm)
        # return roc_curve(y,y_prob[:,1])
        
def lda(X,y):
    lda_ = LinearDiscriminantAnalysis()
    y_pred = cross_val_predict(lda_, X, y, cv=10)
    # y_prob = cross_val_predict(lda_, .X, .y, cv=10,method='predict_proba')
    cm = conf_matrix(y, y_pred)
    return metrics(cm)
    # return roc_curve(y,y_prob[:,1])

def qda(X,y):
    qda_ = QuadraticDiscriminantAnalysis()
    y_pred = cross_val_predict(qda_, X, y, cv=10)
    # y_prob = cross_val_predict(qda_, self.X, self.y, cv=10,method='predict_proba')
    cm = conf_matrix(y, y_pred)
    return metrics(cm)
    # return roc_curve(y,y_prob[:,1])<jupyter_output><empty_output><jupyter_text>Classificação EEG<jupyter_code>X, y = org(eeg)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)<jupyter_output><empty_output><jupyter_text>Classficação ECG<jupyter_code>X, y = org(ecg)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)<jupyter_output><empty_output><jupyter_text>Classficação EMG<jupyter_code>X, y = org(emg)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)<jupyter_output><empty_output><jupyter_text>Combinando Sinais  EEG - ECG - EMG<jupyter_code>aux = np.hstack((eeg[:,:-1],ecg[:,:-1]))
aux2 = np.hstack((aux,emg))

X, y = org(aux2)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)<jupyter_output><empty_output><jupyter_text>ECG - EMG<jupyter_code>aux = np.hstack((ecg[:,:-1],emg))
# aux2 = np.hstack((aux,emg))

X, y = org(aux)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)<jupyter_output><empty_output><jupyter_text>EEG - ECg<jupyter_code>aux = np.hstack((eeg[:,:-1],ecg))
# aux2 = np.hstack((aux,emg))

X, y = org(aux)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)<jupyter_output><empty_output><jupyter_text>EEG - EMG<jupyter_code>aux = np.hstack((eeg[:,:-1],emg))
# aux2 = np.hstack((aux,emg))

X, y = org(aux)

met = []
index_class = ["knn","svm_rbf","svm_poly","nb","lda","qda"]
col_metrics = ['ac','sens','esp']
met.append(KNN(X,y))
met.append(svm_rbf(X,y))
met.append(svm_poly(X,y))
met.append(nb(X,y))
met.append(lda(X,y))
met.append(qda(X,y))

pd.DataFrame(data=np.array(met),columns=col_metrics,index=index_class)
emg.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19321109258978633, 'fraction_numerical': 0.01045612971662373, 'mean_word_length': 6.112068965517241, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '503826', 'license': 'MIT', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2874, 'n_words': 368, 'path': 'Notebooks/classifiers.ipynb', 'repo_name': 'davicn/tuh_seizures', 'size': '6599'}