[973ab6]: / Stats / __pycache__ / TrainingMethod.cpython-35.pyc

Download this file

90 lines (90 with data), 11.4 kB



0÷YČ3Ń@szdZddlmZmZmZmZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlZddl Z ddl!Z!edâZ"edâZ#edâZ$dZ%dZ&dgZ'dZ(dZ)dZ*dZ+dZ,GddädâZ-dS)z"It applies the training functions
Ú)┌Dict┌List┌TypeVar┌Any)┌_LogisticRegression)┌_LogisticRegressionCV)┌_MixedLinearModel)┌_RandomForestClassifier)┌_GradientBoostingClassifier)┌_DecisionTreeClassifier)┌_KNeighborsClassifier)┌_NaiveBayes)┌_NeuralNetwork)┌ReadersWriters)┌	CONSTANTS)┌cross_val_scoreN┌	DataFrame┌ndarray┌OrderedDictzMohsen Mesgarpourz-Copyright 2016, https://github.com/mesgarpour┌GPLz1.1zmohsen.mesgarpour@gmail.com┌Releasec@sÉeZdZddeeedddÉäZddddeeeeedddÉäZ	e
eeedd	d
ÉäZeddd
ÉäZedddÉäZ
e
ee
dddÉäZeeedddÉäZejdddâeeeedddÉäZdde
eeeeddd ÉäZedd!d"ÉäZeed#d$d%ÉäZeed#d&d'ÉäZeed#d(d)ÉäZdS)*┌TrainingMethodN)┌method_name┌path┌titlecCsÜtjtjâ|_|jjtâtâ|_d|_	||_
d|_d|_t
â|_d|_|dk	rć|j|ân|j||âdS)a│Initialise the objects and constants.
        :param method_name: the training method that will be used
        (options: {'lr': Logistic Regression, 'lr_cv': Logistic Regression with Cross-Validation,
        'mlm': Mixed Linear Model, 'rfc': Random Forest Classifier, 'gbc': Gradient Boosting Classifier,
        'dtc' Decision Tree Classifier, 'knc': K-Nearest Neighbors Classifier, 'nb': Multinomial Naive Bayes,
        'nn': Multi-Layer Perceptron (MLP) Neural Network}).
        :param path: the directory path of the saved trained model file, using this application (if applicable).
        :param title: the file name of the saved trained model file, using this application
        N)┌logging┌	getLoggerr┌app_name┌_TrainingMethod__logger┌debug┌__name__r┌ _TrainingMethod__readers_writers┌_TrainingMethod__methodr┌model_labels┌model_train┌dict┌
model_predict┌model_cross_validate┌_TrainingMethod__init__method┌load)┌selfrrręr+˙LC:\Users\eagle\Documents\GitHub\Analytics_UoW\TCARER\Stats\TrainingMethod.py┌__init__5s
					zTrainingMethod.__init__)rr#r$r&r'cCsh|jjdâ|dkr+tâ|_n|dkrFtâ|_nň|dkratâ|_n╩|dkr|tâ|_n»|dkrŚtâ|_nö|dkr▓tâ|_ny|dkr═t	â|_n^|d	krŔt
â|_nC|d
krtâ|_n(|jjt
dt|ââtjâ||_||_|dkrRtân||_||_dS)
a!Initialise the selected training method.
        :param method_name: the training method that will be used
        (options: {'lr': Logistic Regression, 'lr_cv': Logistic Regression with Cross-Validation,
        'mlm': Mixed Linear Model, 'rfc': Random Forest Classifier, 'gbc': Gradient Boosting Classifier,
        'dtc' Decision Tree Classifier, 'knc': K-Nearest Neighbors Classifier, 'nb': Multinomial Naive Bayes,
        'nn': Multi-Layer Perceptron (MLP) Neural Network}).
        :param model_labels: the features names to be inputted into the model.
        Note: the order of features will be preserved internally.
        :param model_train: the training model.
        :param model_predict: the prediction outputs.
        :param model_cross_validate: the cross-validation model.
        zInitialise the training method.┌lrZlr_cvZmlm┌rfcZgbc┌dtcZknc┌nb┌nnz - Invalid training method: N)rrrr"rrr	r
rrr
r┌errorr ┌str┌sys┌exitr#r$r%r&r')r*rr#r$r&r'r+r+r,Z__init__methodQs2
		zTrainingMethod.__init__method)┌features_indep_df┌feature_target┌kwargs┌returncKsT|jjdât|jjâ|_|jj||j||j|Ź|_|jS)a@Perform the training, using the selected method.
        :param features_indep_df: the independent features, which are inputted into the model.
        :param feature_target: the target feature, which is being estimated.
        :param kwargs: the training method's argument.
        :return: the trained model.
        zTrain.)	rr┌list┌columns┌valuesr#r"┌trainr$)r*r7r8r9r+r+r,r>s

	zTrainingMethod.train)r:cCs2|jjdâ|jj|j|jddgâS)zAPlot the tree diagram.
        :return: the model graph.
        zPlot.┌True┌False)rrr"┌plotr$r#)r*r+r+r,rAĆszTrainingMethod.plotcCs#|jjdâ|jj|jâS)zN Produce the training summary.
        :return: the training summary.
        zSummarise training model.)rrr"┌train_summariesr$)r*r+r+r,rBľszTrainingMethod.train_summaries)r7┌sample_namer:cCsA|jjdâ|jj|j||jâ|j|<|j|S)a=Predict probability of labels, using the training model.
        :param features_indep_df: the independent features, which are inputted into the model.
        :param sample_name: the sample to predict(e.g. 'train', 'test', 'validate').
        :return: the predicted probabilities, and the predicted labels.
        zPredict.)rrr"┌predictr$r#r&)r*r7rCr+r+r,rDŁs&zTrainingMethod.predict)r8rCr:cCs;|jjdâ||j|d<|jj|j||âS)aroduce summary statistics for the prediction performance.
        :param feature_target: the target feature, which is being estimated.
        :param sample_name: the sample to predict(e.g. 'train', 'test', 'validate').
        :return: the prediction summaries.
        zSummarise predictions.┌target)rrr&r"┌predict_summaries)r*r8rCr+r+r,rFęsz TrainingMethod.predict_summariesrg═╠╠╠╠╠­?gÜÖÖÖÖÖę?)r8rC┌cutoffsr:cCsB|jjdâ||j|d<|jj|j|d||âS)aiProduce a summary statistics table for a range of cut-off points.
        :param feature_target: the target feature, which is being estimated.
        :param sample_name: the sample to predict(e.g. 'train', 'test', 'validate').
        :param cutoffs: a list of risk cut-off points.
        :return: the summary statistics table for the cut-off points.
        zSummarise predictions.rE┌score)rrr&r"Zpredict_summaries_cutoffs_table)r*r8rCrGr+r+r,┌predict_summaries_risk_bandsÁs
	z+TrainingMethod.predict_summaries_risk_bands┌neg_mean_squared_errorÚ
)r7r8┌scoring┌cvr:cCsB|jjdât|j||j|d|d|â|_|jS)aČEvaluate the model by performing cross-validation.
        :param features_indep_df: the independent features, which are inputted into the model.
        :param feature_target: the target feature, which is being estimated.
        :param scoring: the scoring method (default: 'neg_mean_squared_error').
        :param cv: the cross-validation splitting strategy (optional).
        :return: the cross-validation summary
        zCross-ValidaterLrM)r┌inforr$r#r')r*r7r8rLrMr+r+r,┌cross_validate─s(zTrainingMethod.cross_validatecCs|jS)zhProduce a summary of the applied cross-validation
        :return: the cross-validation summary
        )r')r*r+r+r,┌cross_validate_summariesÍsz'TrainingMethod.cross_validate_summaries)rrcCsw|jjdâtâ}|j|d<|j|d<|j|d<|j|d<|j|d<|jj	||d|âdS)	aZSave (pickle) the training model, as well as predictions and cross-validations.
        Note: summaries statistics won't not saved.
        :param path: the directory path of the saved trained model file, using this application (if applicable).
        :param title: the file name of the saved trained model file, using this application.
        zSaving modelrr#r$r&r'┌objectsN)
rrNr%rr#r$r&r'r!┌save_serialised)r*rrrQr+r+r,┌
save_model▄s	




zTrainingMethod.save_modelcCsw|jjdâtâ}|j|d<|j|d<|j|d<|j|d<|j|d<|jj	||d|âdS)	adSave (pickle) & compressthe training model, as well as predictions and cross-validations.
        Note: summaries statistics won't not saved.
        :param path: the directory path of the saved trained model file, using this application (if applicable).
        :param title: the file name of the saved trained model file, using this application.
        zSave model.rr#r$r&r'rQN)
rrr%rr#r$r&r'r!┌save_serialised_compressed)r*rrrQr+r+r,┌save_model_compressedÝs	




z$TrainingMethod.save_model_compressedcCsá|jjdâ|jj||â}y@|jd|dd|dd|dd|dd|dâWn4fk
rŤ|jjtd|âtjâYnXdS)	aLoad (unpickle) the model, which was saved using this application.
        :param path: the directory path of the saved trained model file, using this application (if applicable).
        :param title: the file name of the saved trained model file, using this application
        zLoad model.rr#r$r&r'z' - Invalid field(s) in the model file: N)	rrr!┌load_serialisedr(r3r r5r6)r*rrrQr+r+r,r)■s



zTrainingMethod.load)r ┌
__module__┌__qualname__r4r-rrr┌NumpyNDArrayr(┌PandasDataFramer>rArBrD┌CollectionsOrderedDictrF┌np┌arangerI┌intrOrPrSrUr)r+r+r+r,r4s2$*

0$r).┌__doc__┌typingrrrrZStats._LogisticRegressionrZStats._LogisticRegressionCVrZStats._MixedLinearModelrZStats._RandomForestClassifierr	Z!Stats._GradientBoostingClassifierr
ZStats._DecisionTreeClassifierrZStats._KNeighborsClassifierrZStats._NaiveBayesr
ZStats._NeuralNetworkr┌ReadersWriters.ReadersWritersr┌Configs.CONSTANTSrZsklearn.model_selectionr┌numpyr\r5rrZrYr[┌
__author__┌
__copyright__┌__credits__┌__license__┌__version__┌__maintainer__┌	__email__┌
__status__rr+r+r+r,┌<module>s8"