liveBook · Manning

Gini impurity

As explained in Build AI Drug Discovery Pipelines

Gini impurity measures the expected misclassification rate when class labels are randomly assigned according to their distribution within a node.

Gini impurity measures how often a randomly labeled sample would be misclassified within a decision-tree node. A value of 0 means every molecule belongs to one class; higher values indicate mixed labels. section 5.2.1

Use the node’s class distribution to interpret the metric:

  • Binary classification reaches its maximum, 0.5, when the node is split evenly between classes. section 5.2.1
  • Decision trees favor splits that reduce impurity, weighting each child by the number of samples it receives. section 5.2.1
  • Inspect samples, class counts, and predicted class alongside Gini to understand each node’s decision. section 5.2.1

Cytochrome P450 inhibition

A decision tree trained on molecular weight and logP can split molecules into inhibitor and noninhibitor groups. Its root contains 5,151 noninhibitors and 3,478 inhibitors, so the node is impure; later splits may create purer groups. section 5.2.1

Listing 5.3 Train Decision Tree Classifier
from rdkit.Chem import Descriptors
from sklearn.tree import DecisionTreeClassifier, export_graphviz, plot_tree
 
RDKIT_DESCRIPTORS = {
  'MolWt' : Descriptors.MolWt,
  'logP' : Descriptors.MolLogP,
}
 
for desc, func in RDKIT_DESCRIPTORS.items():
  train_df[desc] = train_df["Molecule"].apply(func)
  val_df[desc] = val_df["Molecule"].apply(func)
 
tree_clf = DecisionTreeClassifier(max_depth=2)
tree_clf.fit(train_df[['MolWt', 'logP']], train_df.Inhibitor)
val_df['Predicted Class'] = tree_clf.predict(val_df[['MolWt', 'logP']])
val_df['Probability Estimate'] = tree_clf.predict_proba(
  val_df[['MolWt', 'logP']]
)[:,1] 

Run the compact Python workflow to calculate RDKit descriptors, train a depth-two classifier, and obtain predictions and probability estimates. section 5.2.1

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Defines node impurity, class-dependent bounds, and weighted split evaluation, but stops short of broader tree optimization.
Examples
Uses molecular weight, logP, CYP inhibition labels, node counts, and a depth-two tree visualization.
What it assumes
Assumes basic binary classification and class labels while introducing impurity through node-level examples.
What you can do after
Apply it via scikit-learn to interpret tree nodes and compare candidate feature splits.
How it connects
Connects decision trees with classification, class distributions, molecular descriptors, and probability estimates.

Also explained in

Compare how 9 books explain Gini impurity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.