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.
I have been hired by a pretend 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.
I have 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). |
# Importation of libraries to play with
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load dataset
ds_jobs = pd.read_csv("customer_train.csv")
# Create copy to transform
df = ds_jobs.copy()# Preview dataset
print(df.shape)
print(df.describe())
print(df.info())
print(df.head())# Preview first 5 rows of the dataframe
df.head()# Replace null values
df['major_discipline'] = df['major_discipline'].fillna('No Major')
df['company_type'] = df['company_type'].fillna('Other')
df['company_size'] = df['company_size'].fillna('0')# Preiew gender values
print(df['gender'].value_counts())
print(df['gender'].isna().sum())# Fill missing values
df['gender'].fillna(method='ffill', inplace=True)
# View Results
print(df['gender'].value_counts())
print(df['gender'].isna().sum())# Preview count and missing values
print(df['last_new_job'].value_counts())
print(df['last_new_job'].isna().sum())# Fill null values
df['last_new_job'] = df['last_new_job'].fillna('never')
# Convert values to integers, specify categories, convert to Pandas categorical data type
df['last_new_job'] = df['last_new_job'].map({'never': 0, '1': 1, '2': 2, '3': 3, '4': 4, '>4': 5})
last = pd.CategoricalDtype([0, 1, 2, 3, 4, 5], ordered=True)
df['last_new_job'] = df['last_new_job'].astype(last)
# View Results
print(df['last_new_job'].value_counts())
print(df['last_new_job'].isna().sum())# Preview education level values
print(df['education_level'].value_counts())
print(df['education_level'].isna().sum())# Fill null values
df['education_level'] = df['education_level'].fillna('Primary School')
# Specify categories and convert to Pandas categorical data type
level = pd.CategoricalDtype(['Primary School', 'High School', 'Graduate', 'Masters', 'Phd'], ordered=True)
df['education_level'] = df['education_level'].astype(level)
# View Results
print(df['education_level'].value_counts())
print(df['education_level'].isna().sum())# Preview enrolled university values
print(df['enrolled_university'].value_counts())
print(df['enrolled_university'].isna().sum())# Fill null values
df['enrolled_university'] = df['enrolled_university'].fillna('no_enrollment')
# Specify categories and convert to Pandas categorical data type
study = pd.CategoricalDtype(['no_enrollment', 'Part time course', 'Full time course'], ordered=True)
df['enrolled_university'] = df['enrolled_university'].astype(study)
# View Results
print(df['enrolled_university'].value_counts())
print(df['enrolled_university'].isna().sum())# Transform data types to save memory storage
df['relevant_experience'] = df['relevant_experience'].map\
({'Has relevant experience': True, 'No relevant experience': False})
df['job_change'] = df['job_change'].map({1: True, 0: False})
# Float and integer data types
df['city_development_index'] = df['city_development_index'].astype('float16')
df[['student_id', 'training_hours']] = df[['student_id', 'training_hours']].astype('int32')
# Categorical data type
df[['city', 'gender', 'major_discipline', 'company_type']] =\
df[['city', 'gender', 'major_discipline', 'company_type']].astype('category')
# Remove punctuation to convert string to integer
df['experience'] = df['experience'].str.replace('[^\w\s]', '', regex=True)
df['experience'] = df['experience'].fillna(0)
df['experience'] = df['experience'].astype('int32')# View DataFrame information
df.info()