Skip to content

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:

ColumnDescription
student_idA unique ID for each student.
cityA code for the city the student lives in.
city_development_indexA scaled development index for the city.
genderThe student's gender.
relevant_experienceAn indicator of the student's work relevant experience.
enrolled_universityThe type of university course enrolled in (if any).
education_levelThe student's education level.
major_disciplineThe educational discipline of the student.
experienceThe student's total work experience (in years).
company_sizeThe number of employees at the student's current employer.
company_typeThe type of company employing the student.
last_new_jobThe number of years between the student's current and previous jobs.
training_hoursThe number of hours of training completed.
job_changeAn indicator of whether the student is looking for a new job (1) or not (0).
# Import necessary libraries
import pandas as pd
import numpy as np

# Load the dataset
ds_jobs = pd.read_csv("customer_train.csv")
ds_jobs
# Create a copy of ds_jobs for transforming
df = ds_jobs.copy()

# take a look at the dataframe info
df.info()
# take a look at the memory each column use
df.memory_usage()
# EDA to help identify ordinal, nominal, and two-factor categories
for col in ds_jobs.select_dtypes("object").columns:
    print(df[col].value_counts(), '\n')
# check for columns that have only two factors
for x in df.columns:
    print(f"number of unique values in column {x} is: {df[x].nunique()}")
    
# columns ["relevant_experience", "job_change"]
rel_ex = {"Has relevant experience" : True, "No relevant experience" : False}
df["relevant_experience"] = df["relevant_experience"].replace(rel_ex) 

job_change = {1.0: True, 0.0: False}
df["job_change"] = df["job_change"].replace(job_change)

df[["relevant_experience", "job_change"]].head()
# check for columns containing integers only
for x in df.columns:
    print(f"the datatype foe the column {x} is: {df[x].dtype}")

# integer ["student_id", "training_hours"]
# float ["city_development_index"]
# nominal categorical (without order) ["city", "gender", "major_discipline", "company_type"]
# ordinal categorical data (with order) ["education_level", "experience", "company_size", "last_new_job"]
# integer ["student_id", "training_hours"]

df["student_id"] = df["student_id"].astype(np.int32)

df["training_hours"] = df["training_hours"].astype(np.int32)

print(df["student_id"].dtype, df["training_hours"].dtype)
# float ["city_development_index"]

df["city_development_index"] = df["city_development_index"].astype(np.float16)

print(df["city_development_index"].dtype)
# Columns containing nominal categorical data (means that does not have an order)
# nominal categorical (without order) ["city", "gender", "enrolled_university", "major_discipline", "company_type"]

nominal_cols = ["city", "gender", "major_discipline", "company_type"]
for col in nominal_cols: 
    df[col] = df[col].astype("category")
    print(f"the column {col} datatype is: {df[col].dtype}")
# Create a dictionary of columns containing ordered categorical data
ordered_cats = {
    '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']
}

for col in ordered_cats.keys():
    category = pd.CategoricalDtype(ordered_cats[col], ordered=True)
    df[col] = df[col].astype(category)
df = df[(df["experience"] >= "10") & (df["company_size"] >= "1000-4999")]
ds_jobs_transformed = df
ds_jobs_transformed.head()
ds_jobs_transformed.info()
ds_jobs_transformed.memory_usage()