Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
<SOS> metric: categorizationaccuracy Kaggle data source: ghouls,-goblins,-and-ghosts...-boo!<load_from_csv>
LabelEncoder,
StandardScaler,
PolynomialFeatures)
 ShuffleSplit,
GridSearchCV,
cross_val_score)
 BaggingClassifier,
VotingClassifier,
RandomForestClassifier)
%%time
datadir = Path('/kaggle/input/google-quest-challenge')
 train = pd.read_csv(datadir/'train.csv')
 test = pd.read_csv(datadir/'test.csv')
 sample_submission = pd.read_csv(datadir/'sample_submission.csv' )<define_variables>
train = pandas.read_csv('.. /input/train.csv')
 test = pandas.read_csv('.. /input/test.csv')
 features_numerical = [
'bone_length',
'rotting_flesh',
'hair_length',
'has_soul'
]
features_categorical = ['color']
target = 'type'
le_target = LabelEncoder().fit(train[target])
 labels = le_target.transform(train[target])
 classes = list(le_target.classes_)
 train = train.drop([target, 'id'], axis=1)
 test_ids = test.pop('id')
 train = train.drop(features_categorical, axis=1)
 test = test.drop(features_categorical, axis=1 )
feature_columns = [col for col in train.columns if col not in sample_submission.columns]
print("Feature columns are " , feature_columns )<define_variables>
classifiers = {
'blinky': LogisticRegression(
 C=1,
tol=1e-3,
multi_class= 'multinomial',
penalty='l2',
solver='lbfgs' 
),
'pinky': RandomForestClassifier(
 n_estimators=250,
criterion='entropy',
max_depth=5,
min_samples_leaf=8,
min_samples_split=3 
),
'inky': GaussianProcessClassifier(
 kernel=1.0 * RBF(
 length_scale=1.0,
length_scale_bounds=(1e-1, 10.0)
),
warm_start=True 
),
'clyde': XGBClassifier(
 n_estimators=250,
objective="multi:softprob",
max_depth=6,
learning_rate=0.1,
gamma=0,
nthread=6 
),
}
target_cols = ['question_asker_intent_understanding',
'question_body_critical', 'question_conversational',
'question_expect_short_answer', 'question_fact_seeking',
'question_has_commonly_accepted_answer',
'question_interestingness_others', 'question_interestingness_self',
'question_multi_intent', 'question_not_really_a_question',
'question_opinion_seeking', 'question_type_choice',
'question_type_compare', 'question_type_consequence',
'question_type_definition', 'question_type_entity',
'question_type_instructions', 'question_type_procedure',
'question_type_reason_explanation', 'question_type_spelling',
'question_well_written', 'answer_helpful',
'answer_level_of_information', 'answer_plausible', 'answer_relevance',
'answer_satisfaction', 'answer_type_instructions',
'answer_type_procedure', 'answer_type_reason_explanation',
'answer_well_written']<define_variables>
clf_acc = []
ss = ShuffleSplit(
 n_splits=10,
test_size=0.2,
random_state=0 
)
 estimators = dict()
print('
Results:')
 for i,(train_index, val_index)in enumerate(ss.split(train, labels), start=1):
X_train, X_val = train.as_matrix() [train_index], train.as_matrix() [val_index]
y_train, y_val = labels[train_index], labels[val_index]
for name, clf in classifiers.items() :
pipeline = Pipeline([ 
('features', combined_features), 
(name, clf),
])
 estimator = pipeline.fit(X_train, y_train)
 estimators['%s_%s' %(name, i)] = estimator
train_predictions = estimator.predict(X_val)
 acc = metrics.accuracy_score(y_val, train_predictions)
 print('acc: {:.2%} | [split {}] {}'.format(acc, i, name))
clf_acc.append([name, i, acc])
 clf_acc = pandas.DataFrame(clf_acc, columns=['classifier', 'split', 'acc'] )
output_categories = list(train.columns[11:41])
 input_categories = list(train.columns[[1,2,5]] )<string_transform>
clf_cv_acc = clf_acc[['classifier', 'acc']] \ 
.groupby('classifier')\ 
.mean() \ 
.sort_values(by='acc', ascending=False
).reset_index()
print('
Mean classifier accuracy:
%r' %(clf_cv_acc.head(10)))
 top_estimators_acc = clf_cv_acc.head(3 ).classifier.values
vc_estimators = {k: classifiers[k] for k in top_estimators_acc}
tree_tokenizer = TreebankWordTokenizer()
def get_tree_tokens(x):
x = tree_tokenizer.tokenize(x)
 x = ' '.join(x)
 return x<feature_engineering>
ss = ShuffleSplit(
 n_splits=3,
test_size=0.1,
random_state=0 
)
 max_w = 3
weight_scores = []
w = range(1, max_w + 1)
 for weights in itertools.product(w, w, w):
if(len(set(weights)) != 1)or(int(sum(weights)) == 3):
eclf = VotingClassifier(
 estimators=vc_estimators.items() ,
voting='soft',
weights=weights 
)
 for i,(train_index, val_index)in enumerate(ss.split(train, labels), start=1):
X_train, X_val = train.as_matrix() [train_index], train.as_matrix() [val_index]
y_train, y_val = labels[train_index], labels[val_index]
estimator = eclf.fit(X_train, y_train)
 acc = metrics.accuracy_score(y_val, estimator.predict(X_val))
weight_scores.append([weights, i, acc])
 print('%s: [split %s] %s' %(weights, i, acc))
cols = ['weights', 'splits', 'acc']
df = pandas.DataFrame(weight_scores, columns=cols)
 df = df[['weights', 'acc']] \ 
.groupby('weights')\ 
.mean() \ 
.sort_values(by='acc', ascending=False
).reset_index()
print('
%r' % df.head())
 weights, _ = df.as_matrix() [0]
<import_modules><EOS> 
eclf = VotingClassifier(
 estimators=vc_estimators.items() ,
voting='soft',
weights=weights 
)
 eclf.fit(X_train, y_train)
 test_predictions = eclf.predict_proba(test.as_matrix())
 test_predictions = [le_target.classes_[numpy.argmax(i)] for i in test_predictions]
submission = pandas.DataFrame(test_predictions, columns=[target])
 submission.insert(0, 'id', test_ids)
 submission.to_csv('cherries.csv', index=False )