> ## Documentation Index
> Fetch the complete documentation index at: https://docs.graphext.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Graphs and data types

## Introduction

[*UMAP*](/guides/graphs/#dimensionality-reduction-as-layout) and our proprietary [*k-NN*](http://127.0.0.1:8000/guides/graphs/#overview) graph treat the relative balance between categorical and numeric data differently, leading most of the time to qualitatively different embeddings. The k-NNG method has a tendency to clearly separate the network into clusters such that each cluster corresponds to a combination of categories (across different variables). UMAP tends to not divide the network as sharply based on categorical columns, yet can be configured to give them more or less influence on the resulting embedding.

## Example 1 – Human resources data

As a first example, we use a human resources dataset with 10 numeric and 2 categorical columns. The categorical columns represent the *salary* of employees (3 levels) and their *department* (10 levels). We will also be showing two numeric columns: the *number of projects* (integer values from 2 to 7), and *satisfaction level* (continuous in \[0, 1]).

### k-NNG embedding

The k-NNG method produces the following layout:

<Frame caption="Figure 1: K-nn graph colored from top to bottom and left to right by salary, department, number of projects, and satisfaction level.">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/1_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=2fd8888850e2904ce3681aca03ca2520" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/1_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/1_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=96170444aa50d50c8db60a302f47f097" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/1_2.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/1_3.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=89a6c8578446bfcd2448f5d595e9dd6f" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/1_3.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/1_4.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=5a36eaf78cc460caa7f48fcacaecf8a3" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/1_4.webp" />
    </Frame>
  </CardGroup>
</Frame>

Coloring in the top row by salary on the left, and department on the right, we see that the network forms clearly distinct clusters, and such that each cluster corresponds to a combination of the two kinds of categories. This is neither right nor wrong. And whether it's useful depends on the kinds of questions we're interested in. E.g. separating employees so clearly by department may or may not make sense in a specific scenario, since belonging to a specific department may or may not have a significance influence on other variables we may be interested in.

Within each cluster, points are organized according to the numeric variables, forming different directions/gradients of increasing or decreasing values etc. This is more visible in case of the ordinal-like variable *number of projects,* having 7 different values only, than in the case of *satisfaction level*.

### UMAP embedding

Using UMAP has the advantage of giving us more influence on the relative importance of categorical vs numeric variables. In Graphext, this can be done with the `type_weights` parameter, e.g.

```jsx theme={null}
embed_dataset(ds, {"type_weights": {"category": 4.0}}) -> (ds.embedding)
```

In the following series of figures we plot UMAP layouts with different weights being applied to categorical columns.

With default weighting, and using the same color mappings as before:

<Frame
  caption="
Figure 2: UMAP embedding colored from top to bottom and left to right by salary, department, number of projects, and satisfaction level (i.e. equivalent to Figure 1).
"
>
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/2_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=e8c5ea6fcb6a9f8c859b482bb5bb2b9a" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/2_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/2_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=67aa3f5fbb1e3588690ed8aad9f85b2b" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/2_2.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/2_3.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=19d45babc42d5b9dd83c352e213f3f2e" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/2_3.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/2_4.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=d40308c02a09f17e7d70a9dd75f775f5" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/2_4.webp" />
    </Frame>
  </CardGroup>
</Frame>

It is clear that UMAP in this case globally organizes the data according to similarity in the numeric columns, more so than according to the categorical columns.

Note, however, that *salary* data is not randomly or uniformly distributed here, even if that may seem to be the case at first glance. Even though the different levels of the categorical variables can be found spread across the entire network, they nevertheless tend to form little groups, i.e. nodes of the same category more often connect to each other than do nodes with different categories.

Increasing the weight of categorical columns to 6.0 (vs. the default of 1.0 for the rest), we see that data starts to separate into clusters representing different *salary* categories (top left e.g.).

<Frame caption="Figure 3: Color mapped to salary and department, categorical weight of 6.0.">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/3_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=b84b98157167467fe9aff08cdfb434de" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/3_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/3_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=a46bc2331dc4e819ac9b0a9e1f5c1be1" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/3_2.webp" />
    </Frame>
  </CardGroup>
</Frame>

With a weight of 8, the salary variable starts to dominate the global structure, and the network separates into 3 distinct clusters. The department variable also starts to separate parts of the network more clearly:

<Frame caption="Figure 4: Color mapped to salary and department, categorical weight of 8.0.">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/4_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=bd6f37175796d24adafffe21bb140a28" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/4_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/4_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=c069a878a006df7ee70f2ee616e584f7" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/4_2.webp" />
    </Frame>
  </CardGroup>
</Frame>

The same tendency continues with a weight of 10.

<Frame caption="Figure 5: .Color mapped to salary and department, categorical weight of 10.0.">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/5_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=8602f1daac4c2843d26950044f2d0c62" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/5_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/5_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=9075369e0a237ad524152255027351ef" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/5_2.webp" />
    </Frame>
  </CardGroup>
</Frame>

And with weights of 12 and 14 respectively, the network separates into completely distinct clusters, such that each cluster corresponds to a combination of the categories in the two categorical variables.

<Frame caption="Figure 6: Color mapped to salary and department, categorical weight of 12.0 (first row) and 14.0 (second row).">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/6_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=c51e0c8d018a51798a9ecca286bf2a9b" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/6_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/6_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=1254c7ebe1328d203e550ed4e917665f" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/6_2.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/6_3.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=047638faa9e98606ffd86644dc3fdb69" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/6_3.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/6_4.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=e559c9ac749bc8806ba4f49339f37cb8" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/6_4.webp" />
    </Frame>
  </CardGroup>
</Frame>

Note that in no case are numeric columns ignored, even when the network separates into disconnected islands representing individual categories. The following figures again map color by our two numeric columns, using an embedding with categorical weight of 14:

<Frame caption="Figure 7: Color mapped to numeric columns number of projects, and satisfaction level, categorical weight of 14.0.">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/7_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=adbea02dc504826d49c010318405cf9f" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/7_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/7_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=1c6ad80befedf9ae238ea6d751d9e75e" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/7_2.webp" />
    </Frame>
  </CardGroup>
</Frame>

As can be seen, local structure in numeric variables is preserved inside and/or across the individual clusters. If anything, in a more organized manner than the original k-NN graph.

Note that apart from the above observations, UMAP also better maintains the relationship *between* clusters. While the relative positions of isolated clusters in the k-NN graph are completely arbitrary (as a direct implication of the underlying algorithm), UMAP is able to maintain the global structure inherent in the data.

## Example 2 – Titanic dataset

As a another example we will look at (a subset of) the Titanic dataset, which for each passenger contains 3 categorical columns (*sex*, *passenger class,* and *port of embarkment*), and 4 numeric columns (*age*, *number of siblings and spouses aboard*, *number of parents and children aboard*, and *fare price*).

### k-NNG embedding

The k-NNG method produces the following layout (*double-click on an image to see a bigger version*):

<Frame caption="Figure 8: K-nn graph colored from top to bottom and left to right by sex, passenger class, age, and fare price">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/8_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=d737df1751da3b53847ef2e10ebd03e5" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/8_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/8_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=6b28843df0cd2efe0a459054e727dd2b" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/8_2.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/8_3.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=c4d577cc645b92b023207eff729c3455" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/8_3.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/8_4.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=a18c75bed20b5e2c55eb3fc81ada35c0" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/8_4.webp" />
    </Frame>
  </CardGroup>
</Frame>

As before we can see that the k-NNG method strongly separates different categories into separate clusters. Numeric variables than usually influence how nodes are distributed *inside* the clusters. Note that the approach seems to struggle somewhat in mapping the *age* variable\*.\*

### UMAP embedding

For this dataset, using UMAP with default column weights seems to achieve a good balance between categorical and numerical variables out of the box (note that in the following figures we changed the `min_dist`, and `n_epochs` parameters only, as with default values the resulting layout is rather thinly spread):

<Frame caption="Figure 9: UMAP embedding colored from top to bottom and left to right by sex, passenger class, age, and fare price.">
  <CardGroup className="gap-0" cols={2}>
    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/9_1.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=b0b6dfd65ba890fcc5164994bedc88b5" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/9_1.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/9_2.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=0298c71bc06802025d9454fa924d73e9" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/9_2.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/9_3.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=4859dedeb38cf9145c27c99c19c8f687" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/9_3.webp" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/graphext/j2bb0t8eT1oUivLV/images/ds-concepts/graphs_dtypes/9_4.webp?fit=max&auto=format&n=j2bb0t8eT1oUivLV&q=85&s=a117c58f0b48bb66426e2aff696df905" alt="graph layout" width="800" height="597" data-path="images/ds-concepts/graphs_dtypes/9_4.webp" />
    </Frame>
  </CardGroup>
</Frame>

Note that not only are the different categories well represented by identifiable clusters, but numeric variables map much more cleanly to well defined areas or gradients in the layout, when compared with the k-NNG approach.

## Conclusions

As we have illustrated with two examples above, it is possible in many cases to tune UMAP embeddings such that the equilibrium between categorical and numerical variables corresponds to one's expectations (e.g. qualitatively matches results from the alternative k-NNG approach). Depending on the dataset, and the actual distribution of its data, the weighting required to achieve preferred results may differ. And so it is usually an iterative process to arrive at the final configuration. Also, note that not all undesirable layouts are due to the balance between different data types. Sometimes data may simply be distributed in such a way that no "clean" layout can be calculated that would still reflect the truth about the similarities of dataset rows. In other cases it may require tuning of UMAP's own parameters to achieve best results (see [Creating graphs and layout](/docs/recipes/graphs/create/) as well as Graphext's documention of the steps/methods involved, e.g. [embed\_dataset](/docs/steps/prepare/embed/embed_dataset/)).
