<jupyter_start><jupyter_text>mymodel01 > API details.<jupyter_code>#hide
from nbdev.showdoc import *<jupyter_output><empty_output><jupyter_text>Introduction<jupyter_code>!pip install nbdev==1.1.23<jupyter_output>Requirement already satisfied: nbdev==1.1.23 in /opt/app-root/lib/python3.8/site-packages (1.1.23)
Requirement already satisfied: ipykernel in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (5.5.5)
Requirement already satisfied: fastcore>=1.3.21 in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (1.3.27)
Requirement already satisfied: nbformat>=4.4.0 in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (5.1.3)
Requirement already satisfied: nbconvert<6 in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (5.6.1)
Requirement already satisfied: jupyter in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (1.0.0)
Requirement already satisfied: ghapi in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (0.1.19)
Requirement already satisfied: pyyaml in /opt/app-root/lib/python3.8/site-packages (from nbdev==1.1.23) (5.4.1)
Requirement already satisfied: jupyter-client<7.0 in /opt/app-root/lib/python3.8/site-pa[...]<jupyter_text>In this notebook kernel, I'm going to predictions customers are eligible for the loan and check whether what are the missing criteria to know why customer not getting loan to make there own house. We will learning about, Data Analysis Preprocess such as, --- >  Steps are:1. [Gathering Data](1)- [Exploratory Data Analysis](2)- [Data Visualizations](3)- [Machine Learning Model Decision.](4)- [Traing the ML Model](5)- [Predict Model](6)- [Deploy Model](7) **Hope** you guys ****Love It**** and get a better **learning experience**.  🙏 ---    Let's Say, You are the owner of the Housing Finance Company and you want to build your own model to predict the customers are applying for the home loan and company want to check and validate the customer are eligible for the home loan.       The Problem is,   In a Simple Term, Company wants to make automate the Loan Eligibility Process in a real time scenario related to customer's detail provided while applying application for home loan forms.You will use the training set to build your model, and the test set to validate it. Both the files are stored on the web as CSV files; their URLs are already available as character strings in the sample code. First of all, we need to importing the necessary packages to work with the data to solve our problem  Import Packages<jupyter_code>#export
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Show the Dataset Path to get detaset

import os

for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))<jupyter_output><empty_output><jupyter_text>You can load this data with the `read_csv()` method from `pandas` package. It converts the data set to a python dataframe.  Dataset Key Information.  ---    > - `Loan_ID`--------------> Unique Loan ID.- `Gender`   --------------> Male/ Female - `Married`  --------------> Applicant married (Y/N) - `Dependents` ------------> Number of dependents - `Education` -------------> Applicant Education (Graduate/ Under Graduate) - `Self_Employed` ---------> Self-employed (Y/N) - `ApplicantIncome` -------> Applicant income - `CoapplicantIncome` -----> Coapplicant income - `LoanAmount`  -----------> Loan amount in thousands - `Loan_Amount_Term` ------> Term of a loan in months - `Credit_History` --------> Credit history meets guidelines - `Property_Area` ---------> Urban/ Semi-Urban/ Rural - `Loan_Status` -----------> Loan approved (Y/N)   1. Gathering Data<jupyter_code>#export
#  Create New Variable and stores the dataset values as Data Frame

# loan_train = pd.read_csv('/kaggle/input/loan-eligible-dataset/loan-train.csv')
# loan_test = pd.read_csv('/kaggle/input/loan-eligible-dataset/loan-test.csv')

