<jupyter_start><jupyter_text>Data Preparation<jupyter_code>import os
import sys

from tensorflow.keras import layers, callbacks
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split;
import numpy
import pandas
from sklearn.pipeline import Pipeline
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import to_categorical

WORKING_DIR_PATH = globals()['_dh'][0]
WORKING_DIR_PARENT_PATH = os.path.dirname(WORKING_DIR_PATH)
sys.path.insert(1, WORKING_DIR_PARENT_PATH)
from custom_module.utilities import *
features = pandas.read_csv(LOCAL_MOUNTED_DATASET_PATH + '/data/cleaned_features.csv', index_col=0)
features
y = features.loc[:,'genre_label']
X = features.loc[:, features.columns.difference(['genre_label'])]
X
y.value_counts(normalize=True)
wavelet_predictors = X.filter(regex=(r'.+_db[458]{1}_.+'))
wavelet_predictor_labels = wavelet_predictors.columns.values
timbral_rhythmic_predictors = X.loc[:, features.columns.difference(
    numpy.append(wavelet_predictor_labels, 'genre_label'))]
timbral_rhythmic_predictors
predictors_all_label = timbral_rhythmic_predictors.columns.values
predictors_with_outliers_label = ['mean_beats', 'mean_spec_bw', 'mean_spec_centroid', 'mean_spec_rolloff',
                          'median_beats','median_spec_bw', 'median_spec_centroid', 
                          'median_spec_rolloff', 'std_beats', 'std_mfcc_1', 'var_spec_centroid',
                          'var_spec_rolloff'
                         ]
predictors_without_outliers_label = timbral_rhythmic_predictors.loc[
    :, timbral_rhythmic_predictors.columns.difference(predictors_with_outliers_label)].columns.values<jupyter_output><empty_output><jupyter_text>Split & Transform Data<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(
    timbral_rhythmic_predictors, y, test_size=0.2, shuffle=True)
X_train_sp = standardization_pipeline(predictors_all_label, 
                                   predictors_with_outliers_label,
                                   predictors_without_outliers_label
                                  ).fit_transform(X_train)
X_test_sp = standardization_pipeline(predictors_all_label, 
                                   predictors_with_outliers_label,
                                   predictors_without_outliers_label
                                  ).fit_transform(X_test)
X_train_sp.shape, X_test_sp.shape
X_train_np = normalization_pipeline(predictors_all_label
                                  ).fit_transform(X_train)
X_test_np = normalization_pipeline(predictors_all_label
                                  ).fit_transform(X_test)
X_train_np.shape, X_test_np.shape
nrows, ncols = X_train_sp.shape
X_train_sp = X_train_sp.reshape(nrows, ncols, 1)
X_train_np = X_train_np.reshape(nrows, ncols, 1)
nrows, ncols = X_test_sp.shape
X_test_sp = X_test_sp.reshape(nrows, ncols, 1)
X_test_np = X_test_np.reshape(nrows, ncols, 1)
# one hot encoding our labels
y_train = to_categorical(y_train, 4)
y_test = to_categorical(y_test, 4)<jupyter_output><empty_output><jupyter_text>Predictive Modelling<jupyter_code>early_stopping_cb = callbacks.EarlyStopping(patience=10,
                                        restore_best_weights=True)
fnn_layers = [ 
    layers.Flatten(input_shape=[ncols, 1]),
    # add a dense layers
    layers.Dense(128, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(64, activation='relu'),
    # add an output layer
    layers.Dense(4, activation='softmax')
]
# wrap the function with keras wrapper
fnn_1 = KerasClassifier(build_fn=get_layers('fnn', fnn_layers))
# fit model
history_1 = fnn_1.fit(X_train_sp, y_train, batch_size=32,
                    validation_data=(X_test_sp, y_test),
                    epochs=100, callbacks=[early_stopping_cb])
# wrap the function with keras wrapper
fnn_2 = KerasClassifier(build_fn=get_layers('fnn', fnn_layers))
# fit model
history_2 = fnn_2.fit(X_train_np, y_train, batch_size=32,
                    validation_data=(X_test_np, y_test),
                    epochs=100, callbacks=[early_stopping_cb])
cnn_layers = [
    layers.Conv1D(
        filters=16, kernel_size=(3),
        activation='relu', input_shape=[ncols, 1]),
    layers.MaxPooling1D(),
    layers.Conv1D(
        filters=32, kernel_size=(3), activation='relu'),
    layers.MaxPooling1D(),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.25),
    layers.Dense(4, activation='softmax')
]
# wrap the function with keras wrapper
cnn_1 = KerasClassifier(build_fn=get_layers('cnn', cnn_layers))
# fit model
history_3 = cnn_1.fit(X_train_sp, y_train, batch_size=32,
                    validation_data=(X_test_sp, y_test),
                    epochs=100, callbacks=[early_stopping_cb])
# wrap the function with keras wrapper
cnn_2 = KerasClassifier(build_fn=get_layers('cnn', cnn_layers))
# fit model
history_4 = cnn_2.fit(X_train_np, y_train, batch_size=32,
                    validation_data=(X_test_np, y_test),
                    epochs=100, callbacks=[early_stopping_cb])<jupyter_output>Model: "cnn"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, 107, 16)           64        
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 53, 16)            0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 51, 32)            1568      
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 25, 32)            0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 800)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 64)                51264     
_______________________________________________________________[...]
{'fraction_non_alphanumeric': 0.09968949174701748, 'fraction_numerical': 0.020428174538323254, 'mean_word_length': 3.6433990895295905, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '452302', 'license': 'MIT', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1882, 'n_words': 302, 'path': 'feature_extraction_deep_learning/notebooks/rough/data_prep_predictive_modelling_delete.ipynb', 'repo_name': 'DKMDebugin/amgc', 'size': '6119'}