<jupyter_start><jupyter_text>Data Discretization and Gaussian Mixture ModelsThe purpose of this notebook is to explore the use of Gaussian Mixture Models (GMMs) to discretize data. Here, we generate dummy data from 2 populations (male children and female children). The features that we generate are all gaussians and hypothetical around height, weight and sleep, denoted as $X_0$, $X_1$ and $X_2$, respectively. For the male children, these variables are sampled as follows.* $X_0 \sim \mathcal{N}(60, 5)$* $X_1 \sim \mathcal{N}(50 + 0.5 X_0, 20)$* $X_2 \sim \mathcal{N}(1 + 0.1 X_1, 1.5)$For the female children, these variables are sampled as follows.* $X_0 \sim \mathcal{N}(50, 5)$* $X_1 \sim \mathcal{N}(45 + 0.5 X_0, 10)$* $X_2 \sim \mathcal{N}(1.5 + 0.1 X_1, 1.5)$<jupyter_code>%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import random
from scipy import stats

random.seed(37)
np.random.seed(37)
plt.style.use('ggplot')

def get_m_ch_data(N=1000):
    h = np.random.normal(60, 5, N)
    w = np.random.normal(50 + 0.5 * h, 20, N)
    s = np.random.normal(1 + 0.1 * w, 1.5, N)
    return np.concatenate([h.reshape(-1, 1), w.reshape(-1, 1), s.reshape(-1, 1)], axis=1)

def get_f_ch_data(N=1000):
    h = np.random.normal(50, 5, N)
    w = np.random.normal(45 + 0.5 * h, 10, N)
    s = np.random.normal(1.5 + 0.1 * w, 1, N)
    return np.concatenate([h.reshape(-1, 1), w.reshape(-1, 1), s.reshape(-1, 1)], axis=1)

def get_data(N=1000):
    m_ch = get_m_ch_data(N)
    f_ch = get_f_ch_data(N)
    return np.concatenate([m_ch, f_ch], axis=0)
X = get_data()<jupyter_output><empty_output><jupyter_text>VisualizationHere are some visualization of the variables. Had we not known that the full data comes from two populations, it is difficult to tell from the univariate, bivariate and multivariate plots if there are any clusters. Univariate Density PlotWith the exception of $X_0$, all variables seem like well-behaved gaussian distributed variables. For $X_0$, there is a hump that visually detectable.<jupyter_code>fig, ax = plt.subplots(1, 3, figsize=(15, 5))

for c in range(X.shape[1]):
    sns.distplot(X[:,c], ax=ax[c])
    ax[c].set_title(r'$X_{}$'.format(c))

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Bivariate Density PlotsThese are pairwise density plots for the three variables. For $X_0$ vs $X_1$ and $X_0$ vs $X_2$, there seems to be a skewing to the top right. Again, not indicative of any clusters or anything separable along the pairwise dimensions.<jupyter_code>fig, ax = plt.subplots(1, 3, figsize=(15, 5))

sns.kdeplot(X[:,0], X[:,1], ax=ax[0], cmap="Reds", shade=True)
sns.kdeplot(X[:,0], X[:,2], ax=ax[1], cmap="Reds", shade=True)
sns.kdeplot(X[:,1], X[:,2], ax=ax[2], cmap="Reds", shade=True)

ax[0].set_xlabel(r'$X_0$'); ax[0].set_ylabel(r'$X_1$'); 
ax[1].set_xlabel(r'$X_0$'); ax[1].set_ylabel(r'$X_2$'); 
ax[2].set_xlabel(r'$X_1$'); ax[2].set_ylabel(r'$X_2$'); 

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Multivariate Scatter PlotHere's a scatter plot. It looks nice as a 3-D plot, but not telling of any patterns.<jupyter_code>from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')

ax.scatter(X[:,0], X[:,1], X[:,2], c=X[:,2], cmap='viridis', linewidth=0, antialiased=True)
ax.set_xlabel(r'$X_0$')
ax.set_ylabel(r'$X_1$')
_ = ax.set_zlabel(r'$X_2$')<jupyter_output><empty_output><jupyter_text>Gaussian Mixture Model (GMM)Now, let's see if we may use GMM as an unsupervised learning technique to recover any meaningful clusters in the full data. When we use GMM, we iterate from 2 to 5 (inclusive both ends) and observe the associated [AIC](https://en.wikipedia.org/wiki/Akaike_information_criterion) score (a lower AIC is better). Using GMM, we discover that when the number of components is 2, that model has the lowest AIC score. This observation already is promising, as we know that there are 2 populations and GMM (with the number of components set to 2) has basically recovered the clusters.<jupyter_code>from sklearn.mixture import GaussianMixture

def get_gmm_labels(X, k):
    gmm = GaussianMixture(n_components=k, max_iter=50, random_state=37)
    gmm.fit(X)
    aic = gmm.aic(X)
    bic = gmm.bic(X)
    return aic, bic, k, gmm

gmm_scores = [get_gmm_labels(X, k) for k in range(2, 6)]
gmm_scores = sorted(gmm_scores, key=lambda tup: (tup[0], tup[2]))
gmm = gmm_scores[0][3]<jupyter_output><empty_output><jupyter_text>Look at the means. They are nearly identical to the expected values for each cluster.<jupyter_code>gmm.means_
np.sqrt(gmm.covariances_)<jupyter_output><empty_output><jupyter_text>The weights are a bit off; they should be nearly 50% each, however, the zeroth cluster is at 60%.<jupyter_code>gmm.weights_<jupyter_output><empty_output><jupyter_text>Data discretizationWe can actually exploit a GMM model to discretize the data. The logic is very simple. Associated with a GMM are N clusters (or components) and each has an associated vector of means and matrix of covariance; these may be used to form a multivariate gaussian or marginalized out to form a univariate guassian. Call any cluster/component the i-th cluster, and call any mean the j-th mean (there should be as many means as there are features; in this running example, 3). For a value $v$ from the j-th variable, compute the probabilty of the value for each probability density function $P_{ij}(v)$. Take the i-th index associated with the highest $P_{ij}$, and that is the label associated with that value (this step is the discretization).In this example, since there are 2 components in the GMM, this reduces the mapping of continuous values to the binary domain of 0 or 1.  SimulationHere, we simulate $y$, $S$ and $D$ where* $y$ is a vector of labels, $y \in \{0, 1\}$,* $S$ is a matrix of simulated (continuous) data from the GMM, and* $D$ is a matrix corresponding to $S$ with the data discretized.Using the `fitting` pattern of the Scikit-Learn API's, we can conveniently fit models to the pairs, $(S, y)$ and $(D, y)$.<jupyter_code>unvgauss = {}
for i in range(gmm.n_components):
    unvgauss[i] = {}
    for j in range(len(gmm.means_[i])):
        unvgauss[i][j] = stats.norm(gmm.means_[i][j], np.sqrt(gmm.covariances_)[i][j][j])
mvngauss = {}
for i in range(gmm.n_components):
    mvngauss[i] = stats.multivariate_normal(gmm.means_[i], gmm.covariances_[i])
from scipy.stats import dirichlet

def get_data_labels(unvgauss, r):
    data = []
    num_clusters = len(unvgauss)
    num_vars = len(unvgauss[0])
    for v in range(num_vars):
        p = np.argmax([unvgauss[c][v].pdf(r[v]) for c in range(num_clusters)])
        data.append(p)
    return np.array(data)

def get_simulated_data(X, gmm):
    y = np.argmax(dirichlet.rvs(gmm.weights_, size=X.shape[0], random_state=37), axis=1)
    S = np.vstack([mvngauss[label].rvs() for label in y])
    D = np.vstack([get_data_labels(unvgauss, S[r]) for r in range(S.shape[0])])
    return y, S, D
