Polynomial Regression Python Code - Loan Default Prediction

Import necessary libraries

import pandas

import numpy


import matplotlib.pyplot


from sklearn.linear_model import LinearRegression


from sklearn.preprocessing import PolynomialFeatures



Importing data into project
data = pandas.read_csv('loan_default_rate_dataset.csv')

print(data)



Assign Input Columns to X variable and Output column to Y Variable

x = data[['Credit_Score','Annual_Income','Loan_Amount']]


y = data['Loan_Default_Probability']



Importing Polynomial Features to do cross connections, squares in x data. So data will expand and model can learn unpredictable patterns

poly = PolynomialFeatures(degree=2)



training the X variable using the polynomial feature. So it will do cross connections, squares in x data and learn the complex patterns.

x_poly = poly.fit_transform(x)



Now, since we learned the complex patterns and expanded the x values into x_poly variable, we can do linear regression from the Polynomial X data and Y data.

model = LinearRegression()


model.fit(x_poly, y)


We predicted Output from x_poly and storing the data in predicted_y variable

predicted_y = model.predict(x_poly)

print(predicted_y)

Creating scatter plot graph to show actual Y values and predicted Y values


pyplot.figure(figsize=(12,8))

pyplot.scatter(range(len(y)),y,color='blue',label='Actual Default')

pyplot.scatter(range(len(y)),predicted_y,color='red',label='Predicted Default')

pyplot.legend()

pyplot.xlabel('Rows')

pyplot.ylabel('Loan Default Rate')

pyplot.show()

Checking the Model accuracy using r2 method.


r2 = model.score(x_poly,y)

print(r2)





Comments

Popular posts from this blog

What is Artificial Intelligence? What is Machine Learning? What is Data Science? how they are related to each other?

Linear Algebra - What is it?

What is a Python Library?