Name Ethnicity Classification
This repository is part of the name-to-ethnicity research project. If you use this classifier for your scientific work, please cite our paper.
What is name-ethnicity classification?
Name-ethnicity classification is the process of using a person's name to predict their ethnicity. It involves analyzing the linguistic features of the name to determine the likely ethnicity. This can help researchers detect potential biases and discrimination in various contexts, such as education, employment and healthcare.
This project contains a Python package and CLI tool called n2e.
⬇️ Installation:
This project uses the uv Python package manager.
Install the package:
For installing the n2e CLI interface, run:
uv tool install n2e
For the n2e Python package, run:
uv add n2e
If you don't use uv, run:
pip install n2e
For local development:
If you want to set up this repository for local development, clone the repository instead:
git clone https://github.com/name-ethnicity-classifier/name-ethnicity-classifier
cd name-ethnicity-classifier/
Dependencies:
The project was tested with Python ≥ 3.10. The following packages are installed and used as dependencies: NumPy, Pandas and PyTorch.
👨💻 Usage:
Read this first:
Before you start classifying, check out the different model configurations in the "Models" table below. Models are hosted as GitHub release assets and downloaded automatically the first time you use one, then cached in ~/.cache/n2e/.
There you will find different models which each classify a unique set of ethnicities and their average accuracy.
When using the CLI or the Python package, you can specify which model you want to use.
None of the models is suitable for your problem?
On the website www.name-to-ethnicity.com you can request custom models trained on selected ethnicities (for free!). Alternatively, you can use the model-training repository to train models locally.
n2e CLI:
The following flags are available:
| flag | description | example |
|---|---|---|
-i, --input |
Sets the path to an input .csv file containing first and last names; must contain one column called "names". | -i "./examples/names.csv" (required) |
-o, --output |
Path to an output .csv in which the names along with the predictions will be stored (file will be created if it doesn't exist). | -o "./examples/predictions.csv" (optional, default: {input file name}_output.csv) |
-m, --model |
Name of model configuration which can be chosen from the table below. | -m indian_and_else (optional, default: 21_nationalities_and_else) |
-d, --device |
Device on which the model will run, must be either "gpu" or "cpu". | -d "gpu" (optional, default: gpu) |
-b, --batchsize |
Specifies how many names will be processed in parallel (if it crashes choose a batch-size smaller than the amount of names in your .csv file). | -b 128 (optional, default: amount of names in input-file) |
--distribution |
If set, the output will contain the entire output distribution, ie. providing the confidence for all possible ethnicities. | No parameter |
To classify a list of names put them into a .csv file, with one column named "names". For example:
| names |
|---|
| Giorgos Papadopoulos |
| Max Mustermann |
Then run the classification, for example like this:
n2e -i ./examples/names.csv -o ./examples/predicted_ethnicities.csv -m 21_nationalities_and_else -d gpu -b 64
After running the command, the output .csv will look like this:
| names | predictions | confidences |
|---|---|---|
| Giorgos Papadopoulos | greek | 0.73 |
| Max Mustermann | german | 0.92 |
If the --distribution flag was set the output .csv will look like this:
| names | predictions | greek | german |
|---|---|---|---|
| Giorgos Papadopoulos | greek | 0.73 | 0.27 |
| Max Mustermann | german | 0.08 | 0.92 |
Python API:
Import predict_ethnicities to use n2e directly as a Python module:
from n2e import predict_ethnicities
predict_ethnicities(
names, # list[str]
batch_size=128, # names processed in parallel
model="21_nationalities_and_else", # any model from the table below
get_distribution=False, # return confidences for every ethnicity
)
Returns a list of (ethnicity, confidence) tuples, one per name:
>>> predict_ethnicities(["Giorgos Papadopoulos"])
[('greek', 99.045)]
>>> predict_ethnicities(["Giorgos Papadopoulos", "Max Mustermann"])
[('greek', 99.045), ('german', 60.34)]
With get_distribution=True you get a full distribution per name:
>>> predict_ethnicities(["Giorgos Papadopoulos"], get_distribution=True)
[{'british': 0.073, 'else': 0.046, 'indian': 0.008, ...}]
🌍 Models:
| name | classes | accuracy |
|---|---|---|
28_nationalities_english_once |
click to see classesbritish norwegian indian hungarian spanish german zimbabwean portugese polish bulgarian bangladeshi turkish belgian pakistani italian romanian lithuanian french chinese swedish nigerian greek south african japanese dutch danish russian filipino |
78.54% |
21_nationalities_and_else |
click to see classesbritish else indian hungarian spanish german zimbabwean polish bulgarian turkish pakistani italian romanian french chinese swedish nigerian greek japanese dutch ukrainian danish russian |
81.08% |
8_groups |
click to see classesafrican celtic eastAsian european hispanic muslim nordic southAsian |
83.55% |
chinese_and_else |
click to see classeschinese else |
98.55% |
20_most_occuring_nationalities |
click to see classesbritish norwegian indian irish spanish american german polish bulgarian turkish pakistani italian romanian french australian chinese swedish nigerian dutch filipino |
75.36% |
german_austrian_and_else |
click to see classesgerman/austrian combined else |
88.1% |
indian_and_else |
click to see classeselse indian |
94.63% |
japanese_and_else |
click to see classeselse japanese |
99.33% |
newzealand_and_else |
click to see classeselse new zealander |
66.71% |
Metadata
Release files for n2e 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| n2e-0.1.0.tar.gz | 22.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| n2e-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.0 kB
Release files / n2e-0.1.0.tar.gz
| Download URL | n2e-0.1.0.tar.gz |
|---|---|
| Size | 22.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2a2102bda8fb7fc5f648ef1ee776314484d6622406168ca962a380a2d78130f0
|
|
BLAKE2b-256 checksum How to use checksums |
fc622a29da6cc829dfb9f1fa78d5da2aabc1d66cb23a584f8a1db4f179084ea2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / n2e-0.1.0-py3-none-any.whl
| Download URL | n2e-0.1.0-py3-none-any.whl |
|---|---|
| Size | 22.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
242338a25a4314fbee84bbd985386eccdb9bd775ca7d33420f4f09cef2a8594e
|
|
BLAKE2b-256 checksum How to use checksums |
7d8f11f6a0712ab63cdd8dd35019a220b9a4e5de5939726683264f6083f3732b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|