Overview
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 Libraries and Load Dataset
# Import libraries
import pandas as pd
import numpy as np
ds_jobs = pd.read_csv("customer_train.csv")
ds_jobs.sample(20)
Data Cleaning & Preparation
# Copy the DataFrame for cleaning & preparation process
ds_jobs_clean = ds_jobs.copy(deep=True)
print(ds_jobs.info())
# Store non-numeric columns
numeric_columns = [column for column in ds_jobs.columns if pd.api.types.is_numeric_dtype(ds_jobs[column])]
# Iterate over numeric columns and print their maximum value
for column in numeric_columns:
print(f'Maximum value for {column} column:')
print(ds_jobs[column].max())
print('----------------')
From the information above, we can observe that there are 4 numeric columns, student_id
, city_development_index
, training_hours
, and job_change
. city_development_index
column contains floating point with less than 4 digit precisions, and student_id
column contains less than 6 digits. So using float64 and int64 would be overkill 🙂
For efficiency, we will convert integer columns into int32 and float columns into float16! We will also inspect the rest of the non-numeric columns to determine whether they are ordinal data or standard categorical data 😄
# Store non-numeric columns
non_numeric_columns = [column for column in ds_jobs.columns if not pd.api.types.is_numeric_dtype(ds_jobs[column])]
# Iterate over non-numeric columns and print their unique values
for column in non_numeric_columns:
print(f'Column: {column}')
print(ds_jobs[column].unique())
print('----------------')
After inspecting each column, we can conclude that city
, gender
, major_discipline
, and company_type
columns are just standard categorical data. Let's order the rest of the columns based on their hierarchy.
# Create a dictionary of columns containing ordinal data
ordinal_data = {
'relevant_experience': ['No relevant experience', 'Has relevant experience'],
'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', '1', '2', '3', '4', '>4']
}
# Loop through columns to change data types
for col in ds_jobs_clean:
# Convert int columns to int32
if ds_jobs_clean[col].dtype == np.int64:
ds_jobs_clean[col] = ds_jobs_clean[col].astype('int32')
# Convert float columns to float16
elif ds_jobs_clean[col].dtype == 'float':
ds_jobs_clean[col] = ds_jobs_clean[col].astype('float16')
# Convert columns containing ordinal data using dict
elif col in ordinal_data.keys():
category = pd.CategoricalDtype(ordinal_data[col], ordered=True)
ds_jobs_clean[col] = ds_jobs_clean[col].astype(category)
# Convert the rest of columns to vanilla categories
else:
ds_jobs_clean[col] = ds_jobs_clean[col].astype('category')
ds_jobs_converted = ds_jobs_clean.copy(deep=True #copy to in
Okay we're almost done! Let's ace this project by doing the last instruction. We need to filter the DataFrame to only contain students with 10 or more years of experience at companies with at least 1000 employees, as their recruiter base is suited to more experienced professionals at enterprise companies.
# Filter students with 10 or more years experience at companies with at least 1000 employees
ds_jobs_clean = ds_jobs_clean[(ds_jobs_clean['experience'] >= '10') & (ds_jobs_clean['company_size'] >= '1000-4999')]
ds_jobs_clean
‌
‌