<jupyter_start><jupyter_text>Let's try second approach with oversampling.<jupyter_code>df_tr = pd.read_csv("H:/Learnings/Data Science/Hackathon/HR analytics/train_jqd04QH.csv")
df_te =  pd.read_csv("H:/Learnings/Data Science/Hackathon/HR analytics/test_KaymcHn.csv")
df_tr.info()
df_tr.head()
#Copying the dataframes
df_train = df_tr.copy()
df_test = df_te.copy()
class DataFrameImputer(TransformerMixin):

    def __init__(self):
        """Impute missing values.

        Columns of dtype object are imputed with the most frequent value 
        in column.

        Columns of other types are imputed with mean of column.

        """
    def fit(self, X, y=None):

        self.fill = pd.Series([X[c].value_counts().index[0]
            if X[c].dtype == np.dtype('O') else X[c].mean() for c in X],
            index=X.columns)

        return self

    def transform(self, X, y=None):
        return X.fillna(self.fill)
df_train.isnull().sum()
df_test.isnull().sum()
df_train['experience'].replace({'<1':'1','>20':'20'},inplace=True)
df_test['experience'].replace({'<1':'1','>20':'20'},inplace=True)
df_train['company_size'].replace({'<10':'very small','10/49':'very small','50-99':'very small',
                                  '100-500':'small','500-999':'small',
                                  '1000-4999':'medium',
                                  '5000-9999':'large','10000+':'large'},inplace=True)
df_test['company_size'].replace({'<10':'very small','10/49':'very small','50-99':'very small',
                                  '100-500':'small','500-999':'small',
                                  '1000-4999':'medium',
                                  '5000-9999':'large','10000+':'large'},inplace=True)
df_train['last_new_job'].replace({'>4':'5','never': '0'},inplace=True)
df_test['last_new_job'].replace({'>4':'5','never': '0'},inplace=True)
#Imputing the Null values in Train and Test dataset
df_train['gender'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['gender']))
df_test['gender'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['gender']))
df_train['enrolled_university'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['enrolled_university']))
df_test['enrolled_university'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['enrolled_university']))
df_train['education_level'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['education_level']))
df_test['education_level'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['education_level']))
df_train['major_discipline'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['major_discipline']))
df_test['major_discipline'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['major_discipline']))
df_train['experience'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['experience']))
df_test['experience'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['experience']))
df_train['company_size'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['company_size']))
df_test['company_size'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['company_size']))
df_train['last_new_job'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['last_new_job']))
df_test['last_new_job'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['last_new_job']))
df_train['company_type'] = DataFrameImputer().fit_transform(pd.DataFrame(df_train['company_type']))
df_test['company_type'] = DataFrameImputer().fit_transform(pd.DataFrame(df_test['company_type']))
df_test.isnull().sum()
df_train.isnull().sum()
#changing the dtypes of the featues
df_train['experience'] = df_train['experience'].astype('int')
df_test['experience'] = df_test['experience'].astype('int')
df_train['last_new_job'] = df_train['last_new_job'].astype('int')
df_test['last_new_job'] = df_test['last_new_job'].astype('int')
df_train.info()
df_train.drop(['city'],1,inplace=True)
df_test.drop(['city'],1,inplace=True)
df_train = pd.get_dummies(data=df_train,drop_first=True)
df_test = pd.get_dummies(data=df_test,drop_first=True)
df_train.info()
df_train.drop(['enrollee_id'],1,inplace=True)
#oversampling the data
X=df_train.drop(columns=["target"])
y=df_train["target"]
oversample = SMOTE()
X_smote, y_smote = oversample.fit_resample(X, y)
X_train, X_test, y_train, y_test = train_test_split(X_smote, y_smote, test_size=0.2, random_state=123)
# prepare models
models = []
models.append(('LR', LogisticRegression()))
models.append(('ADB', AdaBoostClassifier()))
models.append(('GB', GradientBoostingClassifier()))
models.append(('CAT', CatBoostClassifier()))
models.append(('DT', DecisionTreeClassifier()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('NB', GaussianNB()))

# evaluate each model in turn
results = []
names = []
scoring = 'accuracy'
for name, model in models:
    model.fit(X_train, y_train)
    y_test_pred_new=model.predict_proba(X_test)[:,1]
    ac=metrics.roc_auc_score(y_test,y_test_pred_new)   
    results.append(ac)
    names.append(name)
    msg = "%s: %f " % (name, ac)
    print(msg)
df_test.info()
cb  = CatBoostClassifier()
cb.fit(X_smote, y_smote)
#predicting for test set
x_test = df_test.drop(columns=["enrollee_id"])
y_pred = cb.predict(x_test)
y_pred = y_pred.reshape(15021,1)
df_test['target'] = y_pred
df_test['target'] = df_test['target'].astype(int)
submit = df_test[["enrollee_id","target"]].copy()
submit.to_csv('H:/Learnings/Data Science/Hackathon/HR analytics/sample_submission_2.csv', index=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup