import pandas as pd
%matplotlib inline

"""
Explanation: Example Usage: Titanic Dataset
An example of training a model on the titanic dataset.
The name of the package is pandas-learn, a mixing pandas into scikit-learn.  Therefore, you should always use pandas to handle your data if you are using the package(!):
End of explanation
"""


from pdlearn.ensemble import RandomForestClassifier

"""
Explanation: pandas-learn has an identical module structure to scikit-learn, so you already know where to find all the models you already use:
End of explanation
"""


data = pd.read_csv('titanic-train.csv')                   \
                 .append(pd.read_csv('titanic-test.csv')) \
                 .set_index('name')
        
data['sex'] = data.sex == 'male'
data['child'] = data.age.fillna(20) < 15
X = data[['sex', 'p_class', 'child']].astype(int)
y = data['survived']

train = y.notnull()

X.head(10)

"""
Explanation: You can use pandas to manipulate your data with ease:
End of explanation
"""


y.head(10)

"""
Explanation: (Just in case you are wondering, Mrs. Nasser was apparently 14, so was in fact a child despite being married!).
End of explanation
"""


rf = RandomForestClassifier(n_estimators=500, criterion='gini')

"""
Explanation: pandas-learn modules inherit directly from scikit learn models.  They have basically the same interface:
End of explanation
"""


rf.fit(X[train], y[train]);
print('Feature names: ', rf.feature_names_)
print('Target names:  ', rf.target_names_)

rf.predict(X[~train]).head(10)

rf.predict_proba(X[~train]).head(10)

rf.feature_importances_.plot.bar()

"""
Explanation: When you fit to pandas data, it saves the feature and target names automatically:
End of explanation
"""


