<jupyter_start><jupyter_text>Credit Card Defaults** Data Points : Data set contains 10000 records associated with credit cards accounts.**- Defualt : Indicates whether credit card holder defualted or not.- Student : If holder is a student.- Balance : Card holders current outstanding balance.- Income  : Income of the card holder.<jupyter_code>%pylab inline
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np
default=pd.read_csv("Data/Default.csv")
default.head()
default.describe()<jupyter_output><empty_output><jupyter_text>* Split data into train and test data<jupyter_code>from sklearn.cross_validation import train_test_split
train, test = train_test_split(default, test_size=0.33, random_state=1)
# Converting data into the dataframe
train = pd.DataFrame(data=train, columns=default.columns)
test = pd.DataFrame(data=test, columns=default.columns)

train.hist()
plt.scatter(train.income, train.balance)
plt.xlim(0, 75000)
plt.ylim(0, 3000)
plt.xlabel('Income')
plt.ylabel('Balance')
plt.title('Income vs Balance')
train.plot(x='balance', y='income', kind='scatter', alpha=0.3)
plt.ylim([0,80000])
plt.xlim([0,3000])

train_default=train[train.default==1]
train_no_default=train[train.default==0]
plt.figure()
plt.scatter(train_no_default.balance, train_no_default.income, alpha=0.5, marker='+', c='g')
plt.scatter(train_default.balance, train_default.income, marker='o', edgecolors = 'r', facecolors = 'none')
plt.legend( ('No Default', 'Default'), loc='upper right')
plt.ylim([0,80000])
plt.xlim([0,3000])
plt.xlabel('Income')
plt.ylabel('Balance')<jupyter_output><empty_output><jupyter_text>Plot the same graph differently<jupyter_code>plt.scatter(train_default.balance, train_default.income, alpha=0.5, marker='o', c='g')
plt.scatter(train_no_default.balance, train_no_default.income, marker='+', edgecolors = 'r', facecolors = 'none')
plt.legend( ('Default', 'No Default'), loc='upper right')
plt.ylim([0,80000])
plt.xlim([0,3000])
plt.xlabel('Income')
plt.ylabel('Balance')<jupyter_output><empty_output><jupyter_text>Conclusion- Balace is more closely related to the default rate. =============================================            Logistic Regreassion =============================================<jupyter_code># Logistic regression on the balance variable
bal = smf.logit(formula='default ~ balance', data=train).fit()
bal.summary()
# Get confidence interval.
bal.conf_int()
# odds ratio or Beta
# http://www.ats.ucla.edu/stat/mult_pkg/faq/general/odds_ratio.htm
np.exp(bal.params.balance)<jupyter_output><empty_output><jupyter_text>* Various probability of defualt based on the balance.<jupyter_code>prob = bal.predict({'balance': [1200, 2000]})
print (prob)
bal.predict({'balance': 1900})
x = np.linspace(test.balance.min(), test.balance.max(), 500)
betas = [bal.params.Intercept, bal.params.balance]
y = np.exp(betas[0] + betas[1]*x) / (1 + np.exp(betas[0] + betas[1]*x))
odds = np.exp(betas[0] + betas[1]*x)
log_odds = betas[0] + betas[1]*x
plt.plot(x, y, 'r', linewidth=2)
plt.ylabel('Probability')
plt.text(500, 0.7, r'$\frac{e^{\beta_o + \beta_1x}}{1+e^{\beta_o + \beta_1x}}$', fontsize=25)
plt.plot(x, odds, 'k', linewidth=2)
plt.ylabel('Odds')
plt.text(500, 30, r'$e^{\beta_o + \beta_1x}$', fontsize=20)

plt.plot(x, log_odds, 'c', linewidth=2)
plt.ylabel('Log(Odds)')
plt.xlabel('x')
plt.text(500, 2, r'$\beta_o + \beta_1x$', fontsize=15)
x_data = pd.DataFrame({'balance': np.linspace(test.balance.min(), 
                                              test.balance.max(),
                                              500)})
y_data = bal.predict(x_data)
plt.figure()
plt.scatter(test.balance, test.default, alpha=0.5)
plt.plot(x_data, y_data, 'r', linewidth=2)
plt.xlabel("Balance")
plt.ylabel("Probability of Default")
plt.ylim([-0.05,1.05])
plt.xlim([0, 2800])

# Draw default
plt.plot([1200, 1500], prob, 'y')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18671045982819606, 'fraction_numerical': 0.03486609398686205, 'mean_word_length': 5.873263888888889, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '546693', 'license': 'Apache-2.0', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1404, 'n_words': 287, 'path': 'Data_Story_2/logistic_regression.ipynb', 'repo_name': 'abhipr1/DATA_SCIENCE_INTENSIVE', 'size': '3958'}