Lasso Regression for Job Application Use Case
A Lasso regression analysis was conducted for the use case of job application. Total 14 variables were considered as predictor variables. Since the data set is small, having more variables that do not add much value to the model might overfit the model. Hence, using lasso, we will be able to eliminate the variables that marginally impact the dependent variable. Independent variables considered for this case study are Age, SSC marks, HSC marks, Graduation grade, post graduation grade, skills in python/ sql and excel, work experience, current ctc and job location - Mumbai, Bangalore and Delhi. The target variable is to predict whether the candidate will be selected or not.
Since the scale of independent variables are different from one another, it is important to standardize all the variables with the mean =0 and standard deviation =1. Hence, the standardization acitivity is performed using preprocessing.scale module of sklearn library. Data is split into training and test set in the ratio of 70:30. The value pf folds i.e cv is considered as 5 while building the model on the training set. The mean square error is plotted at each step to identify the best set of predictor variables.
From the regression coefficients of the predictor variables, it can be observed that age is highly negatively correlated with the job selection variable where as python skill is positively correlated with the y variable. Also, variables such as SQL skill, Delhi and Bangalore job location were eliminated from the final model as their regression coefficients are penalized to 0. The 11 variables accounted for the r square value of 66% variance in the job selection variable.
Below is the code used for executing this analysis.
##Importing the libraries
import pandas as pd import numpy as np import matplotlib.pylab as plt from sklearn.model_selection import train_test_split from sklearn.linear_model import LassoLarsCV
##Loading the dataset data = pd.read_csv('Job_Application.csv')
##Selecting predictors and target predictors = data[['Age', 'SSC', 'HSC', 'Grad', 'PG', 'Python', 'SQL', 'Excel', 'Work_ex', 'Current_CTC', 'Mumbai', 'Bangalore', 'Delhi', 'Notice_period']]
target = data.Selected
##Standardizing variables to have mean=0 and standard deviation =1
predictors_1 = predictors.copy()
from sklearn import preprocessing predictors_1['Age'] = preprocessing.scale(predictors_1['Age'].astype('float64')) predictors_1['SSC'] = preprocessing.scale(predictors_1['SSC'].astype('float64')) predictors_1['HSC'] = preprocessing.scale(predictors_1['HSC'].astype('float64')) predictors_1['Grad'] = preprocessing.scale(predictors_1['Grad'].astype('float64')) predictors_1['PG'] = preprocessing.scale(predictors_1['PG'].astype('float64')) predictors_1['Python'] = preprocessing.scale(predictors_1['Python'].astype('float64')) predictors_1['SQL'] = preprocessing.scale(predictors_1['SQL'].astype('float64')) predictors_1['Excel'] = preprocessing.scale(predictors_1['Excel'].astype('float64')) predictors_1['Work_ex'] = preprocessing.scale(predictors_1['Work_ex'].astype('float64')) predictors_1['Current_CTC'] = preprocessing.scale(predictors_1['Current_CTC'].astype('float64')) predictors_1['Mumbai'] = preprocessing.scale(predictors_1['Mumbai'].astype('float64')) predictors_1['Bangalore'] = preprocessing.scale(predictors_1['Bangalore'].astype('float64')) predictors_1['Delhi'] = preprocessing.scale(predictors_1['Delhi'].astype('float64')) predictors_1['Notice_period'] = preprocessing.scale(predictors_1['Notice_period'].astype('float64'))
##Splitting the data into train and test set pred_train,pred_test,tar_train,tar_test = train_test_split(predictors_1,target,test_size=0.3,random_state=123)
##Model building using Lasso model = LassoLarsCV(cv=5,precompute=False).fit(pred_train,tar_train)
##Depicting coefficients of the predictor variables dict(zip(predictors_1.columns,model.coef_))
##Plotting regression coefficients m_log_alphas = -np.log10(model.alphas_) ax = plt.gca() plt.plot(m_log_alphas, model.coef_path_.T) plt.axvline(-np.log10(model.alpha_), linestyle='--', color='k', label='alpha CV') plt.ylabel('Regression Coefficients') plt.xlabel('-log(alpha)') plt.title('Regression Coefficients Progression for Lasso Paths')
m_log_alphascv = -np.log10(model.cv_alphas_) plt.figure() plt.plot(m_log_alphascv, model.mse_path_, ':') plt.plot(m_log_alphascv, model.mse_path_.mean(axis=-1), 'k', label='Average across the folds', linewidth=2) plt.axvline(-np.log10(model.alpha_), linestyle='--', color='k', label='alpha CV') plt.legend() plt.xlabel('-log(alpha)') plt.ylabel('Mean squared error') plt.title('Mean squared error on each fold')
from sklearn.metrics import mean_squared_error train_error = mean_squared_error(tar_train, model.predict(pred_train)) test_error = mean_squared_error(tar_test, model.predict(pred_test)) print ('training data MSE') print(train_error) print ('test data MSE') print(test_error)
rsquared_train=model.score(pred_train,tar_train) rsquared_test=model.score(pred_test,tar_test) print ('training data R-square') print(rsquared_train) print ('test data R-square') print(rsquared_test)










