<jupyter_start><jupyter_text>Encoding Categorical Features<jupyter_code># person_home_ownership
pers_h_own_label=LabelEncoder()
pers_h_own_label.fit(df_raw["person_home_ownership"])
df_raw["person_home_ownership_enc"]=pers_h_own_label.transform(df_raw["person_home_ownership"]);
pers_h_own_label.classes_
pers_h_own_label.transform(pers_h_own_label.classes_)
# loan_intent
loan_intent_label=LabelEncoder()
loan_intent_label.fit(df_raw["loan_intent"])
df_raw["loan_intent_enc"]=loan_intent_label.transform(df_raw["loan_intent"]);
loan_intent_label.classes_
loan_intent_label.transform(loan_intent_label.classes_)
# loan_grade
loan_grade_label=LabelEncoder()
loan_grade_label.fit(df_raw["loan_grade"])
df_raw["loan_grade_enc"]=loan_grade_label.transform(df_raw["loan_grade"]);
loan_grade_label.classes_
loan_grade_label.transform(loan_grade_label.classes_)<jupyter_output><empty_output><jupyter_text>We're not going to use any more categorical features except these ones.   Dealing with NaN values<jupyter_code>pd.DataFrame(df_raw.isna().value_counts())<jupyter_output><empty_output><jupyter_text>person_emp_length has 827 + 68 NaN valuesloan_int_rate has 3048 + 68 NaN values<jupyter_code>plt.hist(df_raw["loan_int_rate"].value_counts(), bins=30);
df_raw.dropna(inplace=False).shape<jupyter_output><empty_output><jupyter_text>We're droping the rows with NaN. It's just almost 4 thousand observations, but I don't have time today to do data preprocessing... =DI'll improve this one day... :)<jupyter_code>df_raw=df_raw.dropna()
df_raw.to_csv(r"..\Data Set\Data_No_NaN.csv",index=False)<jupyter_output><empty_output><jupyter_text>Outputting the final data<jupyter_code>Y=df_raw["loan_status"]
X=df_raw[["person_age","person_income","person_home_ownership_enc",
          "person_emp_length",'loan_intent_enc',
         'loan_grade_enc','loan_amnt','loan_int_rate','loan_percent_income']]
X.info()
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.2,
                 random_state=123 # just for replicability. remove later. 
                )
X_train.to_csv(r"..\Data Set\X_train.csv",index=False)
X_test.to_csv(r"..\Data Set\X_test.csv",index=False)
Y_train.to_csv(r"..\Data Set\Y_train.csv",index=False)
Y_test.to_csv(r"..\Data Set\Y_test.csv",index=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup