Keywords: Data Science, Statistik Beratung, Automatic Feature Engineering


We will be using the Auto MPG Data Set ( ) to demonstrate automatic feature engineering. This dataset is well suited since in practice one has datasets with mixed categorical and numerical attributes.


Besides for feature engineering being the first step in many machine learning algorithms, there seems to be no method which works well across multiple datasets and machine learning algorithms. Many algorithms require the data to be in numerical format. For example clustering is difficult with categorical data. ( ) If the data was in numerical vector format this would allow us to do dimensionality reduction (for example UMAP), visualize and plot the data after dimensionality reduction, observe the number of clusters and then do for example k-means clustering. On the other hand if the data is in numerical vector format, we could use SVM, Neural Networks, Linear Regression for regression or SVM, Neural Networks, Logistic Regression for classification. Ok, one could argue that Random Forest already deal with mixed categorical and numerical data frames, but why using only one method?

Proposed Method

The proposed method goes back to the mathematician Jean Bourgain (, who proved in 1985 that every metric space can be embedded with a “small” distortion in an euclidean space ( Original Work: , See: for an introduction). This means the following: If we have a distance between two data points, we can embed those data points in euclidean space, hence we get automatically numerical features for those data points. What remains is to define a distance between those points. I suggest the following general method:

For numerical features, scale them and use the cosine distance. For categorical features use the trivial metric (0 if the same category, 1 if not) and for sets or lits of categories (for example a list of words) use the Jaccard distance. Having those distance, with values between 0 and 1, we can simply compute the distance \(d = \sqrt{ \sum_i d_i^2 }\). Of course the distance depends on the application, but this suggestion is a first step.

Proposed Method in Action

Let us apply this method for the Auto MPG Data Set to do the following: 1) Regression 2) Visualization of the cars 3) Dimensionality reduction 4) Clustering 5) Computation of similar cars. Yes, all this can be done with mixed categorical and numerical data frames.

I do not want to go into details but the general procedure is this:

  1. Data Transformation:
  1. Define categorical, numerical features.
  2. NA in catgeroical are set to the category “NACAT”
  3. NA in numerical features are set to the median
  4. scale (variance = 1, mean = 0) the numerical variables
  1. Bourgain Embedding (Use the metrics defined above)

  2. Analyse the data now that it is in numerical format and apply your favourite machine learning algorithm.


Below is the python Script which does the Bourgain Embedding. The script ( where the method is coded) can be requested from me (

import os,csv,re,math,random
from scipy.spatial import distance

from bourgain import BourgainEmbedding as BE

numFeat = [5,6,7] # define numerical features
catFeat = [0,1,2] # define categorical features
jaccFeat = [3] # define set / list features

def jaccardDist(set1,set2):
    return 1-len(set1.intersection(set2))/(len(set1.union(set2)))*1.0

def dist(prod1,prod2):
    vec1 = [ float(prod1[i]) for i in numFeat]
    vec2 = [ float(prod2[i]) for i in numFeat]
    d1 = distance.cosine(vec1,vec2)
    ds = [1-(prod1[cf]==prod2[cf]) for cf in catFeat]
    for jf in jaccFeat:
        ds.append( jaccardDist( set(prod1[jf].split(" ")), set(prod2[jf].split(" ")))) 
    return math.sqrt(sum([dd**2 for dd in ds]))

filename = "./tt.csv"

def main():
    csvFile = open(filename,"r")
    csvReader = csv.reader(csvFile,dialect=csv.excel)
    newFile = open("./tt-features.csv","w")
    newCW = csv.writer(newFile,dialect=csv.excel)
    lines = []
    for line in csvReader:
    randomsubset = random.sample(lines,len(lines))
    be = BE(dist),verbose=True)
    featureVectors = be.predict(lines,verbose=True)
    for i in range(len(lines)):
        fv = featureVectors[i]
        fv.insert(0,lines[i][4]) # mpg- for regression
        fv.insert(0,lines[i][3]) # name of car

if __name__ == '__main__':

R-Script for computing the most similar cars: (knn)

d <- read.csv("tt-features.csv",sep=",",header=F)
rs <- sample(seq(1,dim(d)[1]))
dfnn <- data.frame(qp=numeric(0),knn=numeric(0))
vars <- seq(3,dim(d)[2])
k <- 3
knns <- get.knnx(data=d[,vars],query=d[,vars],k=k)

for(i in seq(1,N)){
    querypoint <- i
    knn <- knns$nn.index[i,]
    ndf <- data.frame(qp=rep(querypoint,k),knn=c(knn))
    result <- d[knn,c("V1","V2")]
    qr <- d[i,c("V1","V2")]
    names(result) <- c("V3","V4")
    simProds <- rbind(simProds,cbind(qr,result))



Result of similarity products

Here are some similar cars:

I am not an expert in cars, but the first similarity result shows, that the cars Chevrolet Chevelle Malibu ( and Chevrolet Monte Carlo ( are related by Wikipedia Articles, so also this is no mathematical proof, the method, can not be that bad. ;)