convert Mahalanobis distance to units (inverse transform Mahalanobis)

28 Views Asked by At

I'm looking for a way to create data points at a given Mahalanobis distance from my sample data (mydata below). Basically I feed a distance to a function and then it outputs values of the variables that would lead to that Mahalanobis distance from my sample data.

What I'm looking for is similar to converting a Z-score to a value as below:

"""example for converting Z-score to value (but I'd like Mahalanobis to values)"""
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np

# generate data
x = []
y = []
for i in range(100):
    x_neg_or_pos = -1 if np.random.random() > 0.5 else 1
    x.append(np.random.random() * x_neg_or_pos)
    
    y_neg_or_pos = -1 if np.random.random() > 0.5 else 1    
    y.append(np.random.random() * y_neg_or_pos * np.random.random()**2)
    
mydata = pd.DataFrame({'x' : x, 'y' : y})


# fit scaler object
scaler = StandardScaler()
scaler.fit(mydata)


# create a blank data frame to fill in values corresponding to the z-scores (index)
values = pd.DataFrame(index=[1, 1.5, 3, 6, 10], columns=['x', 'y'])


# fill in values corresponding to specific z-scores that I'm interested in
for z in values.index:
    values.loc[z, :] = scaler.inverse_transform(  # convert z-score to data value
        pd.DataFrame({'x' : z, 'y' : z},
                     index=[0])
    )




0

There are 0 best solutions below