<jupyter_start><jupyter_text>1. Modulos<jupyter_code>import sys
import os.path
from os import path

def is_runing_into_colab_env():
    try:
        import google.colab
        return True
    except:
        return False

if is_runing_into_colab_env():
    if path.exists('/content/proyecto_vocales'):
        print('Pull...\n')
        !cd proyecto_vocales; git pull origin main
    else:
        print('Clone...\n')
        !git clone https://github.com/ccollado7/proyecto_vocales

if is_runing_into_colab_env():
    !pip install praat-parselmouth
if is_runing_into_colab_env():
    sys.path.append('/content/proyecto_vocales/src')
else:
    #sys.path.append(os.path.join(os.getcwd(),'src'))
    os.chdir('src')
#Generales
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

import io
import glob
from pathlib import Path

#Audio
import parselmouth
from parselmouth import praat


#propios
from fwav2df import extraer_formantes, \
                    armar_dataset, \
                    armar_dataset_audios_procesados, \
                    extraer_praat

from graph import graficar_svm

#No Supervisados
from sklearn.cluster import KMeans
from sklearn import svm

#Supervisados
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder<jupyter_output><empty_output><jupyter_text>2. Lectura de datos - Dataframe Leer archivos y armar el dataset<jupyter_code>url = 'https://raw.githubusercontent.com/ccollado7/proyecto_vocales/main/data/raw/features_'
df=armar_dataset_audios_procesados(url)
df.head()<jupyter_output><empty_output><jupyter_text>**Función de extracción de formantes**Si bien los audios con vocales son señales temporales, se aproximan por sus formates del espectro.`(https://www.fceia.unr.edu.ar/prodivoz/home_index.html)` | [Prodivoz](https://www.fceia.unr.edu.ar/prodivoz/home_index.html)<jupyter_code>carpeta_wav=glob.glob('proyecto_vocales/data/raw/wav/*.wav')
df=armar_dataset(carpeta_wav,funcion='lpc_rf')
df.head()
df.columns<jupyter_output><empty_output><jupyter_text>**Alternativa para extraer formantes Praat**`(https://parselmouth.readthedocs.io/en/stable/)` | [Parselmouth](https://parselmouth.readthedocs.io/en/stable/)<jupyter_code>carpeta_wav=glob.glob('proyecto_vocales/data/raw/wav/*.wav')
df=armar_dataset(carpeta_wav,funcion='praat')
df.head()<jupyter_output><empty_output><jupyter_text>3. Analisis Exploratorio<jupyter_code>plt.figure(figsize=(15,8))
sns.scatterplot(x=0,y=1,hue='target',palette={'A':'r','E':'b','I':'k','O':'g','U':'y'},data=df,legend='brief')<jupyter_output><empty_output><jupyter_text>3.1 Atributo 0<jupyter_code>plt.figure(figsize=(15,8))
sns.kdeplot(data=df, x=0, hue="target")
sns.catplot(x="target", y=0, kind="box", data=df)<jupyter_output><empty_output><jupyter_text>3.2 Atributo 2<jupyter_code>plt.figure(figsize=(15,8))
sns.kdeplot(data=df, x=1, hue="target")
sns.catplot(x="target", y=1, kind="box", data=df)<jupyter_output><empty_output><jupyter_text>3.3 Target<jupyter_code>sns.countplot(x="target", data=df)<jupyter_output><empty_output><jupyter_text>4. Metodos No Supervisados  4.1 Método kmeans<jupyter_code>X=df[[0,1]]
X.head()
y=df[['target']]
y.head()
kmeans = KMeans(n_clusters=5).fit(X)
#print(kmeans.labels_)
print(kmeans.cluster_centers_)
plt.figure(1, figsize=(8, 6))
plt.clf()
plt.scatter(X[0], X[1],c=kmeans.labels_, alpha=.5, edgecolor='k')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, marker = '*', c='k', edgecolor=None)
plt.xlabel('Formante 1 [Hz]')
plt.ylabel('Formante 2 [Hz]')
plt.xticks(())
plt.yticks(())<jupyter_output><empty_output><jupyter_text>Análisis de clusters<jupyter_code>tmp = pd.DataFrame({'Labels': df.target, 'Clusters': kmeans.labels_})# Create a DataFrame with labels and varieties as columns: df
ct = pd.crosstab(tmp['Labels'], tmp['Clusters']) # Create crosstab: ct
print(ct)
from sklearn.metrics import adjusted_rand_score
ari0 = adjusted_rand_score(df.target, kmeans.labels_)
print(ari0)<jupyter_output>0.667814355479043<jupyter_text>De la matriz de confusión surge que las mayores confusiones se dan entre la E y la I.  4.2 Método SVM Primero pruebo con dos vocales para hacer SVM binario. Pruebo con la E e I que aparecen algunas confusines en kmeans<jupyter_code>df2=df.loc[(df['target'] == 'E') | (df['target'] == 'I')]
X=np.array(df2[[0,1]].values)
y=df2.iloc[:,2:].values.ravel()

C = 1.0  # SVM regularization parameter
clf = svm.SVC(kernel='linear', C=C)
clf.fit(X, y)<jupyter_output><empty_output><jupyter_text>Los vectores son<jupyter_code>clf.support_vectors_<jupyter_output><empty_output><jupyter_text>código para dibujar<jupyter_code>codes = {'E': 0, 'I': 1}
graficar_svm(X,y,clf,codes,alpha=0.8)<jupyter_output><empty_output><jupyter_text>SVM multiclase<jupyter_code>X=np.array(df[[0,1]].values)
y=df.iloc[:,2:].values.ravel()

C = 1.0  # SVM regularization parameter
clf = svm.SVC(kernel='poly',degree=3, C=C) 
clf.fit(X, y)
codes = {'A':0,'E': 1, 'I': 2, 'O':3, 'U':4}
graficar_svm(X,y,clf,codes,alpha=0.2)<jupyter_output><empty_output><jupyter_text>5. Metodos Supervisados  5.1 Separacion train / test<jupyter_code>x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=42,stratify=y)
x_train.shape
y_train.shape
y_train.value_counts()
x_test.shape
y_test.shape
y_test.value_counts()<jupyter_output><empty_output><jupyter_text>5.2 Estandarizacion features numericos - Encoding del target Estandarizacion de los features numericos<jupyter_code>num_scaler = StandardScaler()
x_train = num_scaler.fit_transform(x_train)
x_test = num_scaler.transform(x_test)<jupyter_output><empty_output><jupyter_text>Encoding del target categorico<jupyter_code>cat_encoder = LabelEncoder()
y_train = cat_encoder.fit_transform(y_train)
y_test = cat_encoder.transform(y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14441645675902604, 'fraction_numerical': 0.017296389588581023, 'mean_word_length': 6.909694555112882, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 68, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '142769', 'license': 'MIT', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2376, 'n_words': 373, 'path': 'k_means_vocales.ipynb', 'repo_name': 'ccollado7/proyecto_vocales', 'size': '5955'}