#!/usr/bin/env python
# coding: utf-8

# In[2]:


import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score


# In[3]:


data = pd.read_csv('ccdefault.csv')
data.drop('ID',axis=1,inplace=True)
data.head()


# In[4]:


# set X and y 
X = data[data.columns[:-1]]
y = data['DEFAULT']
print(X.shape)
print(y.shape)


# In[17]:


# part 1 Random forest estimators 
import time
start = time.process_time()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,stratify=y,random_state=42)
estimator_num = [1,2,10,50,100]
scores = []
time_list= []

for i in estimator_num:
    forest = RandomForestClassifier(criterion='gini',n_estimators=i,random_state=1,n_jobs=2)
    forest.fit(X_train, y_train)
    cv_scores = cross_val_score(forest,X_train,y_train,cv=10)
    scores.append(np.mean(cv_scores))
    time_list.append(end-start)
    end = time.process_time()
print(scores)
print(time_list)

    


# In[10]:


forest_best = RandomForestClassifier(criterion='gini', n_estimators=100, random_state=1, n_jobs=2)
forest_best.fit(X_train, y_train)
feat_labels = data.columns[1:]
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]

for f in range(X_train.shape[1]):
    print("%2d) %-*s %f" % (f + 1, 30, 
                            feat_labels[indices[f]], 
                            importances[indices[f]]))

plt.title('Feature Importance')
plt.bar(range(X_train.shape[1]), 
        importances[indices],
        align='center')

plt.xticks(range(X_train.shape[1]), 
           feat_labels[indices], rotation=90)
plt.xlim([-1, X_train.shape[1]])
plt.tight_layout()
plt.show()


# In[11]:


print("My name is Yuli Tang")
print("My NetID is: ytang56")
print("I hereby certify that I have read the University policy on Academic Integrity and that I am not in violation.")


# In[ ]:




