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 ). |
# Start your code here!
import pandas as pd
import numpy as np
# Import the dataset
dfcustomers = pd.read_csv("customer_train.csv")
# Checking the data types
print(dfcustomers.dtypes)
display(dfcustomers.head())
Once imported, let's filter the dataset to get only the students with at least 10 years of experience at companies with at least 1000 employees.
# Exploring the values in the column "experience"
print(dfcustomers["experience"].value_counts(dropna=False))
# Selecting only the values 10+ ensuring that the value "1" is not included
filter_experience = dfcustomers["experience"].str.contains("^1.|20")
# printing some values to check if the filter works correctly
print(list(zip(dfcustomers["experience"], filter_experience))[0:15])
# Exploring the values in the column "company_size"
print(dfcustomers["company_size"].value_counts(dropna=False))
# Selecting only the values 1000+ employees
filter_employees = dfcustomers["company_size"].str.contains("000")
# printing some values to check if the filter works correctly
print(list(zip(dfcustomers["company_size"], filter_employees))[0:15])
# Filtering the dataset considering the two conditions
ds_jobs = dfcustomers[filter_experience & filter_employees]
# Checking dimensions
print(dfcustomers.shape)
print(ds_jobs.shape)
Once filtered the dataset, I'll select the different target data types to convert them into the desired data types.
# Converting the integers into int32
col_integers = ds_jobs.select_dtypes("int64").astype("int32")
print(col_integers.dtypes)
# Converting the float-typed columns into float16
col_float = ds_jobs.select_dtypes("float64").astype("float16")
print(col_float.dtypes)
For the object-typed columns, I'll check first for the unique values and the values frequency.
# Checking the unique values for each object-typed column
display(ds_jobs.select_dtypes("object").describe())
# Converting all object columns into category
col_objects = ds_jobs.select_dtypes("object").astype("category")
print(col_objects.dtypes)
# Checking for the categories in each column
for i in col_objects.columns:
print(col_objects[i].cat.categories)
Now, we'll select the variables that required to be set as ordinal such as relevant_experience
, enrolled_university
, education_level
, experience
, company_size
and last_new_job
.