<jupyter_start><jupyter_text>Train and Test with best params combination  Data Preparation<jupyter_code>import os
import sys

from sklearn.model_selection import train_test_split
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 *
tf.__version__
import sklearn; sklearn.__version__
# load dataset
features = pandas.read_csv(MOUNTED_DATASET_PATH + '/data/cleaned_features_1.csv', index_col=0)
features
# shuffle dataset
features = features.reindex(numpy.random.permutation(features.index))
features
# split dataset into target (y) and predictors (x)
y = features.loc[:,'genre_label']
X = features.loc[:, features.columns.difference(['genre_label'])]
(timbral_rhythmic_predictors, predictors_with_pos_corr,
 wavelet_predictors) = break_into_subsets(features)
norm_pipelines = create_norm_pipelines(features)
list_ncols = [360, 108, 167, 252]
eval_metrics = {
    'all': [], 'tr': [], 'pos_corr': [],
    'wavelet': []
}<jupyter_output><empty_output><jupyter_text>Predictive Modelling (All Features)<jupyter_code># split all features into sets: training 80% & testing 20% of total dataset
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, shuffle=True)
# one hot encoding our labels
y_train = to_categorical(y_train, 3)
y_test_encoded = to_categorical(y_test, 3)
# best params for all features
params = {
    'units': 2, 'optimizer': 'adam', 'n_hidden': 2, 'lr': 0.0001,
    'kernel_initializer': 'glorot_uniform', 'filters': 32,
    'dropout': 0.15, 'activation': 'selu'
}
# learn model
model = training_best_model(X_train, y_train,
                                'model_all', list_ncols[0],
                                set_shape_create_cnn_model, 
                                norm_pipelines['all'], params, 
                                batch_size=25, epochs=200)
# plot confusion metrics as heat map
visualize_conf_metrics(X_test, y_test_encoded, model)
# compute evaluation metrics & add to dict
(accuracy, precision, recall, f1_score) = evaluate_model(X_test, y_test, model)
eval_metrics['all'] = [accuracy, precision, recall, f1_score]
accuracy, precision, recall, f1_score<jupyter_output><empty_output><jupyter_text>accuracy,precision,recall,f1_score,batch,epooch.738,.732,.742,.737,32,161.744,.733,,746,.732,28,157.755,.748,.751,.749,25,119 0.7440119760479041,0.7688442211055276,0.7050691244239631,0.735576923076923,23,910.7380239520958084,0.7339449541284404,0.7373271889400922,0.7356321839080461,22,1450.75,0.7324561403508771,0.7695852534562212,0.750561797752809,20,1100.7455089820359282,0.7568807339449541,0.7534246575342466,0.7551487414187642,15,92  Predictive Modelling (Timbral & Rhythmic Features)<jupyter_code># split timbral & rhythmic features into sets: training 80% & testing 20% of total dataset
X_train, X_test, y_train, y_test = train_test_split(
    timbral_rhythmic_predictors, y, test_size=0.2, shuffle=True)
# one hot encoding our labels
y_train = to_categorical(y_train, 3)
y_test_encoded = to_categorical(y_test, 3)
# best params for timbral & rhythmic features
params = {
    'units': 90, 'optimizer': 'adam','n_hidden': 1, 'lr': 0.0001,
    'kernel_initializer': 'glorot_uniform', 'filters': 32,
    'dropout': 0.25, 'activation': 'selu'
}
# learn model
model = training_best_model(X_train, y_train,
                                'model_tr', list_ncols[1],
                                set_shape_create_cnn_model, 
                                norm_pipelines['tr'], params, 
                                batch_size=15, epochs=200)
# plot confusion metrics as heat map
visualize_conf_metrics(X_test, y_test_encoded, model)
# compute evaluation metrics & add to dict
(accuracy, precision, recall, f1_score) = evaluate_model(X_test, y_test, model)
eval_metrics['tr'] = [accuracy, precision, recall, f1_score]
accuracy, precision, recall, f1_score<jupyter_output><empty_output><jupyter_text>accuracy,precision,recall,f1_score,batch,epooch0.75,0.7452830188679245,0.7596153846153846,0.7523809523809523,32,960.7470059880239521,0.7476190476190476,0.7548076923076923,0.7511961722488039,30,960.7485029940119761, 0.75, 0.7355769230769231, 0.7427184466019418,28,850.7335329341317365,0.7276995305164319,0.7451923076923077,0.7363420427553445,25,570.7470059880239521,0.7476190476190476,0.7548076923076923,0.7511961722488039,18,630.7529940119760479,0.7511961722488039,0.7548076923076923,0.7529976019184652,15,64 0.7529940119760479,0.7524271844660194,0.7451923076923077,0.7487922705314008,13,600.7455089820359282, 0.75, 0.7355769230769231, 0.7427184466019418,10,52  Predictive Modelling (Features with Positive Correlation)<jupyter_code># split features with pos corr into sets: training 80% & testing 20% of total dataset
X_train, X_test, y_train, y_test = train_test_split(
    predictors_with_pos_corr, y, test_size=0.2, shuffle=True)
# one hot encoding our labels
y_train = to_categorical(y_train, 3)
y_test_encoded = to_categorical(y_test, 3)
# best params for features with pos corr
params = {'units': 71, 'optimizer': 'rmsprop', 'n_hidden': 2, 'lr': 0.0003,
          'kernel_initializer': 'glorot_uniform', 'filters': 64, 
          'dropout': 0.1, 'activation': 'elu'}
