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")

# View the dataset
#ds_jobs.head
# Create a copy of ds_jobs for transforming
ds_jobs_transformed = ds_jobs.copy()

#Dropping the Null experience values
ds_jobs_transformed.dropna(subset=["experience"],inplace=True)

#Making a subset by choosing data with expereince >10 and company size >1000
ds_jobs_transformed = ds_jobs_transformed[ds_jobs_transformed["experience"].isin(["10","11","12","13","14","15","16","17","18","19","20",">20"])]
ds_jobs_transformed = ds_jobs_transformed[ds_jobs_transformed["company_size"].isin(["1000-4999","5000-9999","10000+"])]

#Marking both experience and company size as ordered category and defining the category as well
ds_jobs_transformed["company_size"] = pd.Categorical(ds_jobs_transformed["company_size"],categories=["1000-4999","5000-9999","10000+"],ordered = True)
ds_jobs_transformed["experience"] = pd.Categorical(ds_jobs_transformed["experience"],categories=["10","11","12","13","14","15","16","17","18","19","20",">20"],ordered = True)

#As relevant expereince has only 2 values converting those values to True/False and making Dtype as Boolean

ds_jobs_transformed["relevant_experience"] = np.where(ds_jobs_transformed["relevant_experience"]=="Has relevant experience",True,False)
ds_jobs_transformed["relevant_experience"] = ds_jobs_transformed["relevant_experience"].astype("bool")

#Filling Blank gender values with NA and marking the Dtype of gender as category
ds_jobs_transformed["gender"].fillna("NA",inplace= True)
ds_jobs_transformed["gender"] = ds_jobs_transformed["gender"].astype("category")

#Filling blank enrollled university values with no_enrollment and marking it as ordered category
ds_jobs_transformed["enrolled_university"].fillna("no_enrollment",inplace= True)
ds_jobs_transformed["enrolled_university"] = pd.Categorical(ds_jobs_transformed["enrolled_university"],categories=["no_enrollment","Part time course","Full time course"],ordered = True)

#Fillin blank education level values with NA and converting the Dtype as ordered category
ds_jobs_transformed["education_level"].fillna("NA",inplace= True)
ds_jobs_transformed["education_level"] = pd.Categorical(ds_jobs_transformed["education_level"],categories=["NA","Primary School","High School","Graduate","Masters","Phd"],ordered = True)


#Filling Blank major discipline value as No Major and marking it as Category dtype
ds_jobs_transformed["major_discipline"].fillna("No Major",inplace= True)
ds_jobs_transformed["major_discipline"] = ds_jobs_transformed["major_discipline"].astype("category")

#Filling Blank company type as Other and marking it's Dtype as category
ds_jobs_transformed["company_type"].fillna("Other",inplace= True)
ds_jobs_transformed["company_type"] = ds_jobs_transformed["company_type"].astype("category")

#Filling Blank values with NA and marking the Dtype as Ordered category
ds_jobs_transformed["last_new_job"].fillna("NA",inplace= True)
ds_jobs_transformed["last_new_job"] = pd.Categorical(ds_jobs_transformed["last_new_job"],categories=["NA","never","1","2","3","4",">4"],ordered = True)


#Marking job change as Boolean and assigning its existing values to Boolean values
ds_jobs_transformed["job_change"] = ds_jobs_transformed["job_change"].map({0:False,1:True})
ds_jobs_transformed["job_change"] = ds_jobs_transformed["job_change"].astype("bool")

#Converting the data types of existing int64 and float 64 variables ot int32 and float16 respectively
ds_jobs_transformed["student_id"] = ds_jobs_transformed["student_id"].astype("int32")
ds_jobs_transformed["training_hours"] = ds_jobs_transformed["training_hours"].astype("int32")
ds_jobs_transformed["city_development_index"] = ds_jobs_transformed["city_development_index"].astype("float16")
ds_jobs_transformed["city"] = ds_jobs_transformed["city"].astype("category")


#Finally checking if there are any changes in the memory size
ds_jobs_transformed.info()
ds_jobs.info()