instate 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
instate-1.1.0/PKG-INFO ADDED
@@ -0,0 +1,219 @@
1
+ Metadata-Version: 2.3
2
+ Name: instate
3
+ Version: 1.1.0
4
+ Summary: Instate: predict the state of residence from last name
5
+ Keywords: predict,state,residence,last name
6
+ Author: Atul Dhingra, Gaurav Sood, Rajashekar Chintalapati
7
+ Author-email: Atul Dhingra <dhingra.atul92@gmail.com>, Gaurav Sood <gsood07@gmail.com>, Rajashekar Chintalapati <rajshekar.ch@gmail.com>
8
+ License: MIT
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: Topic :: Utilities
18
+ Requires-Dist: pandas>=1.5.0
19
+ Requires-Dist: numpy>=1.21.0,<2.0.0
20
+ Requires-Dist: torch>=2.0.0
21
+ Requires-Dist: levenshtein>=0.20.0
22
+ Requires-Dist: requests>=2.25.0
23
+ Requires-Dist: tqdm>=4.64.0
24
+ Requires-Python: >=3.11, <3.14
25
+ Project-URL: Bug Tracker, https://github.com/appeler/instate/issues
26
+ Project-URL: Homepage, https://github.com/appeler/instate
27
+ Project-URL: Repository, https://github.com/appeler/instate
28
+ Description-Content-Type: text/markdown
29
+
30
+ ## instate: predict spoken language and the state of residence from last name
31
+
32
+ [![image](https://github.com/appeler/instate/workflows/test/badge.svg)](https://github.com/appeler/instate/actions?query=workflow%3Atest)
33
+ [![image](https://img.shields.io/pypi/v/instate.svg)](https://pypi.org/project/instate)
34
+ [![Documentation](https://github.com/appeler/instate/actions/workflows/docs.yml/badge.svg)](https://github.com/appeler/instate/actions/workflows/docs.yml)
35
+ [![image](https://static.pepy.tech/badge/instate)](https://pepy.tech/project/instate)
36
+
37
+ Using the Indian electoral rolls data (2017), we provide a Python
38
+ package that takes the last name of a person and gives its distribution
39
+ across states. This package can also predict the spoken language of the
40
+ person based on the last name.
41
+
42
+ # Potential Use Cases
43
+
44
+ India has 22 official languages. To serve such a diverse language base
45
+ is a challenge for businesses and surveyors. To the extent that
46
+ businesses have access to the last name (and no other information) and
47
+ in the absence of other data that allows us to model a person\'s spoken
48
+ language, the distribution of last names across states is the best we
49
+ have.
50
+
51
+ # Dataset
52
+
53
+ Refer to
54
+ [lastname_langs_india.csv.tar.gz](https://github.com/appeler/instate/blob/main/instate/data/lastname_langs_india.csv.tar.gz)
55
+ for the dataset that will be used to predict/lookup the spoken language
56
+ based on the last name.
57
+
58
+ Refer to
59
+ [lastname_langs_india_top3.csv.tar.gz](https://github.com/appeler/instate/blob/main/instate/data/lastname_langs_india_top3.csv.tar.gz)
60
+ for the dataset that will be used to predict the top-3 spoken languages
61
+ based on the last name. A LSTM model has been trained on this dataset to
62
+ predict the top-3 spoken languages.
63
+
64
+ Refer to the
65
+ [notebooks](https://github.com/appeler/instate/tree/main/model_training/notebooks)
66
+ for the notebooks that were used to prepare the above datasets and train the
67
+ models.
68
+
69
+ # Web UI
70
+
71
+ Note: Streamlit app is currently unavailable.
72
+
73
+ # Installation
74
+
75
+ We strongly recommend installing instate inside a Python
76
+ virtual environment (see [venv
77
+ documentation](https://docs.python.org/3/library/venv.html#creating-virtual-environments))
78
+
79
+ pip install instate
80
+
81
+ # Examples
82
+
83
+ from instate import last_state
84
+ last_dat = pd.read_csv("last_dat.csv")
85
+ last_state_dat = last_state(last_dat, "dhingra")
86
+ print(last_state_dat)
87
+
88
+ # API
89
+
90
+ instate provides 4 main functions for predicting state and language from Indian last names.
91
+
92
+ ## Electoral Rolls Lookup
93
+
94
+ - **get_state_distribution** - Get P(state|lastname) from 2017 electoral rolls data
95
+
96
+ ```python
97
+ import instate
98
+
99
+ # With list of names
100
+ names = ["sharma", "patel", "singh"]
101
+ result = instate.get_state_distribution(names)
102
+ print(result[["name", "Delhi", "Gujarat", "Punjab"]].head())
103
+
104
+ # With DataFrame
105
+ import pandas as pd
106
+ df = pd.DataFrame({"lastname": ["sharma", "patel"]})
107
+ result = instate.get_state_distribution(df, "lastname")
108
+ print(result.shape) # (2, 33) - 2 names + 31 state columns
109
+ ```
110
+
111
+ - **get_state_languages** - Map states to their official languages
112
+
113
+ ```python
114
+ # Map states to languages
115
+ states = ["Delhi", "Punjab", "Gujarat"]
116
+ result = instate.get_state_languages(states)
117
+ print(result[["state", "official_languages"]])
118
+
119
+ # state official_languages
120
+ # 0 Delhi Hindi, English
121
+ # 1 Punjab Punjabi
122
+ # 2 Gujarat Gujarati
123
+ ```
124
+
125
+ ## Neural Network Predictions
126
+
127
+ - **predict_state** - Predict likely states using trained GRU model
128
+
129
+ ```python
130
+ # Predict top 3 most likely states
131
+ names = ["sharma", "patel", "singh"]
132
+ result = instate.predict_state(names, top_k=3)
133
+ print(result["predicted_states"].iloc[0])
134
+ # ['Delhi', 'Uttar Pradesh', 'Bihar']
135
+ ```
136
+
137
+ - **predict_language** - Predict likely languages using LSTM or k-nearest neighbor
138
+
139
+ ```python
140
+ # LSTM neural network prediction (top 3)
141
+ result = instate.predict_language(names, model="lstm", top_k=3)
142
+ print(result["predicted_languages"].iloc[0])
143
+ # ['hindi', 'punjabi', 'urdu']
144
+
145
+ # K-nearest neighbor lookup (single best)
146
+ result = instate.predict_language(names, model="knn")
147
+ print(result["predicted_languages"].iloc[0])
148
+ # 'hindi'
149
+ ```
150
+
151
+ ## Complete Example
152
+
153
+ ```python
154
+ import pandas as pd
155
+ import instate
156
+
157
+ # Sample data
158
+ df = pd.DataFrame({
159
+ "person_id": [1, 2, 3],
160
+ "lastname": ["sharma", "patel", "singh"]
161
+ })
162
+
163
+ # Get state distributions from electoral rolls
164
+ state_dist = instate.get_state_distribution(df, "lastname")
165
+ print("Electoral rolls data shape:", state_dist.shape)
166
+
167
+ # Predict states with neural network
168
+ predicted_states = instate.predict_state(df, "lastname", top_k=3)
169
+ print("Top 3 predicted states:", predicted_states["predicted_states"].iloc[0])
170
+
171
+ # Predict languages
172
+ predicted_langs = instate.predict_language(df, "lastname", model="lstm", top_k=3)
173
+ print("Top 3 predicted languages:", predicted_langs["predicted_languages"].iloc[0])
174
+
175
+ # Map states to languages
176
+ states_df = pd.DataFrame({"state": ["Delhi", "Gujarat", "Punjab"]})
177
+ lang_map = instate.get_state_languages(states_df)
178
+ print("State language mapping:")
179
+ print(lang_map[["state", "official_languages"]])
180
+ ```
181
+
182
+ # Data
183
+
184
+ The underlying data for the package can be accessed at:
185
+ <https://doi.org/10.7910/DVN/ZXMVTJ>
186
+
187
+ # Evaluation
188
+
189
+ The model has a top-3 accuracy of 85.3% on [unseen
190
+ names](https://github.com/appeler/instate/blob/main/model_training/notebooks/model_dnn_gpu.ipynb).
191
+ The KNN model does quite well. See the details
192
+ [here](https://github.com/appeler/instate/blob/main/model_training/notebooks/KNN_cosine_distance_simple_avg_modal_state.ipynb).
193
+ The name-to-language lookup has an accuracy of 67.9%. The
194
+ name-to-language model prediction has an accuracy of 72.2%.
195
+
196
+ # Authors
197
+
198
+ Atul Dhingra, Gaurav Sood and Rajashekar Chintalapati
199
+
200
+ # Contributor Code of Conduct
201
+
202
+ The project welcomes contributions from everyone! In fact, it depends on
203
+ it. To maintain this welcoming atmosphere, and to collaborate in a fun
204
+ and productive way, we expect contributors to the project to abide by
205
+ the [Contributor Code of
206
+ Conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct/).
207
+
208
+ # License
209
+
210
+ The package is released under the [MIT
211
+ License](https://opensource.org/licenses/MIT).
212
+
213
+ ## 🔗 Adjacent Repositories
214
+
215
+ - [appeler/naampy](https://github.com/appeler/naampy) — Infer Sociodemographic Characteristics from Names Using Indian Electoral Rolls
216
+ - [appeler/ethnicolr2](https://github.com/appeler/ethnicolr2) — Ethnicolr implementation with new models in pytorch
217
+ - [appeler/parsernaam](https://github.com/appeler/parsernaam) — AI name parsing. Predict first or last name using a DL model.
218
+ - [appeler/ethnicolor](https://github.com/appeler/ethnicolor) — Race and Ethnicity based on name using data from census, voter reg. files, etc.
219
+ - [appeler/ethnicolr](https://github.com/appeler/ethnicolr) — Predict Race and Ethnicity Based on the Sequence of Characters in a Name
@@ -0,0 +1,190 @@
1
+ ## instate: predict spoken language and the state of residence from last name
2
+
3
+ [![image](https://github.com/appeler/instate/workflows/test/badge.svg)](https://github.com/appeler/instate/actions?query=workflow%3Atest)
4
+ [![image](https://img.shields.io/pypi/v/instate.svg)](https://pypi.org/project/instate)
5
+ [![Documentation](https://github.com/appeler/instate/actions/workflows/docs.yml/badge.svg)](https://github.com/appeler/instate/actions/workflows/docs.yml)
6
+ [![image](https://static.pepy.tech/badge/instate)](https://pepy.tech/project/instate)
7
+
8
+ Using the Indian electoral rolls data (2017), we provide a Python
9
+ package that takes the last name of a person and gives its distribution
10
+ across states. This package can also predict the spoken language of the
11
+ person based on the last name.
12
+
13
+ # Potential Use Cases
14
+
15
+ India has 22 official languages. To serve such a diverse language base
16
+ is a challenge for businesses and surveyors. To the extent that
17
+ businesses have access to the last name (and no other information) and
18
+ in the absence of other data that allows us to model a person\'s spoken
19
+ language, the distribution of last names across states is the best we
20
+ have.
21
+
22
+ # Dataset
23
+
24
+ Refer to
25
+ [lastname_langs_india.csv.tar.gz](https://github.com/appeler/instate/blob/main/instate/data/lastname_langs_india.csv.tar.gz)
26
+ for the dataset that will be used to predict/lookup the spoken language
27
+ based on the last name.
28
+
29
+ Refer to
30
+ [lastname_langs_india_top3.csv.tar.gz](https://github.com/appeler/instate/blob/main/instate/data/lastname_langs_india_top3.csv.tar.gz)
31
+ for the dataset that will be used to predict the top-3 spoken languages
32
+ based on the last name. A LSTM model has been trained on this dataset to
33
+ predict the top-3 spoken languages.
34
+
35
+ Refer to the
36
+ [notebooks](https://github.com/appeler/instate/tree/main/model_training/notebooks)
37
+ for the notebooks that were used to prepare the above datasets and train the
38
+ models.
39
+
40
+ # Web UI
41
+
42
+ Note: Streamlit app is currently unavailable.
43
+
44
+ # Installation
45
+
46
+ We strongly recommend installing instate inside a Python
47
+ virtual environment (see [venv
48
+ documentation](https://docs.python.org/3/library/venv.html#creating-virtual-environments))
49
+
50
+ pip install instate
51
+
52
+ # Examples
53
+
54
+ from instate import last_state
55
+ last_dat = pd.read_csv("last_dat.csv")
56
+ last_state_dat = last_state(last_dat, "dhingra")
57
+ print(last_state_dat)
58
+
59
+ # API
60
+
61
+ instate provides 4 main functions for predicting state and language from Indian last names.
62
+
63
+ ## Electoral Rolls Lookup
64
+
65
+ - **get_state_distribution** - Get P(state|lastname) from 2017 electoral rolls data
66
+
67
+ ```python
68
+ import instate
69
+
70
+ # With list of names
71
+ names = ["sharma", "patel", "singh"]
72
+ result = instate.get_state_distribution(names)
73
+ print(result[["name", "Delhi", "Gujarat", "Punjab"]].head())
74
+
75
+ # With DataFrame
76
+ import pandas as pd
77
+ df = pd.DataFrame({"lastname": ["sharma", "patel"]})
78
+ result = instate.get_state_distribution(df, "lastname")
79
+ print(result.shape) # (2, 33) - 2 names + 31 state columns
80
+ ```
81
+
82
+ - **get_state_languages** - Map states to their official languages
83
+
84
+ ```python
85
+ # Map states to languages
86
+ states = ["Delhi", "Punjab", "Gujarat"]
87
+ result = instate.get_state_languages(states)
88
+ print(result[["state", "official_languages"]])
89
+
90
+ # state official_languages
91
+ # 0 Delhi Hindi, English
92
+ # 1 Punjab Punjabi
93
+ # 2 Gujarat Gujarati
94
+ ```
95
+
96
+ ## Neural Network Predictions
97
+
98
+ - **predict_state** - Predict likely states using trained GRU model
99
+
100
+ ```python
101
+ # Predict top 3 most likely states
102
+ names = ["sharma", "patel", "singh"]
103
+ result = instate.predict_state(names, top_k=3)
104
+ print(result["predicted_states"].iloc[0])
105
+ # ['Delhi', 'Uttar Pradesh', 'Bihar']
106
+ ```
107
+
108
+ - **predict_language** - Predict likely languages using LSTM or k-nearest neighbor
109
+
110
+ ```python
111
+ # LSTM neural network prediction (top 3)
112
+ result = instate.predict_language(names, model="lstm", top_k=3)
113
+ print(result["predicted_languages"].iloc[0])
114
+ # ['hindi', 'punjabi', 'urdu']
115
+
116
+ # K-nearest neighbor lookup (single best)
117
+ result = instate.predict_language(names, model="knn")
118
+ print(result["predicted_languages"].iloc[0])
119
+ # 'hindi'
120
+ ```
121
+
122
+ ## Complete Example
123
+
124
+ ```python
125
+ import pandas as pd
126
+ import instate
127
+
128
+ # Sample data
129
+ df = pd.DataFrame({
130
+ "person_id": [1, 2, 3],
131
+ "lastname": ["sharma", "patel", "singh"]
132
+ })
133
+
134
+ # Get state distributions from electoral rolls
135
+ state_dist = instate.get_state_distribution(df, "lastname")
136
+ print("Electoral rolls data shape:", state_dist.shape)
137
+
138
+ # Predict states with neural network
139
+ predicted_states = instate.predict_state(df, "lastname", top_k=3)
140
+ print("Top 3 predicted states:", predicted_states["predicted_states"].iloc[0])
141
+
142
+ # Predict languages
143
+ predicted_langs = instate.predict_language(df, "lastname", model="lstm", top_k=3)
144
+ print("Top 3 predicted languages:", predicted_langs["predicted_languages"].iloc[0])
145
+
146
+ # Map states to languages
147
+ states_df = pd.DataFrame({"state": ["Delhi", "Gujarat", "Punjab"]})
148
+ lang_map = instate.get_state_languages(states_df)
149
+ print("State language mapping:")
150
+ print(lang_map[["state", "official_languages"]])
151
+ ```
152
+
153
+ # Data
154
+
155
+ The underlying data for the package can be accessed at:
156
+ <https://doi.org/10.7910/DVN/ZXMVTJ>
157
+
158
+ # Evaluation
159
+
160
+ The model has a top-3 accuracy of 85.3% on [unseen
161
+ names](https://github.com/appeler/instate/blob/main/model_training/notebooks/model_dnn_gpu.ipynb).
162
+ The KNN model does quite well. See the details
163
+ [here](https://github.com/appeler/instate/blob/main/model_training/notebooks/KNN_cosine_distance_simple_avg_modal_state.ipynb).
164
+ The name-to-language lookup has an accuracy of 67.9%. The
165
+ name-to-language model prediction has an accuracy of 72.2%.
166
+
167
+ # Authors
168
+
169
+ Atul Dhingra, Gaurav Sood and Rajashekar Chintalapati
170
+
171
+ # Contributor Code of Conduct
172
+
173
+ The project welcomes contributions from everyone! In fact, it depends on
174
+ it. To maintain this welcoming atmosphere, and to collaborate in a fun
175
+ and productive way, we expect contributors to the project to abide by
176
+ the [Contributor Code of
177
+ Conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct/).
178
+
179
+ # License
180
+
181
+ The package is released under the [MIT
182
+ License](https://opensource.org/licenses/MIT).
183
+
184
+ ## 🔗 Adjacent Repositories
185
+
186
+ - [appeler/naampy](https://github.com/appeler/naampy) — Infer Sociodemographic Characteristics from Names Using Indian Electoral Rolls
187
+ - [appeler/ethnicolr2](https://github.com/appeler/ethnicolr2) — Ethnicolr implementation with new models in pytorch
188
+ - [appeler/parsernaam](https://github.com/appeler/parsernaam) — AI name parsing. Predict first or last name using a DL model.
189
+ - [appeler/ethnicolor](https://github.com/appeler/ethnicolor) — Race and Ethnicity based on name using data from census, voter reg. files, etc.
190
+ - [appeler/ethnicolr](https://github.com/appeler/ethnicolr) — Predict Race and Ethnicity Based on the Sequence of Characters in a Name
@@ -0,0 +1,35 @@
1
+ """
2
+ instate: Predict state and language from Indian lastnames.
3
+
4
+ This package provides functions to:
5
+ 1. Look up state distributions from 2017 Indian electoral rolls
6
+ 2. Predict states and languages using neural networks
7
+
8
+ Main functions:
9
+ - get_state_distribution: Get P(state|lastname) from electoral rolls
10
+ - get_state_languages: Map states to official languages
11
+ - predict_state: Neural prediction of most likely states
12
+ - predict_language: Neural prediction of most likely languages
13
+ """
14
+
15
+ from .electoral import (
16
+ get_state_distribution,
17
+ get_state_languages,
18
+ list_available_states,
19
+ )
20
+ from .predict import predict_language, predict_state
21
+
22
+ __all__ = [
23
+ "get_state_distribution",
24
+ "get_state_languages",
25
+ "predict_state",
26
+ "predict_language",
27
+ "list_available_states",
28
+ ]
29
+
30
+ try:
31
+ from importlib.metadata import version
32
+
33
+ __version__ = version("instate")
34
+ except ImportError:
35
+ __version__ = "1.0.0"