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. |
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 pandas as pd
# Load the dataset
ds_jobs = pd.read_csv('customer_train.csv')
ds_jobs.info()
ds_jobs.experience.unique()
ds_jobs['job_change'].unique()
# Filter the dataset
ds_jobs_clean = ds_jobs[(ds_jobs['experience'].isin(['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '>20'])) & (ds_jobs['company_size'].isin(['1000-4999', '5000-9999', '10000+']))]
# Convert columns to the specified data types
ds_jobs_clean = ds_jobs_clean.astype({
'student_id': 'int32',
'city': 'category',
'city_development_index': 'float16',
'gender': 'category',
'relevant_experience': 'category',
'enrolled_university': 'category',
'education_level': 'category',
'major_discipline': 'category',
'experience': 'category',
'company_size': 'category',
'company_type': 'category',
'last_new_job': 'category',
'training_hours': 'int32',
'job_change': 'int32'
})
# Set the order of ordinal categorical data
ordinal_categories_edu = ['Primary School', 'High School', 'Graduate', 'Masters', 'Phd']
ordinal_categories_comp = ['<10','10-49', '50-99', '100-499', '500-999', '1000-4999', '5000-9999', '10000+']
ordinal_categories_rel_exp = ['No relevant experience', 'Has relevant experience']
ordinal_categories_enr_uni = ['no_enrollment', 'Part time course', 'Full time course']
ordinal_categories_exp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
ordinal_categories_last_new = ['never', '1', '2', '3','4', '>4']
# Apply the ordering
ds_jobs_clean['education_level'] = pd.Categorical(ds_jobs_clean['education_level'], categories=ordinal_categories_edu, ordered=True)
ds_jobs_clean['company_size'] = pd.Categorical(ds_jobs_clean['company_size'], categories=ordinal_categories_comp, ordered=True)
ds_jobs_clean['relevant_experience'] = pd.Categorical(ds_jobs_clean['relevant_experience'], categories=ordinal_categories_rel_exp, ordered=True)
ds_jobs_clean['enrolled_university'] = pd.Categorical(ds_jobs_clean['enrolled_university'], categories=ordinal_categories_enr_uni, ordered=True)
ds_jobs_clean['experience'] = pd.Categorical(ds_jobs_clean['experience'], categories=ordinal_categories_exp, ordered=True)
ds_jobs_clean['last_new_job'] = pd.Categorical(ds_jobs_clean['last_new_job'], categories=ordinal_categories_last_new, ordered=True)
# Ensure the columns are in the same order as the original dataset
ds_jobs_clean = ds_jobs_clean[ds_jobs.columns]
# Print the first 5 rows of the cleaned dataset
print(ds_jobs_clean.head())
ds_jobs.info()
ds_jobs_clean.info()