Created
August 1, 2020 13:53
-
-
Save arunm8489/327590c56e949a5e42b037bf2ac39b45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# label encoding categorical features | |
def label_encoding(col): | |
label_encoder = LabelEncoder() | |
train_cols = label_encoder.fit_transform(X_train[col]) | |
X_test[col] = X_test[col].apply(lambda x: 'unknown' if x not in label_encoder.classes_ else x) | |
label_encoder.classes_ = np.append(label_encoder.classes_, 'unknown') | |
test_cols = label_encoder.fit_transform(X_test[col]) | |
return train_cols, test_cols | |
lb_train_school_state, lb_test_school_state = label_encoding("school_state") | |
lb_train_category,lb_test_category = label_encoding('project_subject_categories') | |
lb_train_grade_category,lb_test_grade_category = label_encoding('project_grade_category') | |
lb_train_teacher_prefix,lb_test_teacher_prefix = label_encoding('teacher_prefix') | |
lb_train_sub_category,lb_test_sub_category = label_encoding("project_subject_subcategories") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment