<commit_before>from sklearn.model_selection import StratifiedKFold
from sklearn import tree
from sklearn.ensemble import ExtraTreesClassifier, GradientBoostingClassifier, RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.metrics import confusion_matrix
from tools import ConfusionMatrixUtils
import pandas as pd
import pydotplus
import numpy as np
import matplotlib.pyplot as plt
import itertools
from featuresExtraction import extractFeatures
import joblib

# Returns the best classifiers for faster experiments
def bestClfs():

    DTC23 = tree.DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=15,
            min_samples_split=61, min_weight_fraction_leaf=0,
            presort=False, random_state=None, splitter='best')
    #   ----> Accuracy: 0.553043478261 +- 0.0141287624428
    RFC23 = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features=0.497907908371,
            max_leaf_nodes=None, min_impurity_split=1e-07,
            min_samples_leaf=2, min_samples_split=2,
            min_weight_fraction_leaf=0, n_estimators=150, n_jobs=4,
            oob_score=True, random_state=None, verbose=0, warm_start=False)

    DTC7 = tree.DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=9,
            min_samples_split=40, min_weight_fraction_leaf=0,
            presort=False, random_state=None, splitter='best')
    #   ----> Accuracy: 0.553043478261 +- 0.0141287624428
    RFC7 = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features=0.59,
            max_leaf_nodes=None, min_impurity_split=1e-07,
            min_samples_leaf=2, min_samples_split=15,
            min_weight_fraction_leaf=0, n_estimators=84, n_jobs=4,
            oob_score=True, random_state=None, verbose=0, warm_start=False)
    #1 0.548 +-0.015 with beatsdataset.csv (windows and steps 1 1 0.05 0.05) SIN ESSENTIA BPM 0.47
    #2 0.492 +- 0.015 with beatsdataset1-1-01-005.csv
    #3 0.486 +- 0.015 with beatsdataset1-1-01-01.csv
    #4 0.424 +- 0.023 with beatsdataset1-1-04-04.csv
    #5 0.4383 +- 0.0103 with beatsdataset1-05-005-0025.csv
    #6 0.463 +- 0.032 with beatsdataset138-stStep25.csv
    #7 0.493 +- 0.011 with beatsdataset138-stStep50.csv  CON ESSENTIA BPM 0.56 +- 0.007
    #10 0.694 +- 0.044 with gtzan.csv


    ETC = ExtraTreesClassifier(bootstrap=True, criterion="gini",
                               max_features=1, min_samples_leaf=2,
                               min_samples_split=10, n_estimators=100)

    # Accuracy 138 step 50 with BPM essentia (0.56260869565217386, 0.012251306785743798)
    ETC = ExtraTreesClassifier(bootstrap=False, criterion="gini",
                               max_features=0.5, min_samples_leaf=2,
                               min_samples_split=10, n_estimators=100)

    # Best with GTZAN
    #1 0.534 +- 0.01 with beatsdataset.csv
    #2 0.46 +- 0.01 with beatsdataset1-1-01-005.csv
    #3 0.48 +- 0.014 with beatsdataset1-1-01-01.csv
    #4 0.422 +- 0.019 with beatsdataset1-1-04-04.csv
    #5 0.4387 +- 0.0182 with beatsdataset1-05-005-0025.csv
    #6 0.452 +- 0.0198 with beatsdataset138-stStep25.csv
    #7 0.486 +- 0.024 with beatsdataset138-stStep50.csv
    #10 0.731 +- 0.021 with gtzan.csv

    GBC = GradientBoostingClassifier(learning_rate=0.1, max_depth=6,
                                     max_features=0.5, min_samples_leaf=13,
                                     min_samples_split=6, subsample=0.8)

    #1 0.556 +-0.016 with beatsdataset.csv SIN ESSENTIA BPM 0.48
    #2 0.477 +- 0.012 with beatsdataset1-1-01-005.csv
    #3 0.477 +- 0.007 with beatsdataset1-1-01-01.csv
    #4 0.451 +- 0.007 with beatsdataset1-1-04-04.csv
    #5 0.443 +- 0.019 with beatsdataset1-05-005-0025.csv
    #6 0.479 +- 0.011 with beatsdataset138-stStep25.csv
    #7 0.5 +- 0.02 with beatsdataset138-stStep50.csv CON ESSENTIA BPM 0.557, 0.017
    #10 0.722 +- 0.012 with gtzan.csv

    XGB = XGBClassifier(learning_rate=0.1, max_depth=5,
                        min_child_weight=6, nthread=4,
                        subsample=0.55)

    clfs = [DTC23, RFC23, DTC7, RFC7]
    return clfs

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap, vmax=sum(cm[0][:]))
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)<commit_msg>Modified matrix normalization and xgboost import.<commit_after>from sklearn.model_selection import StratifiedKFold
from sklearn import tree
from sklearn.ensemble import ExtraTreesClassifier, GradientBoostingClassifier, RandomForestClassifier
from sklearn.metrics import confusion_matrix
from tools import ConfusionMatrixUtils
import pandas as pd
import pydotplus
import numpy as np
import matplotlib.pyplot as plt
import itertools
from featuresExtraction import extractFeatures
import joblib
try:
    from xgboost import XGBClassifier
