liveBook · Manning

Multi-output classification

As explained in Build AI Drug Discovery Pipelines

Multioutput classification predicts multiple output variables, whose values may have different class structures or data types.

Multi-output classification predicts several output variables for each input. Outputs may use different class structures or data types, and the framework includes multiclass and multilabel tasks. section 5.4.2

Choose the formulation according to how labels can occur:

  • Use multiclass classification when each instance has exactly one class. section 5.4.2
  • Use multilabel classification when one instance may receive several labels simultaneously. section 5.4.2
  • Apply logistic functions independently to multilabel outputs; use MultiOutputClassifier when independent estimators are appropriate. section 5.4.2
  • Prefer a single model such as Random Forest when labels may be correlated; independent models are reasonable when they are not. section 5.4.2

CYP inhibition

Represent each molecule with five CYP inhibition labels—CYP2C19, CYP2D6, CYP3A4, CYP1A2, and CYP2C9—and ask which enzymes it inhibits, if any. section 5.4.2

Listing 5.10 Create Multilabel CYP Inhibitor Data and Train and Evaluate Models
from rdkit.Chem import MolToInchi, MolFromInchi
from skmultilearn.model_selection import IterativeStratification
from sklearn.multioutput import MultiOutputClassifier
from sklearn.metrics import coverage_error, label_ranking_loss
 
def process_cyp_data(cyp):
  df = ADME(name = f'{cyp}_Veith').get_data()
  df = df.rename(columns={'Y':f'{cyp}'})
  PandasTools.AddMoleculeColumnToFrame(df, "Drug", "Molecule")
  df['Inchi'] = df.Molecule.apply(MolToInchi)
  df = df.dropna()
  return df
 
cyps = ['CYP2C19', 'CYP2D6', 'CYP3A4', 'CYP1A2', 'CYP2C9']
cyp_inhibitor_dfs = [process_cyp_data(cyp) for cyp in cyps]
cyp_inhibitor_df = cyp_inhibitor_dfs[0]
for df in cyp_inhibitor_dfs[1:]:
  cyp_inhibitor_df = cyp_inhibitor_df.merge(df, on='Inchi', how='inner')
 
cyp_inhibitor_df = cyp_inhibitor_df[
    ['Molecule', 'CYP2C19', 'CYP2D6', 'CYP3A4', 'CYP1A2', 'CYP2C9']
]
cyp_inhibitor_df['fingerprints'] = cyp_inhibitor_df['Molecule'].apply(
    compute_fingerprint, r=2, nBits=2048
)
 
X = cyp_inhibitor_df["fingerprints"]
y = cyp_inhibitor_df[cyps]
 
stratifier = IterativeStratification(
    n_splits=2, order=2, sample_distribution_per_fold=[0.2, 0.8]
)
train_indexes, test_indexes = next(stratifier.split(X, y))
X_train, y_train = X.loc[train_indexes], y.loc[train_indexes]
X_test, y_test = X.loc[test_indexes], y.loc[test_indexes]
 
lr_clf = MultiOutputClassifier(LogisticRegression()).fit(
    np.array(X_train.values.tolist()), y_train
)
lr_y_pred = lr_clf.predict(np.array(X_test.values.tolist()))
lr_y_pred_proba = lr_clf.predict_proba(np.array(X_test.values.tolist()))
 
rf_clf = RandomForestClassifier(n_estimators=500, n_jobs=-1).fit(
    np.array(X_train.values.tolist()), y_train
)
rf_y_pred = rf_clf.predict(np.array(X_test.values.tolist()))
rf_y_pred_proba = rf_clf.predict_proba(np.array(X_test.values.tolist()))
 
print(f"LR Coverage Error: {coverage_error(y_test, lr_y_pred):.3f}")
# Output -> LR Coverage Error: 2.863
print(f"RF Coverage Error: {coverage_error(y_test, rf_y_pred):.3f}")
# Output -> RF Coverage Error: 2.862
print(f"LR Ranking Loss: {label_ranking_loss(y_test, lr_y_pred):.3f}")
# Output -> LR Ranking Loss: 0.481
print(f"RF Ranking Loss: {label_ranking_loss(y_test, rf_y_pred):.3f}")
# Output -> RF Ranking Loss: 0.476
 

Follow the workflow for merging CYP datasets, preparing molecular representations, training multilabel classifiers, and evaluating predictions. section 5.4.2

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Defines multiclass, multilabel, and multi-output distinctions, then gives implementation and model-selection guidance.
Examples
Uses five CYP inhibition labels assigned to molecules as the central example.
What it assumes
Assumes basic classification vocabulary while defining multiclass, multilabel, and multi-output distinctions explicitly.
What you can do after
Apply it via MultiOutputClassifier or Random Forests to train and evaluate multilabel molecular predictors.
How it connects
Connects multiclass classification, multilabel classification, logistic regression, Random Forests, and MultiOutputClassifier.

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.