Showing posts with label project. Show all posts
Showing posts with label project. Show all posts

Sunday, 12 September 2021

Loan Approval Prediction using Python


 

 

Introduction 


Background


    Loan eligibility is primarily dependent on the income and repayment capacity of the individual(s). There are other factors that determine the eligibility of loan such as age, financial position, credit history, credit score, and other financial duty.


Problem Description

    Loan Approval Prediction is automate loan eligibility process from customer details. These details are Gender, Marital status, Education, Number of dependents, Self-employed, Monthly income, Loan Application amount and Credit history. In this process identify the customer segmentation and those are eligible for the loan amount.

Interest

    Financial Company or banks needs to automate the loan eligibility process based on customer detail provided while filling online application form.  In this project used to automate loan eligibility process from historical data for customer details.


Data acquisition and cleaning


Data set

Range Index: 614 entries (0 to 613)

Data columns: 13 columns


Data type


Dataset have three type of data types of the values. These are Object, Float and Integer. The number of categorical datatype in shown in figure 1.1.

Figure 1.1


You can find more information about the data, go to Loan Approval Data

Data cleaning


1. First downloaded the data from source and find the data information, description and shape of the data in after analysis. There were missing values from dataset, because of lack of record keeping. And also lot of data is object type. It is trouble to feature prediction. So we change the datatype from object to numeric values.

2. Data set has to several problems, so start the cleaning of data. 

3. Many columns are contain object type of datatype. And then some other columns are complicated values like date, float and negative values. After drop the unwanted columns based on further analysis.

4. After fixing these problems, I checked for outliers in the data. I found there were some extreme outliers, mostly caused by some types of small sample size problem.


Feature Selection


 Final step of the Data acquisition, Feature selection is important to the predictive modelling. After data cleaning, there were 194673 samples and 49 features in the data. Upon examining the meaning of each feature, it was clear that there was some redundancy in the features. But these data set contain all features are important to future predicting.


Exploratory Data Analysis

    The problem is Loan Approval prediction. So the target value is Loan Status.  The Loan _Status column contain loan approved or not. It is categorical (Yes / No) values in shown figure 1.2.


Figure 1.2


    Gender is common feature of identical data. It is the range of characteristics pertaining to, and differentiating between, masculinity and femininity. It has two different types of feature in this columns. There are male and female. The total number of male is 489 and total number of female is 112. And then given plot is define how many male or female approval for loan in shown figure 1.3.

Figure 1.3



    Marital status is one of the important features. It distinct options that describe a person’ relationship with a significant other. Here are some of the important ways a change in marital status can affect the target variable. The value of ‘Married’ are ‘Yes’ and ‘No’. The total number of ‘Yes’ is 398 and total number of ‘No’ is 213. And then given plot is define marital status for loan approval in shown figure 1.4.

Figure 1.4

    Education is major role on loan approval prediction. Loan eligibility criteria is pursing graduate or post graduate degree.  It has to contain two values, there are ‘Graduate’ and ‘Not Graduate’. The total number of ‘Graduate’ is 480 and total number of ‘Not Graduate’ is 134.  And then given plot is define Education for loan approval in shown figure 1.5.


Figure 1.5

    Self-Employment is another major role on loan approval prediction. Here the maximum applicants are not self-employed for this dataset. The value of ‘Self-Employed’ are ‘Yes’ and ‘No’. The total number of ‘Yes’ is 82 and total number of ‘No’ is 500. And then given plot is define Self-Employed for loan approval in shown figure 1.6.



Figure 1.6

    The distribution is represent for log transformation. The log transformation can be used to make highly skewed distributions less skewed. This can be valuable both for making patterns in the data more interpretable and for helping to meet the assumptions of inferential statistics.

    The plots are different between normal distribution and log normal distribution for total income in shown figure 1.7.

Figure 1.7


    This plot are different between normal distribution and log normal distribution for loan amount in shown figure 1.8. 

Figure 1.8


Box plot of major features

    The box plot is represent shape of distribution, its central value, and its variability. Its helps to understanding of data in shown figure 1.9. 

Figure 1.9


Correlation between each data


    Correlation of data is define relationship between each columns in the data. It has to understand how to handle the data and columns. In this plot of diagram is represent with different colors of range (-0.2 to 1.0). The range of color is negative value, it low rate of correlation between the data. For example, Education and Loan Amount is low correlation of each data. Its correlation value is -0.2.

    The range of color is equal to zero, it normal of correlation between the data. In dataset more amount of columns are correlation between each data should be normal. For example, Gender and Self_Employed, ApplicantIncome and Loan_Amount_Term.




Figure 1.10


    The range of color is positive value, it high rate of correlation between the data. Each columns are highly correlated in x-axis and y-axis. So both are same values. And otherwise some columns are highly correlated. For example, Total income and Application Income is high correlation of each data. Its correlation value is 1.0. The correlation of each data is visualize in shown figure 1.10.


