-
-
Notifications
You must be signed in to change notification settings - Fork 167
Description
Hello!
I found an AI-Specific Code smell in your project.
The smell is called: Columns and DataType Not Explicitly Set
You can find more information about it in this paper: https://dl.acm.org/doi/abs/10.1145/3522664.3528620.
According to the paper, the smell is described as follows:
| Problem | If the columns are not selected explicitly, it is not easy for developers to know what to expect in the downstream data schema. If the datatype is not set explicitly, it may silently continue the next step even though the input is unexpected, which may cause errors later. The same applies to other data importing scenarios. |
|---|---|
| Solution | It is recommended to set the columns and DataType explicitly in data processing. |
| Impact | Readability |
Example:
### Pandas Column Selection
import pandas as pd
df = pd.read_csv('data.csv')
+ df = df[['col1', 'col2', 'col3']]
### Pandas Set DataType
import pandas as pd
- df = pd.read_csv('data.csv')
+ df = pd.read_csv('data.csv', dtype={'col1': 'str', 'col2': 'int', 'col3': 'float'})
You can find the code related to this smell in this link:
Kaggler/tests/preprocessing/test_ohe.py
Lines 3 to 23 in 510e70b
| import pandas as pd | |
| from kaggler.preprocessing import OneHotEncoder | |
| N_OBS = int(1e6) | |
| N_FEATURE = 10 | |
| N_CATEGORY = 1000 | |
| def test(): | |
| df = pd.DataFrame( | |
| np.random.randint(0, N_CATEGORY, size=(N_OBS, N_FEATURE)), | |
| columns=["c{}".format(x) for x in range(N_FEATURE)], | |
| ) | |
| profiler = cProfile.Profile(subcalls=True, builtins=True, timeunit=0.001) | |
| ohe = OneHotEncoder(min_obs=100) | |
| profiler.enable() | |
| ohe.fit(df) | |
| X_new = ohe.transform(df) | |
| profiler.disable() | |
| profiler.print_stats() |
I also found instances of this smell in other files, such as:
File: https://github.com/jeongyoonlee/Kaggler/blob/master/kaggler/metrics/plot.py#L84-L94 Line: 89
File: https://github.com/jeongyoonlee/Kaggler/blob/master/kaggler/metrics/plot.py#L87-L97 Line: 92
File: https://github.com/jeongyoonlee/Kaggler/blob/master/kaggler/metrics/plot.py#L98-L108 Line: 103
File: https://github.com/jeongyoonlee/Kaggler/blob/master/kaggler/model/automl.py#L205-L215 Line: 210
File: https://github.com/jeongyoonlee/Kaggler/blob/master/kaggler/preprocessing/categorical.py#L303-L313 Line: 308
.
I hope this information is helpful!