except ImportError:
    print("xgboost not installed!")

# Returns the best classifiers for faster experiments
def bestClfs():

    DTC23 = tree.DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=15,
            min_samples_split=61, min_weight_fraction_leaf=0,
            presort=False, random_state=None, splitter='best')
    #   ----> Accuracy: 0.553043478261 +- 0.0141287624428
    RFC23 = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features=0.497907908371,
            max_leaf_nodes=None, min_impurity_split=1e-07,
            min_samples_leaf=2, min_samples_split=2,
            min_weight_fraction_leaf=0, n_estimators=150, n_jobs=4,
            oob_score=True, random_state=None, verbose=0, warm_start=False)

    DTC7 = tree.DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=9,
            min_samples_split=40, min_weight_fraction_leaf=0,
            presort=False, random_state=None, splitter='best')
    #   ----> Accuracy: 0.553043478261 +- 0.0141287624428
    RFC7 = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features=0.59,
            max_leaf_nodes=None, min_impurity_split=1e-07,
            min_samples_leaf=2, min_samples_split=15,
            min_weight_fraction_leaf=0, n_estimators=84, n_jobs=4,
            oob_score=True, random_state=None, verbose=0, warm_start=False)
    #1 0.548 +-0.015 with beatsdataset.csv (windows and steps 1 1 0.05 0.05) SIN ESSENTIA BPM 0.47
    #2 0.492 +- 0.015 with beatsdataset1-1-01-005.csv
    #3 0.486 +- 0.015 with beatsdataset1-1-01-01.csv
    #4 0.424 +- 0.023 with beatsdataset1-1-04-04.csv
    #5 0.4383 +- 0.0103 with beatsdataset1-05-005-0025.csv
    #6 0.463 +- 0.032 with beatsdataset138-stStep25.csv
    #7 0.493 +- 0.011 with beatsdataset138-stStep50.csv  CON ESSENTIA BPM 0.56 +- 0.007
    #10 0.694 +- 0.044 with gtzan.csv


    ETC = ExtraTreesClassifier(bootstrap=True, criterion="gini",
                               max_features=1, min_samples_leaf=2,
                               min_samples_split=10, n_estimators=100)

    # Accuracy 138 step 50 with BPM essentia (0.56260869565217386, 0.012251306785743798)
    ETC = ExtraTreesClassifier(bootstrap=False, criterion="gini",
                               max_features=0.5, min_samples_leaf=2,
                               min_samples_split=10, n_estimators=100)

    # Best with GTZAN
    #1 0.534 +- 0.01 with beatsdataset.csv
    #2 0.46 +- 0.01 with beatsdataset1-1-01-005.csv
    #3 0.48 +- 0.014 with beatsdataset1-1-01-01.csv
    #4 0.422 +- 0.019 with beatsdataset1-1-04-04.csv
    #5 0.4387 +- 0.0182 with beatsdataset1-05-005-0025.csv
    #6 0.452 +- 0.0198 with beatsdataset138-stStep25.csv
    #7 0.486 +- 0.024 with beatsdataset138-stStep50.csv
    #10 0.731 +- 0.021 with gtzan.csv

    GBC = GradientBoostingClassifier(learning_rate=0.1, max_depth=6,
                                     max_features=0.5, min_samples_leaf=13,
                                     min_samples_split=6, subsample=0.8)

    #1 0.556 +-0.016 with beatsdataset.csv SIN ESSENTIA BPM 0.48
    #2 0.477 +- 0.012 with beatsdataset1-1-01-005.csv
    #3 0.477 +- 0.007 with beatsdataset1-1-01-01.csv
    #4 0.451 +- 0.007 with beatsdataset1-1-04-04.csv
    #5 0.443 +- 0.019 with beatsdataset1-05-005-0025.csv
    #6 0.479 +- 0.011 with beatsdataset138-stStep25.csv
    #7 0.5 +- 0.02 with beatsdataset138-stStep50.csv CON ESSENTIA BPM 0.557, 0.017
    #10 0.722 +- 0.012 with gtzan.csv

    XGB = XGBClassifier(learning_rate=0.1, max_depth=5,
                        min_child_weight=6, nthread=4,
                        subsample=0.55)

    clfs = [DTC23, RFC23, DTC7, RFC7]
    return clfs

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap, vmax=sum(cm[0][:]))
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = np.round(100*cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]).astype('int')
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)
{'fraction_non_alphanumeric': 0.1048990808300957, 'fraction_numerical': 0.10660475694115418, 'mean_word_length': 2.762566844919786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3982960', 'n_tokens_mistral': 4390, 'n_tokens_neox': 3631, 'n_words': 738}