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)
x = data[['Credit_Score','Annual_Income','Loan_Amount']]
y = data['Loan_Default_Probability']
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)
model = LinearRegression()
model.fit(x_poly, y)
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
Post a Comment