Created
April 26, 2016 02:52
-
-
Save matmoody/92d23d30b36a6212b6c9098de3b64dbe to your computer and use it in GitHub Desktop.
Linear regression analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import statsmodels.api as sm | |
import matplotlib.pyplot as plt | |
%matplotlib inline | |
import pandas as pd | |
loansData = pd.read_csv('https://github.com/Thinkful-Ed/curric-data-001-data-sets/raw/master/loans/loansData.csv') | |
# Brief look at the data | |
loansData.head() | |
# Clean Interest.Rate column | |
loansData['Interest.Rate'] = loansData['Interest.Rate'].map(lambda x: float(x.rstrip('%'))) | |
# Clean Loan.Length column | |
loansData['Loan.Length'] = loansData['Loan.Length'].map(lambda x: x.strip(' months')) | |
# Clean and convert FICO.Range | |
loansData['FICO.Score'] = loansData['FICO.Range'].map(lambda x: int(x[:3])) | |
# Review data to make sure cleaning and conversions look correct | |
loansData.head() | |
plt.figure() | |
p = loansData['FICO.Score'].hist() | |
plt.show() | |
a = pd.scatter_matrix(loansData, alpha=0.05, figsize=(10,10), diagonal='hist') | |
intrate = loansData['Interest.Rate'] | |
loanamt = loansData['Amount.Requested'] | |
fico = loansData['FICO.Score'] | |
# Dependent variable | |
y = np.matrix(intrate).transpose() | |
# Independent variables | |
x1 = np.matrix(fico).transpose() | |
x2 = np.matrix(loanamt).transpose() | |
# Put the two indepedent variable columns together to make input matrix | |
# One column for each indepdent variable | |
x = np.column_stack([x1,x2]) | |
# Now create linear model | |
X = sm.add_constant(x) | |
model = sm.OLS(y, X) | |
f = model.fit() | |
f.summary() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment