<jupyter_start><jupyter_text>So, there are missing values on many variables including the Gender, Married, LoanAmount variable. Next, we will remove all the rows which contain any missing values in them:<jupyter_code>train = train.dropna()
train.isnull().sum()<jupyter_output><empty_output><jupyter_text>Now there are no missing values in the dataset. Next, we will separate the dependent (Loan_Status) and the independent variables:<jupyter_code>X = train[['Gender', 'Married', 'ApplicantIncome', 'LoanAmount', 'Credit_History']]
y = train.Loan_Status
X.shape, y.shape
#X=pd.get_dummies(X,columns=['Gender','Married'])
import category_encoders as ce
encoder= ce.OrdinalEncoder(cols=['Gender'],return_df=True,
                           mapping=[{'col':'Gender',
'mapping':{'Male':0,'Female':1}}])

X_transformed = encoder.fit_transform(X)


encoder1= ce.OrdinalEncoder(cols=['Married'],return_df=True,
                           mapping=[{'col':'Married',
'mapping':{'No':0,'Yes':1}}])

X = encoder1.fit_transform(X_transformed)
X.head()
X.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 480 entries, 1 to 613
Data columns (total 5 columns):
 #   Column           Non-Null Count  Dtype  
---  ------           --------------  -----  
 0   Gender           480 non-null    int64  
 1   Married          480 non-null    int64  
 2   ApplicantIncome  480 non-null    int64  
 3   LoanAmount       480 non-null    float64
 4   Credit_History   480 non-null    float64
dtypes: float64(2), int64(3)
memory usage: 22.5 KB<jupyter_text>For this particular project, I have only picked 5 variables that I think are most relevant. These are the Gender, Marital Status, ApplicantIncome, LoanAmount, and Credit_History and stored them in variable X. Target variable is stored in another variable y. And there are 480 observations available. Next, let’s move on to the model building stage.Here, we will first split our dataset into a training and validation set, so that we can train the model on the training set and evaluate its performance on the validation set.<jupyter_code>from sklearn.model_selection import train_test_split
x_train, x_cv, y_train, y_cv = train_test_split(X,y, test_size = 0.2, random_state = 10)<jupyter_output><empty_output><jupyter_text>We have split the data using the train_test_split function from the sklearn library keeping the test_size as 0.2 which means 20 percent of the total dataset will be kept aside for the validation set. Next, we will train the random forest model using the training set:<jupyter_code>from sklearn.ensemble import RandomForestClassifier 
model = RandomForestClassifier(max_depth=5, random_state = 15) 
model.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>Here, I have kept the max_depth as 4 for each of the trees of our random forest and stored the trained model in a variable named model. Now, our model is trained, let’s check its performance on both the training and validation set:<jupyter_code>from sklearn.metrics import accuracy_score
pred_cv = model.predict(x_cv)
accuracy_score(y_cv,pred_cv)<jupyter_output><empty_output><jupyter_text>The model is 80% accurate on the validation set. Let’s check the performance on the training set too:<jupyter_code>pred_train = model.predict(x_train)
accuracy_score(y_train,pred_train)
# saving the model 
import pickle 
pickle_out = open("predictor.pkl", mode = "wb") 
pickle.dump(model, pickle_out) 
pickle_out.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09526542324246771, 'fraction_numerical': 0.020946915351506456, 'mean_word_length': 4.752475247524752, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '30438', 'license': 'MIT', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1059, 'n_words': 355, 'path': 'AutomatedLoanPredictor.ipynb', 'repo_name': 'eetinosa/AutomatedLoanPredict', 'size': '3485'}