Posts for: #Python

Patterns for Customizing Class Creation

__init_subclass__ was introduced in PEP 487 and according to James Powell covers every use that was previously done in metaclasses (with the one exception being implementation of protocols on types). It’s main purpose was to customize subclass creation

Just to get it out of the way, let’s see the order in which these functions are called (the other functions being __new__ and __init__)

class Parent:
    def __init__(self, *args, **kwargs) -> None:
        print('Parent __init__')

    def __new__(cls, *args, **kwargs):
        print('Parent __new__')
        return super().__new__(cls, *args, **kwargs)

    def __init_subclass__(cls):
        print('__init_subclass__')

class Child(Parent):
    def __init__(self, *args, **kwargs):
        print('Child __init__')
        super().__init__(*args, **kwargs)
__init_subclass__

We see that __init_subclass__ is run at time of child class creation, NOT instance creation

[Read more]

Experiments customizing __new__ in Python

object.__new__(cls[, ...])

__new__ is called to create a new instance of class cls. It is a static method, which takes the class of which an instances was requested as its first argument. Remaining are arguments passed into the constructor. The return value should be a new object instance (if this is not returned, the instance is not created)

Typically call super().__new(cls[, ...]).

__init__ vs __new__

According to the python docs, __new__ was for customizing instance creation when subclassing built-int types. Since it’s invoked before __init__, it is called with the CLASS as it’s first argument (whereas __init__ is called with an instance as its first and doesn’t return anything)

[Read more]

XGBoost, Imbalanced Classification and Hyperopt

This is a tutorial/explanation of how to set up XGBoost for imbalanced classification while tuning for imbalanced data.

There are three main sections:

  1. Hyperopt/Bayesian Hyperparameter Tuning
  2. Focal and Crossentropy losses
  3. XGBoost Parameter Meanings

(references are dropped as-needed)

Hyperopt

The hyperopt package is associated with Bergstra et. al.. The authors argued that the performance of a given model depends both on the fundamental quality of the algorithm as well as details of its tuning (also known as its hyper-parameters).

[Read more]