Skip to content

Instantly share code, notes, and snippets.

@joeleonjr
Created December 5, 2016 15:54
Show Gist options
  • Save joeleonjr/f0ea56e5dbc83923cbb00fd247948c84 to your computer and use it in GitHub Desktop.
Save joeleonjr/f0ea56e5dbc83923cbb00fd247948c84 to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import statsmodels.api as sm
loansData = pd.read_csv('https://github.com/Thinkful-Ed/curric-data-001-data-sets/raw/master/loans/loansData.csv')
loansData.dropna(inplace=True)
loansData['Interest.Rate'] = loansData['Interest.Rate'].map(lambda x: float(x.replace('%', '')))
loansData['Loan.Length'] = loansData['Loan.Length'].map(lambda x: float(x.replace(' months', '')))
loansData['FICO.Score'] = loansData['FICO.Range'].map(lambda x: float(x.split('-')[0]))
# plt.figure(1)
# loansData.hist(column='FICO.Score')
# plt.show()
# plt.figure(2)
# stats.probplot(loansData['FICO.Score'], dist='norm', plot=plt)
# plt.show()
#plt.figure(3)
#pd.scatter_matrix(loansData, alpha=.05, figsize=(10,10), diagonal='hist')
#plt.show()
interest_rate = loansData['Interest.Rate']
loan_amount = loansData['Amount.Requested']
fico = loansData['FICO.Score']
y = np.matrix(interest_rate).transpose()
x1 = np.matrix(fico).transpose()
x2 = np.matrix(loan_amount).transpose()
x = np.column_stack([x1,x2])
X = sm.add_constant(x)
#this appends a column of ones to an array (constant term)
model = sm.OLS(y,X)
result = model.fit()
print(result.summary())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment