text_plain_output_1.png
from sklearn.feature_extraction.text import CountVectorizer

from sklearn.metrics import log_loss

from sklearn.model_selection import StratifiedShuffleSplit

from sklearn.tree import DecisionTreeClassifier

import numpy as np

import pandas as pd

train = pd.read_json('../input/train.json')
train['listing_id'] = train['listing_id'].apply(str)

vec = CountVectorizer(stop_words='english', max_features=200)
train['features_new'] = train['features'].apply(lambda y: ' '.join(['_'.join(x.split(' ')).lower() for x in y]))
tr_sparse = vec.fit_transform(train['features_new'])
feature_names = vec.get_feature_names()

target_num_map = {'high': 0, 'medium': 1, 'low': 2}
features = tr_sparse.toarray()
labels = train['interest_level'].apply(lambda x: target_num_map[x]).as_matrix()

clf = DecisionTreeClassifier(max_depth=5)

cv = StratifiedShuffleSplit(n_splits=3, test_size=0.3)
for train_idx, test_idx in cv.split(features, labels):
    features_train, labels_train = (features[train_idx], labels[train_idx])
    features_test, labels_test = (features[test_idx], labels[test_idx])
    clf.fit(features_train, labels_train)
    print('log loss:', -1 * round(log_loss(labels_test, clf.predict_proba(features_test)), 3))
    print('high importance features:')
    for idx in np.where(clf.feature_importances_ > 0.05)[0]:
        print('  ', feature_names[idx], round(clf.feature_importances_[idx], 3))