traindata_url="https://gist.githubusercontent.com/erwangranger/c1534647ecd6efc6188a92d69657f3bc/raw/eb6da3554971d9c3ab8f0506b37f285fb6453642/loans_train.csv"
testdata_url="https://gist.githubusercontent.com/erwangranger/43749b3d049b572ae83572719519cbbc/raw/47277db78d37994f8d9347265078be6619df234d/loans_test.csv"
loan_train = pd.read_csv(traindata_url)
loan_test = pd.read_csv(testdata_url)<jupyter_output><empty_output><jupyter_text>- Lets display the some few information from our large datasetsHere, We shows the first five rows from datasets<jupyter_code>loan_train.head()<jupyter_output><empty_output><jupyter_text>- As we can see in the above output, there are too many columns, ( columns known as features as well. )We can also use `loan_train` to show few rows from the first five and last five record from the dataset<jupyter_code>loan_train<jupyter_output><empty_output><jupyter_text>>  Here, we can see there are many rows and many columns, To know how many records and columns are available in our dataset, we can use the `shape` attribute or we can use `len()` to know how many records and how many features available in the dataset.<jupyter_code>print("Rows: ", len(loan_train))<jupyter_output>Rows:  614<jupyter_text>Pandas has inbuild attribute to get all column from the dataset, With the help of this feature we can get the how many column available we have.<jupyter_code>print("Columns: ", len(loan_train.columns))<jupyter_output>Columns:  13<jupyter_text>Also we can get the shape of the dataset using `shape` attribute<jupyter_code>print("Shape : ", loan_train.shape)<jupyter_output>Shape :  (614, 13)<jupyter_text>>  *After we collecting the data, Next step we need to understand what kind of data we have.*  Also we can get the column as an list(array) from dataset> **Note: DataFrame.columns returns the total columns of the dataset,> Store the number of columns in variable `loan_train_columns`**<jupyter_code>#export
loan_train_columns = loan_train.columns # assign to a variable
loan_train_columns # print the list of columns<jupyter_output><empty_output><jupyter_text>Now, Understanding the Data - First of all we use the `loan_train.describe()` method to shows the important information from the dataset- It provides the `count`, `mean`, `standard deviation (std)`, `min`, `quartiles` and `max` in its output.<jupyter_code>loan_train.describe()<jupyter_output><empty_output><jupyter_text>As I said the above cell, this the information of all the methamatical details from dataset. Like `count`, `mean`, `standard deviation (std)`, `min`, `quartiles(25%, 50%, 75%)` and `max`. >  Another method is `info()`, This method show us the information about the dataset, Like1. What's the type of culumn have?- How many rows available in the dataset?- What are the features are there?- How many null values available in the dataset?- Ans so on...<jupyter_code>loan_train.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 614 entries, 0 to 613
Data columns (total 13 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   Loan_ID            614 non-null    object 
 1   Gender             601 non-null    object 
 2   Married            611 non-null    object 
 3   Dependents         599 non-null    object 
 4   Education          614 non-null    object 
 5   Self_Employed      582 non-null    object 
 6   ApplicantIncome    614 non-null    int64  
 7   CoapplicantIncome  614 non-null    float64
 8   LoanAmount         592 non-null    float64
 9   Loan_Amount_Term   600 non-null    float64
 10  Credit_History     564 non-null    float64
 11  Property_Area      614 non-null    object 
 12  Loan_Status        614 non-null    object 
dtypes: float64(4), int64(1), object(8)
memory usage: 62.5+ KB<jupyter_text>As we can see in the output.1. There are `614` entries- There are total 13 features (0 to 12)- There are three types of datatype `dtypes: float64(4), int64(1), object(8)`- It's Memory usage that is, `memory usage: 62.5+ KB`- Also, We can check how many missing values available in the `Non-Null Count` column  2. Exploratory Data AnalysisIn this section, We learn about extra information about data and it's characteristics. - First of all, We explore object type of dataSo let's make a function to know how many types of values available in the column<jupyter_code>def explore_object_type(df ,feature_name):
    """
    To know, How many values available in object('categorical') type of features
    And Return Categorical values with Count.
    """    
    if df[feature_name].dtype ==  'object':
        print(df[feature_name].value_counts())<jupyter_output><empty_output><jupyter_text>- After defined a function, Let's call it. and check what's the output of our created function.<jupyter_code># Now, Test and Call a function for gender only
explore_object_type(loan_train, 'Gender')<jupyter_output>Male      489
Female    112
Name: Gender, dtype: int64<jupyter_text>- Here's one little issue occurred, Suppose in your datasets there are lots of feature to defined like this above code.<jupyter_code># Solution is, Do you remember we have variable with name of `loan_train_columns`, Right,  let's use it
# 'Loan_ID', 'Gender', 'Married', 'Dependents', 'Education', 'Self_Employed', 'Property_Area', 'Loan_Status'

for featureName in loan_train_columns:
    if loan_train[featureName].dtype == 'object':
        print('\n"' + str(featureName) + '\'s" Values with count are :')
        explore_object_type(loan_train, str(featureName))<jupyter_output>"Loan_ID's" Values with count are :
LP001006    1
LP001637    1
LP002131    1
LP002050    1
LP002953    1
           ..
LP001318    1
LP001052    1
LP002051    1
LP002368    1
LP002505    1
Name: Loan_ID, Length: 614, dtype: int64

"Gender's" Values with count are :
Male      489
Female    112
Name: Gender, dtype: int64

"Married's" Values with count are :
Yes    398
No     213
Name: Married, dtype: int64

"Dependents's" Values with count are :
0     345
1     102
2     101
3+     51
Name: Dependents, dtype: int64

"Education's" Values with count are :
Graduate        480
Not Graduate    134
Name: Education, dtype: int64

"Self_Employed's" Values with count are :
No     500
Yes     82
Name: Self_Employed, dtype: int64

"Property_Area's" Values with count are :
Semiurban    233
Urban        202
Rural        179
Name: Property_Area, dtype: int64

"Loan_Status's" Values with count are :
Y    422
N    192
Name: Loan_Status, dtype: int64<jupyter_text>>  *Note: Your output maybe shorter or longer, It's totally depend upon your dataset's columns*  - We need to fill null values with `mean` and `median` using `missingno` package<jupyter_code>#export

!pip install missingno
import missingno as msno
# list of how many percentage values are missing
loan_train

loan_train.isna().sum()
# round((loan_train.isna().sum() / len(loan_train)) * 100, 2)
msno.bar(loan_train)
msno.matrix(loan_train )<jupyter_output><empty_output><jupyter_text>- As we can see here, there are too many columns missing with small amount of null values so we use `mean` amd `mode` to replace with `NaN` values.<jupyter_code>#export


loan_train['Credit_History'].fillna(loan_train['Credit_History'].mode(), inplace=True) # Mode
loan_test['Credit_History'].fillna(loan_test['Credit_History'].mode(), inplace=True) # Mode


loan_train['LoanAmount'].fillna(loan_train['LoanAmount'].mean(), inplace=True) # Mean
loan_test['LoanAmount'].fillna(loan_test['LoanAmount'].mean(), inplace=True) # Mean<jupyter_output><empty_output><jupyter_text>convert Categorical variable with Numerical values. `Loan_Status` feature boolean values, So we replace `Y` values with `1` and `N` values with `0`and same for other `Boolean` types of columns<jupyter_code>#export

loan_train.Loan_Status = loan_train.Loan_Status.replace({"Y": 1, "N" : 0})
# loan_test.Loan_Status = loan_test.Loan_Status.replace({"Y": 1, "N" : 0}) 

loan_train.Gender = loan_train.Gender.replace({"Male": 1, "Female" : 0})
loan_test.Gender = loan_test.Gender.replace({"Male": 1, "Female" : 0})

loan_train.Married = loan_train.Married.replace({"Yes": 1, "No" : 0})
loan_test.Married = loan_test.Married.replace({"Yes": 1, "No" : 0})

loan_train.Self_Employed = loan_train.Self_Employed.replace({"Yes": 1, "No" : 0})
loan_test.Self_Employed = loan_test.Self_Employed.replace({"Yes": 1, "No" : 0})

#export

loan_train['Gender'].fillna(loan_train['Gender'].mode()[0], inplace=True)
loan_test['Gender'].fillna(loan_test['Gender'].mode()[0], inplace=True)

loan_train['Dependents'].fillna(loan_train['Dependents'].mode()[0], inplace=True)
loan_test['Dependents'].fillna(loan_test['Dependents'].mode()[0], inplace=True)

loan_train['Married'].fillna(loan_train['Married'].mode()[0], inplace=True)
loan_test['Married'].fillna(loan_test['Married'].mode()[0], inplace=True)

loan_train['Credit_History'].fillna(loan_train['Credit_History'].mean(), inplace=True)
loan_test['Credit_History'].fillna(loan_test['Credit_History'].mean(), inplace=True)<jupyter_output><empty_output><jupyter_text>* Here, `Property_Area`, `Dependents` and `Education` has multiple values so now we can use `LabelEncoder` from `sklearn` package<jupyter_code>#export

from sklearn.preprocessing import LabelEncoder
feature_col = ['Property_Area','Education', 'Dependents']
le = LabelEncoder()
for col in feature_col:
    loan_train[col] = le.fit_transform(loan_train[col])
    loan_test[col] = le.fit_transform(loan_test[col])<jupyter_output><empty_output><jupyter_text>>  Finally, We have all the features with numerical values,  3. Data VisualizationsIn this section, We are showing the visual information from the dataset, For that we need some pakages that are `matplotlib` and `seaborn`<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline


import seaborn as sns
sns.set_style('dark')

loan_train
loan_train.plot(figsize=(18, 8))

plt.show()
plt.figure(figsize=(18, 6))
plt.subplot(1, 2, 1)


loan_train['ApplicantIncome'].hist(bins=10)
plt.title("Loan Application Amount ")

plt.subplot(1, 2, 2)
plt.grid()
plt.hist(np.log(loan_train['LoanAmount']))
plt.title("Log Loan Application Amount ")

plt.show()
plt.figure(figsize=(18, 6))
plt.title("Relation Between Applicatoin Income vs Loan Amount ")

plt.grid()
plt.scatter(loan_train['ApplicantIncome'] , loan_train['LoanAmount'], c='k', marker='x')
plt.xlabel("Applicant Income")
plt.ylabel("Loan Amount")
plt.show()
plt.figure(figsize=(12, 6))
plt.plot(loan_train['Loan_Status'], loan_train['LoanAmount'])
plt.title("Loan Application Amount ")
plt.show()
plt.figure(figsize=(12,8))
sns.heatmap(loan_train.corr(), cmap='coolwarm', annot=True, fmt='.1f', linewidths=.1)
plt.show()<jupyter_output><empty_output><jupyter_text>In this heatmap, we can clearly seen the relation between two variables   4. Choose ML Model. * In this step, We have a lots of Machine Learning Model from sklearn package, and we need to decide which model is give us the better performance. then we use that model in final stage and send to the production level.<jupyter_code>#export
# import ml model from sklearn pacakge

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>First of all, we are use `LogisticRegression` from `sklearn.linear_model` package. Here is the little information about `LogisticRegression`.`Logistic Regression` is a **classification algorithm**. It is used to predict a binary outcome (`1 / 0`, `Yes / No`, and `True / False`) given a set of independent variables. To represent binary / categorical outcome, we use dummy variables. You can also think of logistic regression as a special case of linear regression when the outcome variable is categorical, where we are using log of odds as the dependent variable.![](https://www.analyticsvidhya.com/wp-content/uploads/2015/10/logit.png) * Let's build the model<jupyter_code>#export
logistic_model = LogisticRegression()<jupyter_output><empty_output><jupyter_text>5. Traing the ML Model >  **Before fitting the model, We need to decide how many feature are available for testing and training, then after complete this step. fitt the model** Currently, we are using `Credit_History', 'Education', 'Gender` features for training so let's create train and test variables<jupyter_code>#export
train_features = ['Credit_History', 'Education', 'Gender']

x_train = loan_train[train_features].values
y_train = loan_train['Loan_Status'].values

x_test = loan_test[train_features].values

#export
logistic_model.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>6. Predict Model<jupyter_code>#export
# Predict the model for testin data

predicted = logistic_model.predict(x_test)

# check the coefficeints of the trained model
print('Coefficient of model :', logistic_model.coef_)

# check the intercept of the model
print('Intercept of model',logistic_model.intercept_)
# Accuray Score on train dataset
# accuracy_train = accuracy_score(x_test, predicted)
score = logistic_model.score(x_train, y_train)
print('accuracy_score overall :', score)
print('accuracy_score percent :', round(score*100,2))


# predict the target on the test dataset
predict_test = logistic_model.predict(x_test)
print('Target on test data',predict_test)<jupyter_output>Target on test data [1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 1 1 1 1
 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1
 1 1 1 1 1 1 0 0 0 1 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0
 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 0 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 0 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 0 1 0 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1
 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1]<jupyter_text>7. Deploy Model - Finally, we are done so far. The last step is to deploy our model in production map. So we need to export our model and bind with web application API. Using pickle we can export our model and store in to `logistic_model.pkl` file, so we can ealy access this file and calculate customize prediction using Web App API. A little bit information about pickle:`Pickle` is the standard way of serializing objects in Python. You can use the pickle operation to serialize your machine learning algorithms and save the serialized format to a file. Later you can load this file to deserialize your model and use it to make new predictions>>  Here is example of the Pickle export model```model.fit(X_train, Y_train) save the model to diskfilename = 'finalized_model.sav'pickle.dump(model, open(filename, 'wb')) some time later... load the model from diskloaded_model = pickle.load(open(filename, 'rb'))result = loaded_model.score(X_test, Y_test)print(result)```<jupyter_code>#export
import pickle as pkl
#export
filename = 'logistic_model_01.pkl'
pkl.dump(logistic_model, open(filename, 'wb')) # wb means write as binary<jupyter_output><empty_output><jupyter_text>Now, You can check your current directory. You can see the file with named "logistic_model.pkl" - To read model from file``` load the model from diskloaded_model = pkl.load(open(filename, 'rb'))  rb means read as binaryresult = loaded_model.score(X_test, Y_test)```<jupyter_code>#loaded_model = pkl.load(open(filename, 'rb')) # rb means read as binary
#result = loaded_model.score(x_test, y_test)
from nbdev.export import *
notebook2script()<jupyter_output>Converted 00_core.ipynb.
Converted create_model_01.ipynb.
Converted index.ipynb.
{'fraction_non_alphanumeric': 0.10857719475277497, 'fraction_numerical': 0.04626639757820383, 'mean_word_length': 4.508893829905503, 'pattern_counts': {'":': 8, '<': 99, '<?xml version=': 0, '>': 125, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 58, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '502132', 'license': 'Apache-2.0', 'n_tokens_mistral': 7261, 'n_tokens_neox': 6318, 'n_words': 2336, 'path': 'create_model_01.ipynb', 'repo_name': 'erwangranger/nbdevtest01', 'size': '19820'}