{ "cells": [ { "cell_type": "markdown", "id": "8f4fe37e", "metadata": {}, "source": [ "# A quick tour through UBayFS" ] }, { "cell_type": "markdown", "id": "9c50798f", "metadata": {}, "source": [ "## Introduction\n", "The UBayFS package implements the framework proposed in [Jenul et al. (2022)](https://link.springer.com/article/10.1007/s10994-022-06221-9). UBayFS is an ensemble feature selection technique embedded in a Bayesian statistical framework. The method combines data and user knowledge, where the first is extracted via data-driven ensemble feature selection. The user can control the feature selection by assigning prior weights to features and penalizing specific feature combinations. In particular, the user can define a maximum number of selected features and must-link constraints (features must be selected together) or cannot-link constraints (features must not be selected together). A parameter $\\rho$ regulates the shape of a penalty term accounting for side constraints, where feature sets that violate constraints lead to a lower target value. \n", "\n", "In this notebook, we use the [Breast Cancer Wisconsin dataset](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html) for demonstration. Specifically, the dataset consists of 569 samples and 30 features. The dataset describes a classification problem, where the aim is to distinguish between malignant and benign cancer based on image data. Features are derived from 10 image characteristics, where each characteristic is represented by three features (summary statistics) in the dataset. For instance, the characteristic *radius* is represented by features *radius mean*, *radius standard deviation*, and *radius worst*.\n", "\n", "\n", "## Requirements and dependencies\n", "- numpy>=1.23.5\n", "- pandas>=1.5.3\n", "- scikit-learn>=1.2.2\n", "- scipy>=1.10.0\n", "- random\n", "- sklearn-features>=1.1.0\n", "- mrmr>=0.2.6\n", "- pygad>=3.0.1\n", "- math\n", "\n", "To run UBayFS in Python we must import the classes UBaymodel and UBayconstraint." ] }, { "cell_type": "code", "execution_count": 1, "id": "0c0dddd5", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import sys\n", "sys.path.append(\"../../src/UBayFS\")\n", "from UBaymodel import UBaymodel\n", "from UBayconstraint import UBayconstraint" ] }, { "cell_type": "code", "execution_count": 2, "id": "80d7a1ac", "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv(\"./data/data.csv\")\n", "labels = pd.read_csv(\"./data/labels.csv\").replace((\"M\",\"B\"),(0,1)).astype(int)" ] }, { "cell_type": "markdown", "id": "4cbea67d", "metadata": {}, "source": [ "## Background\n", "This section summarizes the core parts of UBayFS, where a central part is Bayes' Theorem for two random variables $\\boldsymbol{\\theta}$ and $\\boldsymbol{y}$:\n", "$$p(\\boldsymbol{\\theta}|\\boldsymbol{y})\\propto p(\\boldsymbol{y}|\\boldsymbol{\\theta})\\cdot p(\\boldsymbol{\\theta}),$$ where $\\boldsymbol{\\theta}$ represents an importance parameter of single features and $\\boldsymbol{y}$ collects evidence about $\\boldsymbol{\\theta}$ from an ensemble of elementary feature selectors. In the following, the concept will be outlined. \n", "\n", "### Ensemble feature selection as likelihood\n", "The first step in UBayFS is to build $M$ ensembles of elementary feature selectors. Each elementary feature selector $m=1,\\dots,M$ selects features, denoted by a binary membership vector $\\boldsymbol{\\delta}^{(m)} \\in \\{0,1\\}^N$, based on a randomly selected training dataset, where $N$ denotes the total number of features in the dataset. In the binary membership vector $\\boldsymbol{\\delta}^{(m)}$, a component $\\delta_i^{(m)}=1$ indicates that feature $i\\in\\{1,\\dots,N\\}$ is selected, and $\\delta_i^{(m)}=0$ otherwise. Statistically, we interpret the result from each elementary feature selector as a realization from a multinomial distribution with parameters $\\boldsymbol{\\theta}$ and $l$, where $\\boldsymbol{\\theta}\\in[0,1]^N$ defines the success probabilities of sampling each feature in an individual feature selection and $l$ corresponds to the number of features selected in $\\boldsymbol{\\delta}^{(m)}$. Therefore, the joint probability density of the observed data $\\boldsymbol{y} = \\sum\\limits_{m=1}^{M}\\boldsymbol{\\delta}^{(m)}\\in\\{0,\\dots,M\\}^N$ --- the likelihood function --- has the form \n", "$$ p(\\boldsymbol{y}|\\boldsymbol{\\theta}) = \\prod\\limits_{m=1}^{M} f_{\\text{mult}}(\\boldsymbol{\\delta}^{(m)};\\boldsymbol{\\theta},l),$$\n", "where $f_{\\text{mult}}$ is the probability density function of the multinomial distribution.\n", "\n", "### Expert knowledge as prior\n", "UBayFS includes two types of expert knowledge: prior feature weights and feature set constraints. \n", "\n", "#### Prior feature weights\n", "\n", "To introduce expert knowledge about the importance of features, the user may define a vector $\\boldsymbol{\\alpha} = (\\alpha_1,\\dots,\\alpha_N)$, $\\alpha_i>0$ for all $i=1,\\dots,N$, assigning a weight to each feature. High weights indicate that a feature is important. By default, if all features are equally important or no prior weighting is used, $\\boldsymbol{\\alpha}$ is set to the 1-vector of length $N$. With the weighting in place, we assume the a-priori feature importance parameter $\\boldsymbol{\\theta}$ follows a Dirichlet distribution [@R:DirichletReg]\n", "$$p(\\boldsymbol{\\theta}) = f_{\\text{Dir}}(\\boldsymbol{\\theta};\\boldsymbol{\\alpha}),$$\n", "where the probability density function of the Dirichlet distribution is given as\n", "$$f_{\\text{Dir}}(\\boldsymbol{\\theta};\\boldsymbol{\\alpha}) = \\frac{1}{\\text{B}(\\boldsymbol{\\alpha})} \\prod\\limits_{n=1}^N \\theta_n^{\\alpha_n-1},$$\n", "where $\\text{B}(.)$ denotes the multivariate Beta function. Generalizations of the Dirichlet distribution [@wong:gdirichlet,@hankin:hyperdirichlet] are also implemented in UBayFS.\n", "\n", "Since the Dirichlet distribution is the conjugate prior with respect to a multivariate likelihood, the posterior density is given as\n", "$$p(\\boldsymbol{\\theta}|\\boldsymbol{y}) \\propto f_{\\text{Dir}}(\\boldsymbol{\\theta};\\boldsymbol{\\alpha}^\\circ),$$\n", "with\n", "$$\\boldsymbol{\\alpha}^\\circ = \\left( \\alpha_1 + \\sum\\limits_{m=1}^M \\delta_1^{(m)}, \\dots, \\alpha_N + \\sum\\limits_{m=1}^M \\delta_N^{(m)} \\right)$$\n", "representing the posterior parameter vector $\\boldsymbol{\\alpha}^\\circ$.\n", "\n", "#### Feature set constraints\n", "In addition to the prior weighting of features, the UBayFS user can also add different types of constraints to the feature selection:\n", "\n", "- *max-size constraint*: Maximum number of features that shall be selected.\n", "- *must-link constraint*: For a pair of features, either both or none is selected (defined as pairwise constraints, one for each pair of features).\n", "- *cannot-link constraint*: Used if a pair of features must not be selected jointly.\n", "\n", "All constraints can be defined *block-wise* between feature blocks (instead of individual features).\n", "Constraints are represented as a linear system of linear inequalities $\\boldsymbol{A}\\boldsymbol{\\delta}-\\boldsymbol{b}\\leq \\boldsymbol{0}$, where $\\boldsymbol{A}\\in\\mathbb{R}^{K\\times N}$ and $\\boldsymbol{b}\\in\\mathbb{R}^K$. $K$ denotes the total number of constraints. For constraint $k \\in 1,..,K$, a feature set $\\boldsymbol{\\delta}$ is admissible only if $\\left(\\boldsymbol{a}^{(k)}\\right)^T\\boldsymbol{\\delta} - b^{(k)} \\leq 0$, leading to the inadmissibility function (penalty term)\n", "\n", "$$\n", "\\kappa_{k,\\rho}(\\boldsymbol{\\delta}) = \\left\\{\n", " \\begin{array}{l l}\n", " 0 & \\text{if}~\\left(\\boldsymbol{a}^{(k)}\\right)^T\\boldsymbol{\\delta}\\leq b^{(k)}\\\\\n", " 1 & \\text{if}~ \\left(\\boldsymbol{a}^{(k)}\\right)^T\\boldsymbol{\\delta}> b^{(k)} \\land \\rho =\\infty\\\\\n", " \\frac{1-\\xi_{k,\\rho}}{1 + \\xi_{k,\\rho}} & \\text{otherwise},\n", " \\end{array}\n", " \\right.\n", "$$\n", " \n", "where $\\rho\\in\\mathbb{R}^+ \\cup \\{\\infty\\}$ denotes a relaxation parameter and\n", "$\\xi_{k,\\rho} = \\exp\\left(-\\rho \\left(\\left( \\boldsymbol{a}^{(k)}\\right)^T\\boldsymbol{\\delta} - b^{(k)}\\right)\\right)$ defines the exponential term of a logistic function. To handle $K$ different constraints for one feature selection problem, the joint inadmissibility function is given as\n", "$$ \\kappa(\\boldsymbol{\\delta})\n", " = 1 - \\prod\\limits_{k=1}^{K} \\left(1 -\\kappa_{k,\\rho}(\\boldsymbol{\\delta})\\right)$$\n", "which originates from the idea that $\\kappa = 1$ (maximum penalization) if at least one $\\kappa_{k,\\rho}=1$, while $\\kappa=0$ (no penalization) if all $\\kappa_{k,\\rho}=0$. \n", "\n", "To obtain an optimal feature set $\\boldsymbol{\\delta}^\\star$, we use a target function $U(\\boldsymbol{\\delta}, \\boldsymbol{\\theta})$ which represents a posterior expected utility of feature sets $\\boldsymbol{\\delta}$ given the posterior feature importance parameter $\\boldsymbol{\\theta}$, regularized by the inadmissibility function $\\kappa(.)$.\n", "\n", "$$\\mathbb{E}_{\\boldsymbol{\\theta}|\\boldsymbol{y}}[U(\\boldsymbol{\\delta}, \\boldsymbol{\\theta}(\\boldsymbol{y}))] = \\boldsymbol{\\delta}^T \\mathbb{E}_{\\boldsymbol{\\boldsymbol{\\delta}}|\\boldsymbol{y}}[\\boldsymbol{\\theta}(\\boldsymbol{y})]-\\lambda\\kappa(\\boldsymbol{\\delta})\\longrightarrow \\underset{\\boldsymbol{\\delta}\\in\\{0,1\\}^N}{\\text{arg max}}\n", "$$\n", " \n", "Since an exact optimization is impossible due to the non-linear function $\\kappa$, we use a genetic algorithm to find an appropriate feature set. In detail, the genetic algorithm is initialized via a Greedy algorithm and computes combinations of the given feature sets with regard to a fitness function in each iteration.\n" ] }, { "cell_type": "markdown", "id": "adaf3378", "metadata": {}, "source": [ "## Application of UBayFS\n", " \n", "### Ensemble Training\n", "The class ``UBaymodel()`` initializes the UBayFS model and trains an ensemble of elementary feature selectors. The training dataset and target are initialized with the arguments ``data`` and ``target``. Although the UBayFS concept permits unsupervised, multiclass, or regression setups, the current implementation supports binary target and regression variables only. While ``M`` defines the ensemble size (number of elementary feature selectors), the types of the elementary feature selectors is set via ``method``. The mRMR feature selector is implemented as baseline and can be called directlz with \"mrmr\". In general, the ``method`` argument allows for each self-implemented feature selection function with the arguments ``X`` (numpy array describing the data), ``y`` (numpy array describing the target), and ``n`` (describing the number of features that shall be selected. The function must return the indices of the selected features. Some examples are shown below.\n", "\n", "Each ensemble model is trained on a random subset comprising ``tt_split``$\\cdot 100$ percent of the train data. Using the argument ``prior_model`` the user specifies whether the standard Dirichlet distribution or a generalized variant should be used as prior model. Furthermore, the number of features selected in each ensemble can be controlled by the parameter ``nr_features``.\n", "\n", "The class ``UBayconstraint`` provides an easy way to define side constraints for the model. The generated object can be easily added to the UBaymodel by assigning it to the argument ``constraints`` which is by default ``None``.\n", "\n", "For the standard UBayFS initialization, all prior feature weights are set to 0.01, and only the required ``max_size`` constraint is included. Ensemble counts indicate how often a feature was selected over the ensemble feature selections. \n" ] }, { "cell_type": "code", "execution_count": 3, "id": "32e591a6", "metadata": {}, "outputs": [], "source": [ "model = UBaymodel(data = data,\n", " target = labels,\n", " feat_names = data.columns,\n", " M = 100,tt_split = 0.75,\n", " nr_features = 10,\n", " method = [\"mrmr\"],\n", " weights = [0.01],l = 1,\n", " constraints = UBayconstraint(rho=np.array([1]), \n", " constraint_types=[\"max_size\"], \n", " constraint_vars=[3], \n", " num_elements=data.shape[1]),\n", " optim_method = \"GA\",\n", " popsize = 100,\n", " maxiter = 100,\n", " random_state = 0)" ] }, { "cell_type": "code", "execution_count": 4, "id": "06a2ea5d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(30,)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.shape(model.getWeights())" ] }, { "cell_type": "code", "execution_count": 5, "id": "3bfb085b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | mean.radius | \n", "mean.texture | \n", "mean.perimeter | \n", "mean.area | \n", "mean.smoothness | \n", "mean.compactness | \n", "mean.concavity | \n", "mean.concave.points | \n", "mean.symmetry | \n", "mean.fractal.dimension | \n", "... | \n", "worst.radius | \n", "worst.texture | \n", "worst.perimeter | \n", "worst.area | \n", "worst.smoothness | \n", "worst.compactness | \n", "worst.concavity | \n", "worst.concave.points | \n", "worst.symmetry | \n", "worst.fractal.dimension | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ensemble count | \n", "100 | \n", "0 | \n", "100 | \n", "100 | \n", "0 | \n", "0 | \n", "100 | \n", "100 | \n", "0 | \n", "0 | \n", "... | \n", "100 | \n", "0 | \n", "100 | \n", "100 | \n", "0 | \n", "0 | \n", "98 | \n", "100 | \n", "0 | \n", "0 | \n", "
prior weight | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "... | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "0.01 | \n", "
2 rows × 30 columns
\n", "