> For the complete documentation index, see [llms.txt](https://globalchem.gitbook.io/globalchem-your-chemical-graph-network/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://globalchem.gitbook.io/globalchem-your-chemical-graph-network/cheminformatics/one-hot-encoding-smiles.md).

# One-Hot Encoding SMILES

One-Hot encoding is the processing of moving something that is built on some sort of language can be indexed appropriately with numbers. In machine learning, it is common to convert strings to numeric characters so the machine can work with the numbers. Most often there is an encoder to convert the strings, perform the machine learning, and then decode the numbers to check the performance of the machine learning.

{% embed url="<https://towardsdatascience.com/basic-molecular-representation-for-machine-learning-b6be52e9ff76>" %}

**Imports**

```
from global_chem import GlobalChem
from global_chem_extensions import GlobalChemExtensions

gc = GlobalChem()
cheminformatics = GlobalChemExtensions().cheminformatics()
```

**Convert A List of SMILES to GlobalChem Encoder**

{% tabs %}
{% tab title="Code" %}

```
smiles_list = list(gc.get_node_smiles('pihkal').values())

encoded_smiles = cheminformatics.encode_smiles(smiles_list, max_length=200)

print ('Encoded SMILES: %s' % encoded_smiles[0])

decoded_smiles = cheminformatics.decode_smiles(encoded_smiles)

print ('Decoded SMILES: %s' % decoded_smiles[0])
```

{% endtab %}

{% tab title="Output" %}

```
Encoded SMILES: [[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
Decoded SMILES: CCC(N)CC1=CC(=C(OC)C(=C1)OC)OC
```

{% endtab %}
{% endtabs %}

**Algorithm**&#x20;

In the code there is an index mapping of the the SMILES to the index and then convert to numpy array. Currently, the mapping is set to this to include polymeric SMILES, atom classes, and virtual atoms.&#x20;

```
__SMILES_MAPPING__ = [
        ' ',
        '#', '%', '(', ')', '+', '-', '.', '/',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '=', '@',
        'A', 'B', 'C', 'F', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P',
        'R', 'S', 'T', 'V', 'X', 'Z',
        '[', '\\', ']',
        'a', 'b', 'c', 'e', 'g', 'i', 'l', 'n', 'o', 'p', 'r', 's',
        't', 'u',
        '&', ':', '*'
    ]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://globalchem.gitbook.io/globalchem-your-chemical-graph-network/cheminformatics/one-hot-encoding-smiles.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