Relationship between major features


    The matrix plot is represent to the relationship between major features. In this plot visualize the data based on scatter plot and histogram. The histogram is present in diagonal. The scatter plot is represent to the relationship between major columns without diagonal. The matrix plot in shown figure 1.11.


 



Figure 1.11

Predictive Modelling

    Predictive modelling uses statistics to predict outcomes. It is the general concept of building a model that capable of making predictions. Typically, such a model includes a machine learning algorithm that learns certain properties from a training data set in order to make those predictions.


Models

    Models can use one or more classifiers in trying to determine the probability of a set of data belonging to another set. There are two types of models, Regression and Classification. 
    
    Regression is Supervised Learning task where output is having continuous value. The goal here is to predict a value as much closer to actual output value as our model can and then evaluation is done by calculating error value. The smaller the error the greater the accuracy of our regression model. 

    Classification is a Supervised Learning task where output is having defined labels (discrete value). The goal here is to predict discrete values belonging to a particular class and evaluate on the basis of accuracy. It can be either binary or multi class classification. In binary classification, model predicts either 0 or 1; yes or no but in case of multi class classification, model predicts more than one class.  

    In this project target value is categorical type (discrete value). So I choose classification model.



Applying standard Classification algorithms 

    Classification in machine learning and statistics is a supervised learning approach in which the computer program learns from the data given to it and make new observations or classifications. A classification model attempts to draw some conclusion from observed values. Given one or more inputs a classification model will try to predict the value of one or more outcomes. Outcomes are labels that can be applied to a data set.


    There are a number of classification models. Classification models include K nearest neighbor and Naive Bayes, Logistic regression, Decision tree, and Random forest.



Performance of Models

    Model evaluation metrics are required to quantify model performance. I choose the model evaluation metrics depends on our machine learning task such as classification algorithms. In precision – recall are useful for multiple tasks.

    I applied some classification matrix for model evaluation. There are Classification accuracy and Confusion matrix. Classification accuracy is the number of correct prediction made as a ratio of all predictions made. Confusion matrix provide a more detailed breakdown of correct and incorrect classification for each class. And also fine Actual and predicted values (True and False).

    It estimated performance of a model tells as how well it preform on unseen data. And also I find best classifier on this problem based on the table, it is decision tree. It has to high accuracy and better result on confusion matrix. In table explain performance of different models.

Conclusion 


    Finally, I predicted the loan approval based on further analysis. I achieved above 80% accuracy in classification algorithms. That is helps to identify the eligibility of loan. And also analysis of major features, it used to get better result of this problem.

Purpose of this project was to predict the Loan Approval. Company wants to automate the loan eligibility process based on customer details. These details are Gender, Marital status, Education, Number of dependents, Self-employed, Monthly income, Loan Application amount and Credit history. In this process identify the customer segmentation and those are eligible for the loan amount.



And you want to explore the project:



Sunday, 29 August 2021

Breast Cancer Prediction using CNN

 



Breast Cancer Prediction using Convolutional Neural Network for Beginners


Introduction:


    In this blog, I created Breast Cancer prediction using Convolutional neural network on breast cancer data, which I have implemented using Keras.

    This blog offers you a step-by-step instruction guide with source code, so you can build your model. It is not designed to be a deep dive into model design, statistical analysis, improvement, and validation. If you want to learn more, please check out my blog site: Techy Scientists.

It contains the following parts:


  1. Setup your environment
  2. Build your Breast Cancer classification model
  3. Model Validation


Setup your environment


   To run the program on your local computer, install the following required libraries, These libraries are 


  1.   python 3.8.0
  2.   numpy
  3.   pandas
  4.   matplotlib
  5.   scikit-learn
  6.   tensorflow 2.0
  7.   keras 2.3.0


Build your Breast Cancer classification model


Step 1: Understand the data


  The first step of model prediction is to understand the data. It is more important to all machine learning and deep learning projects. You can find more information about the data, go to Breast Cancer Data.



Step 2: Import the Packages


  Create a python file (for example model.py). After installed the required packages, import packages  in your python file.


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn import datasets, metrics
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from keras import Sequential
from keras.layers import Flatten, Dense, Dropout
from keras.layers import BatchNormalization
from keras.layers import Conv1D, MaxPool1D

Step 3: Import the data

    Next, import the data using pandas

data = datasets.load_breast_cancer()
df = pd.DataFrame(data=data.data, 
	columns=data.feature_names)

Step 4: Split the data
    
    We want to create a model, must have split it into training and testing. the model trained by training dataset and then apply the evaluation of model used by test dataset.

X = df
y = data.target