# learn model
model = training_best_model(X_train, y_train,
                                'model_pos_corr', list_ncols[2],
                                set_shape_create_cnn_model, 
                                norm_pipelines['pos_corr'], params, 
                                batch_size=20, epochs=200)
# plot confusion metrics as heat map
visualize_conf_metrics(X_test, y_test_encoded, model)
# compute evaluation metrics & add to dict
(accuracy, precision, recall, f1_score) = evaluate_model(X_test, y_test, model)
eval_metrics['pos_corr'] = [accuracy, precision, recall, f1_score]
accuracy, precision, recall, f1_score<jupyter_output><empty_output><jupyter_text>accuracy,precision,recall,f1_score,batch,epooch0.718562874251497, 0.688, 0.7853881278538812, 0.7334754797441364,32,390.6796407185628742,0.6763485477178424,0.7442922374429224,0.7086956521739131,30,190.7005988023952096,0.6952789699570815,0.7397260273972602,0.7168141592920354,28,250.7035928143712575,0.6960352422907489,0.7214611872146118,0.7085201793721972,25,250.7095808383233533,0.7298578199052133,0.7031963470319634,0.7162790697674418,20,25 0.7050898203592815,0.7040358744394619,0.7168949771689498,0.7104072398190046,18,250.7080838323353293, 0.688034188034188, 0.7351598173515982, 0.7108167770419426,15,36  Predictive Modelling (Wavelet Features)<jupyter_code># split wavelet features into sets: training 80% & testing 20% of total dataset
X_train, X_test, y_train, y_test = train_test_split(
    wavelet_predictors, y, test_size=0.2, shuffle=True)
# one hot encoding our labels
y_train = to_categorical(y_train, 3)
y_test_encoded = to_categorical(y_test, 3)
# best params for wavelet features
params = {
    'units': 90, 'optimizer': 'adam', 'n_hidden': 1, 'lr': 0.0001,
    'kernel_initializer': 'glorot_uniform', 'filters': 32,
    'dropout': 0.25, 'activation': 'selu'
}
# learn model
model = training_best_model(X_train, y_train,
                                'model_wav', list_ncols[3],
                                set_shape_create_cnn_model, 
                                norm_pipelines['wavelet'], params, 
                                batch_size=25, epochs=200)
# plot confusion metrics as heat map
visualize_conf_metrics(X_test, y_test_encoded, model)
# compute evaluation metrics & add to dict
(accuracy, precision, recall, f1_score) = evaluate_model(X_test, y_test, model)
eval_metrics['wavelet'] = [accuracy, precision, recall, f1_score]
accuracy, precision, recall, f1_score<jupyter_output><empty_output><jupyter_text>accuracy,precision,recall,f1_score,batch,epooch0.6916167664670658,0.7230046948356808,0.7031963470319634,0.7129629629629628,32,530.6916167664670658,0.7342995169082126,0.6940639269406392,0.7136150234741784,28,530.6946107784431138,0.7255813953488373,0.7123287671232876,0.7188940092165899,25,53 0.688622754491018, 0.7110091743119266, 0.7077625570776256, 0.7093821510297483,20,530.6946107784431138,0.7333333333333333,0.7031963470319634,0.7179487179487178,18,530.6901197604790419,0.7373737373737373,0.6666666666666666,0.7002398081534772,15,530.688622754491018, 0.6995515695067265, 0.7123287671232876, 0.7058823529411764,12,33  Plot Evaluation Metrics<jupyter_code>index = ['accuracy', 'precision', 'recall', 'f1-score']
df_metrics = pandas.DataFrame(
    data={
        'All Features': eval_metrics['all'], 
        'Timbral & Rhythmic Features': eval_metrics['tr'], 
        'Positive Correlated Features': eval_metrics['pos_corr'],
        'Wavelet Features': eval_metrics['wavelet']
    }, 
    index=index
)
df_metrics.plot.bar(rot=45, figsize=(15,7))
pyplot.legend(loc="lower right", fontsize=15)
pyplot.show()<jupyter_output><empty_output><jupyter_text>Train Final Model<jupyter_code>X = timbral_rhythmic_predictors
y = to_categorical(y, 3)
# best params for timbral & rhythmic features
params = {
    'units': 90, 'optimizer': 'adam','n_hidden': 1, 'lr': 0.0001,
    'kernel_initializer': 'glorot_uniform', 'filters': 32,
    'dropout': 0.25, 'activation': 'selu'
}
# learn model
model = training_best_model(X, y,'model_final', list_ncols[1],
                            set_shape_create_cnn_model, 
                            norm_pipelines['tr'], params, 
                            batch_size=15, epochs=200)
norm_pipe = model.named_steps['preprocess']
clf = model.named_steps['clf']
# save pipeline estimator to disk
norm_pipe_path = MOUNTED_DATASET_PATH + '/model/pipeline_estimator_3.pkl'
joblib.dump(norm_pipe, open(norm_pipe_path, 'wb'))
#  save keras model to disk
model_path = MOUNTED_DATASET_PATH + '/model/cnn_model_3.h5'
clf.model.save(model_path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11499697641604516, 'fraction_numerical': 0.20348720016125782, 'mean_word_length': 5.08399754751686, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '229673', 'license': 'MIT', 'n_tokens_mistral': 4898, 'n_tokens_neox': 3661, 'n_words': 602, 'path': 'feature_extraction_deep_learning/notebooks/10_data_prep_predictive_modelling.ipynb', 'repo_name': 'DKMDebugin/amgc', 'size': '9922'}