y, S, D = get_simulated_data(X, gmm)<jupyter_output><empty_output><jupyter_text>Applying logistic regression on continuous dataWhen logistic regression learns and validates against the same continuous data, the [AUROC](https://en.wikipedia.org/wiki/Receiver_operating_characteristic) score is 90%.<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score

lr = LogisticRegression(solver='lbfgs', random_state=37)
lr.fit(S, y)
print(lr.coef_)
y_pred = lr.predict_proba(S)[:,1]
print(lr.score(S, y))
print(roc_auc_score(y, y_pred))<jupyter_output>[[ 0.33399925  0.05627331 -0.47788181]]
0.8295
0.9042213606795519<jupyter_text>Applying logistic regression on discretized dataWhen logistic regression learns and validatest against the same discretized data, the AUROC is 86%.<jupyter_code>lr = LogisticRegression(solver='lbfgs', random_state=37)
lr.fit(D, y)
print(lr.coef_)
y_pred = lr.predict_proba(D)[:,1]
print(lr.score(D, y))
print(roc_auc_score(y, y_pred))<jupyter_output>[[2.7574824  1.03567117 0.86697295]]
0.8065
0.8702358831938608<jupyter_text>Applying random forest on continuous dataWhen random forest learns and validates against the same continuous data, the AUROC is nearly perfect at 99%.<jupyter_code>from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(n_estimators=100, random_state=37)
rf.fit(S, y)
y_pred = rf.predict_proba(S)[:,1]
print(roc_auc_score(y, y_pred))<jupyter_output>1.0<jupyter_text>Applying random forest on discretized dataWhen random forest learns and validates against the same discretized data, the AUROC is nearly perfect at 86%.<jupyter_code>rf = RandomForestClassifier(n_estimators=100, random_state=37)
rf.fit(D, y)
y_pred = rf.predict_proba(D)[:,1]
print(roc_auc_score(y, y_pred))<jupyter_output>0.8702358831938608<jupyter_text>Testing logistic regression and random forest on hold-out continuous dataHere we apply logistic regression and random forest to learn from the same continous data, but the validation is against a hold-out set (never seen before).<jupyter_code>y_h, S_h, D_h = get_simulated_data(X, gmm)

lr = LogisticRegression(solver='lbfgs', random_state=37)
lr.fit(S, y)
y_pred = lr.predict_proba(S_h)[:,1]
print(roc_auc_score(y_h, y_pred))

print('>')

rf = RandomForestClassifier(n_estimators=100, random_state=37)
rf.fit(S, y)
y_pred = rf.predict_proba(S_h)[:,1]
print(roc_auc_score(y_h, y_pred))<jupyter_output>0.895238535396134
>
0.9092054001989415<jupyter_text>Testing logistic regression and random forest on hold-out discretized dataHere we apply logistic regression and random forest to learn from the same discretized data, but the validation is against a hold-out set (never seen before).<jupyter_code>lr = LogisticRegression(solver='lbfgs', random_state=37)
lr.fit(D, y)
y_pred = lr.predict_proba(D_h)[:,1]
print(roc_auc_score(y_h, y_pred))

print('>')

rf = RandomForestClassifier(n_estimators=100, random_state=37)
rf.fit(D, y)
y_pred = rf.predict_proba(D_h)[:,1]
print(roc_auc_score(y_h, y_pred))<jupyter_output>0.8597473422832468
>
0.8597473422832468<jupyter_text>Testing random forest on hold-out dataHere we want to see how well random forest performs on unseen continuous and discretized data. We want to compare if we have sacrificed any accuracy when going from the continuous to discretized domain for classification tasks. We always learn our models from the same original data, $S$ (continuous) or $D$ (discretized), but we validate on 20 new synthetic data sets the model has not seen before.<jupyter_code>import pandas as pd

def do_validation(S, y, X, gmm):
    y_h, S_h, D_h = get_simulated_data(X, gmm)

    rf = RandomForestClassifier(n_estimators=100, random_state=37)
    rf.fit(S, y)
    y_pred = rf.predict_proba(S_h)[:,1]
    s_score = roc_auc_score(y_h, y_pred)
    
    rf = RandomForestClassifier(n_estimators=100, random_state=37)
    rf.fit(D, y)
    y_pred = rf.predict_proba(D_h)[:,1]
    d_score = roc_auc_score(y_h, y_pred)
    
    return s_score, d_score

valid_scores = pd.DataFrame([do_validation(S, y, X, gmm) for _ in range(20)], columns=['continuous', 'discretized'])<jupyter_output><empty_output><jupyter_text>The mean AUROC scores when using random forest on continuous and discretized data seem very close.<jupyter_code>print(valid_scores.mean())

print('difference is {:.3f}'.format(
    valid_scores.mean().values[0] - valid_scores.mean().values[1]))<jupyter_output>continuous     0.914705
discretized    0.871970
dtype: float64
difference is 0.043<jupyter_text>A simple two-sided t-test indicates that p < 0.01, so we fail to reject the null hypothesis and there is a significant difference between the average AUROC performance.<jupyter_code>stats.ttest_ind(valid_scores['continuous'], valid_scores['discretized'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12066644227532818, 'fraction_numerical': 0.03862335913833726, 'mean_word_length': 5.566298342541437, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 66, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '41519', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 4618, 'n_tokens_neox': 4274, 'n_words': 1251, 'path': 'sphinx/datascience/source/gmm-data-discretization.ipynb', 'repo_name': 'oneoffcoder/books', 'size': '11884'}