X_train, X_test, y_train, y_test = 
train_test_split(X, y, test_size=0.2)
print("Train_Data:")
print(X_train.shape)
print(y_train.shape)
print("\nTest_Data:")
print(X_test.shape)
print(y_test.shape)
Out[]:
Train_Data:
(455, 30)
(455,)

Test_Data:
(114, 30)
(114,)


Step 5: Standardization the data

    Standardize the data removes the mean and scales each feature or variable to unit variance.
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)

Then, resize the data

X_train = X_train.reshape(455, 30, 1)
X_test = X_test.reshape(114, 30, 1)

Step 6:
 Create Convolutional Neural Network Model

    We create model for breast cancer prediction model using convolutional neural network. It is type of deep learning networks. It is used for classification, segmentation and image processing problems. In this neural network, extract the features from input layer and perform mathematical convolutional operation. 

epochs = 10
model = Sequential()
model.add(Conv1D(32, 2, 
	activation='relu', 
	input_shape=(30,1)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Conv1D(64, 2, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))


  •     Sequential - appropriate for a plain stack of layers where each layer has    exactly one input tensor and one output tensor.
  •     Conv1D - Convolutional one dimensional layer
  •     Batch Normalization - allows the network to do learning more independently. It is used to normalize the output of the previous layers.
  •     Flatten -  matrix flatten to one dimensional array.
  •     Dense - fully connected neural network layer and it implement the operations.
  •     Activation - used through an activation  layer, or through the activation argument supported by all forward layers.



Step 7: Train the Model

    Now, we ready to train the model.

model.compile(optimizer='rmsprop', 
	loss='binary_crossentropy', 
    	metrics=['accuracy'])

history=model.fit(X_train,
	y_train,
    	epochs = epochs,
    	validation_data = (X_test, y_test))
Out[]:
Train on 455 samples, validate on 114 samples
Epoch 1/10
455/455 [=========] - 8s 18ms/sample - loss: 0.2800 - 
accuracy: 0.8835- val_loss: 0.4431- val_accuracy: 0.947
Epoch 2/10
455/455 [=========] - 1s 1ms/sample - loss: 0.1105 - 
accuracy: 0.9582- val_loss: 0.3559 - val_accuracy: 0.929
Epoch 3/10
455/455 [=========] - 1s 1ms/sample - loss: 0.1102 - 
accuracy: 0.9648- val_loss: 0.3136 - val_accuracy: 0.886
Epoch 4/10
455/455 [=========] - 1s 1ms/sample - loss: 0.1375 - 
accuracy: 0.9604- val_loss: 0.3360 - val_accuracy: 0.842
Epoch 5/10
455/455 [=========] - 1s 1ms/sample - loss: 0.1137 - 
accuracy: 0.9714- val_loss: 0.3202 - val_accuracy: 0.850
Epoch 6/10
455/455 [=========] - 1s 1ms/sample - loss: 0.0760 - 
accuracy: 0.9802- val_loss: 0.3164 - val_accuracy: 0.842
Epoch 7/10
455/455 [=========] - 1s 1ms/sample - loss: 0.0655 - 
accuracy: 0.9692- val_loss: 0.2960 - val_accuracy: 0.859
Epoch 8/10
455/455 [=========] - 1s 1ms/sample - loss: 0.0661 - 
accuracy: 0.9758- val_loss: 0.3347 - val_accuracy: 0.850
Epoch 9/10
455/455 [=========] - 1s 1ms/sample - loss: 0.1068 - 
accuracy: 0.9758- val_loss: 0.2602 - val_accuracy: 0.877
Epoch 10/10
455/455 [=========] - 1s 1ms/sample - loss: 0.0466 - 
accuracy: 0.9846- val_loss: 0.2348 - val_accuracy: 0.903


Model Validation

    Finally, We created the model and then validate it.

test =  model.evaluate(X_test, y_test, verbose=0)
print("Testing Accuracy: %.2f%%" % (test[1]*100))
Out[]:
Testing Accuracy: 90.35%
    Visualize the accuracy and loss between training and validation.
def plot_result(history, epoch):
    
    epoch_range = range(1, epoch+1)
    
    plt.plot(epoch_range, 
    	history.history['accuracy'], 
        label='Training acc')
    plt.plot(epoch_range, 
    	history.history['val_accuracy'], 
        label='Validation acc')
    plt.title('Training and validation accuracy')
    plt.xlabel('epochs')
    plt.ylabel('acc')
    plt.legend()

    plt.show()
    
    plt.plot(epoch_range, 
    	history.history['loss'], 
        label='Training loss')
    plt.plot(epoch_range, 
    	history.history['val_loss'], 
        label='Validation loss')
    plt.title('Training and validation loss')
    plt.xlabel('epochs')
    plt.ylabel('loss')
    plt.legend()

    plt.show()
plot_result(history, epochs)

Training and validation accuracy



Training and validation loss



Conclusion:

     In summary, we created the breast cancer prediction model using convolutional neural network on breast cancer data, which I have implemented using Keras. If you want to source code, check this GitHub linkBreast Cancer Prediction using CNN.

Thank you...



Saturday, 31 July 2021

House Price Prediction using Flask

 



House Price Prediction using Flask


Introduction:


   In this project, I developed the predictive power of a model trained on houses price data. It deploys with flask API and using Linear Regression to predict the price value. Deploy Machine Learning Model Using Flask to take a model from python code.


    This blog offers you a step-by-step instruction guide with source code, so you can build your model. It is not designed to be a deep dive into model design, statistical analysis, improvement, and validation. If you want to learn more, please check out my blog site: Techy Scientists.


It contains the following parts:


  1. Setup your environment
  2. Build your house price prediction model
  3. Create flask API
 

Setup your environment


   To run the web app on your local computer, install the required libraries, These packages are 


  1.   python 3.8.0
  2.   flask 2.0.1
  3.   werkzeug 2.0.1
  4.   sci-kit learn
  5.   pandas
  6.   numpy
  7.   pickle



Build your house price prediction model


Step 1: Understand the data


  The first step of model prediction is to understand the data. It is more important to all machine learning and deep learning projects. You can find more information about the data, go to House Price - Advanced Regression Techniques competition in Kaggle (Note: If you don't know Kaggle, please check this blog What is Kaggle? How to use it?). And it is also available in my repositories and find the dataset or click this link: dataset.


Step 2: Import the Packages


  Create a python file (for example house.py). After installed the required packages, import packages  in your python file.


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle

Step 3: Import the data

    Next, import the data using pandas
df = pd.read_csv('house_data.csv')
    
Step 4: Feature Selection 

    Remove the unwanted features (Columns), now choose columns are  number of bedrooms, number of bathrooms, number of floors, year of build, and price of the house. In price of the house is the target value. We trained the model in the other four columns to find the price of the house.
columns = ['bedrooms', 
	'bathrooms', 
	'floors', 
	'yr_built', 
	'price']

df = df[columns]

X = df.iloc[:, 0:4]
y = df.iloc[:, 4:]

Step 5: Split the data
    
    We want to create a model, must have split it into training and testing. the model trained by training dataset and then apply the evaluation of model used by test dataset.
X_train, X_test, y_train, y_test = 
train_test_split(X, y, test_size=0.25)

Step 6: Create Machine Learning Model
    
    Now, we use Linear regression model to predict the house price.
lr = LinearRegression()
lr.fit(X_train, y_train)


Step 7: Dump the model using pickle

    It used to store the model data in a file

pickle.dump(lr, open('model.pkl', 'wb'))


Create Flask API


    Flask is a web framework for python. It is used for managing HTTP request and render templates. Now, start to create basic flask API (for example app.py). 

    Pickle load function is used to load the model data in flask API. App route('/') map to home page('index.html') and App route(/predict) map to predict function, it is also call in home page. In predict function get the values from the form and then used them for model prediction.

from flask import Flask, render_template, request
import numpy as np
import pickle

app = Flask(__name__)
model = pickle.load(open('model.pkl', 'rb'))


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/predict', methods=['GET', 'POST'])
def predict():
    val1 = request.form['bedrooms']
    val2 = request.form['bathrooms']
    val3 = request.form['floors']
    val4 = request.form['yr_built']
    arr = np.array([val1, val2, val3, val4])
    arr = arr.astype(np.float64)
    pred = model.predict([arr])

    return render_template('index.html', 
    	data=int(pred))


if __name__ == '__main__':
    app.run(debug=True)

Finally, create simple form in html
<!DOCTYPE html>
<html>

<head>
    
<link rel='stylesheet' type='text/css' 
	media='screen'
	href="{{ url_for('static', 
    	filename='css/main.css') }}">
</head>

<body>

<h1>HOUSE PRICE PREDICTION</h1>
<form action="{{url_for('predict')}}" method="POST">
Bedrooms: <input type="number" name='bedrooms' 
	placeholder="Enter the no of Bedrooms"><br>
Bathrooms: <input type="number" name='bathrooms' 
	placeholder="Enter the no of Bathrooms"><br>
Floors: <input type="number" name='floors' 
	placeholder="Enter the no of Floors"><br>
Year of Build: <input type="number" name='yr_built' 
	placeholder="Enter year of Build"><br>
<input type="submit" value="predict" class="submit">
</form>
<p> House Price is: {{data}}</p>

</body>
</html>


Preview:





Conclusion:


    To run code on your computer, following command in terminal
python app.py
    In summary, we created the House Price Prediction using Linear Regression and deploy with Flask application of the given  dataset. If you want to source code, check this GitHub link: House Price Prediction using Flask.