text_plain_output_1.png
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import sklearn as sklearn #machine learning

test = pd.read_csv('../input/test.csv', low_memory=False)
train = pd.read_csv('../input/train.csv', low_memory=False)

loans_in_default = train.default.value_counts(True)

default_by_zip = train.default.groupby(train.ZIP).mean()

default_by_year = train.default.groupby(train.year).mean()

y_train = train['default']
x_train = train[['rent', 'education', 'income', 'loan_size', 'payment_timing', 'job_stability', 'ZIP', 'occupation']]
x_train = pd.get_dummies(x_train)

clf = sklearn.ensemble.RandomForestClassifier(n_estimators=100, max_depth=4, random_state=42, oob_score=True, n_jobs=-1)

clf.fit(x_train, y_train.values.ravel())

y_test = test[['default']]
x_test = test[['rent', 'education', 'income', 'loan_size', 'payment_timing', 'job_stability', 'ZIP', 'occupation']]
x_test = pd.get_dummies(data=x_test)

out_sample_pred = clf.predict(x_test)

test['out_sample_pred'] = out_sample_pred
minority_default = test.out_sample_pred.groupby(test.minority).mean()

print('Question 9', '\n', '\n')
print('Default rate for minorities:', minority_default[1] * 100, '%')