Package: skflex
skflexFLEXIBLE FUNCTIONS-----FAST PROCESSING AND EVALUATIONskflex provides a suite of utility functions for use with the sklearn library. The module primarily focuses on producing typical plots and metrics for evaluating machine learning models. It has been designed with flexability and customisation in mind to speed up workflows, and enhance comparative evaluation. Refer to theGithubrepository for example outputs and images.Installation and Importpip install skflex

import skflex.skflex as skfROC_AUC Curveroc_auc_plot(models, X_test = None, y_test = None, width = 14, height = 12, legend_size = 14, title='ROC Curve')Accepts fitted model(s) and test data. It will then:Calculate ROCCalculate AUCPlot ROC curve with AUC provided in the legendParameters:models - fitted model objects. NOTE: Only models with a 'predict_proba' or 'decision_function' method are supported.X_test - test feature sety_test - test labels settitle - title for ROC curve plotwidth - plot widthheight - plot heightlegend_size - size of plot legendExample:from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression

model_1 = GaussianNB()
model_2 = LogisticRegression()

model_1.fit(X_train, y_train)
model_2.fit(X_train, y_train)

skf.roc_auc_plot(model_1, model_2, X_test = X_test, y_test = y_test, 
                title = 'Example ROC plot')Classification Reportsclassifier_train_report(models, X_train = None, y_train = None, X_test = None, y_test = None, scoring = 'accuracy', title = 'Reports')Accepts classifier models, training data, and test data. It will then:Fit the model(s) to training dataMake predictions using test dataProduce classification report for comparisonProduce confusion matrix for comparisonProvide an ordered summary (ranked best to worst score) using given evaluation metricParameters:models - model objects to be trained and evaluatedX_train - training feature sety_train - training label setX_test - test feature sety_test - test label setscoring - summary evaluation metric. Common classifier evaluation metrics including accuracy, f1, precision, and recall are supported. Refer tosklean scoring documentationfor more information. Scoring methodologies should be passed as strings, for example, precision would be passed asscoring = 'precision'title - title for output reportExample:from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression

model_1 = GaussianNB()
model_2 = LogisticRegression()

skf.classifier_train_report(model_1, model_2, X_train = X_train, y_train = y_train, 
                            X_test = X_test, 
                            y_test = y_test, 
                            scoring = 'accuracy', 
                            title = 'Example Report')Validation Curvevalidation_plot(model = None, param = None, param_grid = None, X_train = None, y_train = None, cv = 5, scoring = 'accuracy', width = 9, height = 9, title = 'Validation Curve')Accepts a model, a related hyper-parameter, a list of hyper-parameter values, training and test data, number of cross-validation folds, scoring methodology, as well as a plot title.
It will produce a plot of the validation curve for the training and test data using the mean scores and standard deviations obtained through the cross-validation process.Parameters:model - model objectparam - hyperparameter to be used to produce the validation curveparam_grid - hyperparameter values to be testedX_train - training feature sety_train - training label setcv - number of cross-validation foldsscoring - scoring methodology used during cross-validation processtitle - title for validation plotwidth - plot widthheight - plot heightExample:from sklearn.tree import DecisionTreeClassifier

model_1 = DecisionTreeClassifier()
params = [5, 10, 15, 20, 30, 40, 50]

skf.validation_plot(model = model_1, param = 'max_depth', param_grid = params, 
                    X_train = X_train, 
                    y_train = y_train, 
                    title = 'Example Validation Curve')Train, Validation and Test Splittrain_val_test(data = None, class_labels = None, train = 0.6, val = 0.2, shuffle = True, random_state = None)Accepts a Pandas dataframe and will return a training, validation, and test set. Operates in a similar fashion to the sklearn train_test_split function by defining a percentage split for the training and validation sets (example 0.6 = 60%). The remainder is allocated to the test set.Parameters:data - dataframe to be split into a training, validation, and test setclass_labels - column in the dataframe containing class labelstrain - percentage of data to be allocated to the training setval - percentage of data to be allocated to the validation setshuffle - if true, will shuffle the rows in the dataframe before splittingrandom_state - if shuffle is ture, will set a random seed so that ordering produced by shuffle can be reproducedReturns: X_train, y_train, X_val, y_val, X_test, y_testExample:X_train, y_train, X_val, y_val, X_test, y_test = skf.train_val_test(data = my_data, 
                                                                    class_labels = 'labels', 
                                                                    train = 0.6, 
                                                                    val = 0.2)PCA Scree Plotpca_scree_plot(data = None, n_components = None, width = 16, height = 10, legend_size = 12, scale_data = False, title = 'PCA Scree Plot')Accepts data (array/dataframe), and number of principal components to be analysed. It will produce a scree plot of the cumulative variance explained.Parameters:data - dataset to be analysedn_components - number of principal components to be analysedwidth - width of plotheight - height of plotlegend_size - size of plot legendscale_data - normalises data before analysis and plotting. If the data being passed has not yet been normalised, this parameter should be set asscale_data = Truetitle - plot titleExample:from sklearn.preprocessing import scale

n_data = scale(my_data)

skf.pca_scree_plot(data = n_data, n_components = 70, title = 'Example PCA Scree Plot')RequirementsSklearn >= 0.24.1Matplotlib >= 3.3.4Pandas >= 1.2.4Numpy >= 1.20.1