A common problem when creating models to generate business value from data is that the datasets can be so large that it can take days for the model to generate predictions. Ensuring that your dataset is stored as efficiently as possible is crucial for allowing these models to run on a more reasonable timescale without having to reduce the size of the dataset.
You've been hired by a major online data science training provider called Training Data Ltd. to clean up one of their largest customer datasets. This dataset will eventually be used to predict whether their students are looking for a new job or not, information that they will then use to direct them to prospective recruiters.
You've been given access to customer_train.csv, which is a subset of their entire customer dataset, so you can create a proof-of-concept of a much more efficient storage solution. The dataset contains anonymized student information, and whether they were looking for a new job or not during training:
| Column | Description |
|---|---|
student_id | A unique ID for each student. |
city | A code for the city the student lives in. |
city_development_index | A scaled development index for the city. |
gender | The student's gender. |
relevant_experience | An indicator of the student's work relevant experience. |
enrolled_university | The type of university course enrolled in (if any). |
education_level | The student's education level. |
major_discipline | The educational discipline of the student. |
experience | The student's total work experience (in years). |
company_size | The number of employees at the student's current employer. |
company_type | The type of company employing the student. |
last_new_job | The number of years between the student's current and previous jobs. |
training_hours | The number of hours of training completed. |
job_change | An indicator of whether the student is looking for a new job (1) or not (0). |
# Import necessary libraries
import pandas as pd
# Load the dataset
ds_jobs = pd.read_csv("customer_train.csv")
# View the dataset
ds_jobs.head()# Create a copy of ds_jobs for transforming
ds_jobs_transformed = ds_jobs.copy()ds_jobs_transformed.info()# search for categories in numerical columns
for col in ds_jobs_transformed.select_dtypes('number').columns:
print(f'{col}: ', ds_jobs_transformed[col].nunique())
# check type of column to be nominal or ordinal category
for col in ds_jobs_transformed.select_dtypes('object').columns:
print('-' * 15)
print(ds_jobs_transformed[col].value_counts())As we can see, we will have:
- 2 integer columns
- 1 float columns
- 2 bool columns (binary categories)
- 5 ordinal category columns
- 4 nominal category columns
# columns that have only 2 values
binary_categories = {
'job_change': {1.0: True, 0.0: False},
'relevant_experience': {"Has relevant experience": True, "No relevant experience": False}
}
# columns that have integer and float values
integer_columns = ['student_id', 'training_hours']
float_columns = ['city_development_index']
# columns that have ordinal categories
ordinal_categories = {
"enrolled_university": ["no_enrollment", "Part time course", "Full time course"],
"education_level": ["Primary School", "High School", "Graduate", "Masters", "Phd"],
"experience": ['<1'] + list(map(str, range(1, 21))) + ['>20'],
"company_size": ["<10", "10-49", "50-99", "100-499", "500-999", "1000-4999", "5000-9999", "10000+"],
"last_new_job": ['never'] + list(map(str, range(1, 4))) + ['>4']
}for col in ds_jobs_transformed.columns:
# columns that have only 2 values
if col in binary_categories:
ds_jobs_transformed[col] = ds_jobs_transformed[col].map(binary_categories[col])
# columns that have integer and float values
elif col in integer_columns:
ds_jobs_transformed[col] = ds_jobs_transformed[col].astype('int32')
elif col in float_columns:
ds_jobs_transformed[col] = ds_jobs_transformed[col].astype('float16')
# columns that have ordinal categories
elif col in ordinal_categories:
category = pd.CategoricalDtype(categories=ordinal_categories[col], ordered=True)
ds_jobs_transformed[col] = ds_jobs_transformed[col].astype(category)
# columns that have nominal category
else:
ds_jobs_transformed[col] = ds_jobs_transformed[col].astype('category')# For submission, we need to filter the DataFrame to include only rows where the 'experience' column is greater than or equal to '10'
# and the 'company_size' column is greater than '500-999'
ds_jobs_transformed = ds_jobs_transformed.loc[
(ds_jobs_transformed['experience'] >= '10') & # Filter for experience of 10 years and more
(ds_jobs_transformed['company_size'] > "500-999") # Filter for company size greater than 500-999
]