Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
test = postprocessing(test )<count_values>
%matplotlib inline
warnings.filterwarnings("ignore" )
for column in TARGET_COLUMNS:
print(test[column].value_counts() )<prepare_output>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
sub = test[TARGET_COLUMNS].reset_index()<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
sub[ sub[TARGET_COLUMNS] > 1.0] = 1.0<load_from_csv>
train.drop("id", axis=1, inplace=True )
test = pd.read_csv(f'{DATA_DIR}/test.csv' )<define_variables>
train.drop("color", axis=1, inplace=True)
 test.drop("color", axis=1, inplace=True )
n=test['url'].apply(lambda x:(( 'ell.stackexchange.com' in x)or('english.stackexchange.com' in x)) ).tolist()
spelling=[]
for x in n:
if x:
spelling.append(0.5)
 else:
spelling.append(0.)<prepare_output>
X_train = train.drop("type", axis=1)
 columns = X_train.columns
y_train = train["type"].copy()
X_test = test.drop("id", axis=1)
 Id = test["id"].copy()
sub['question_type_spelling'] = spelling<save_to_csv>
std = StandardScaler()
X_train = std.fit_transform(X_train)
 X_test = std.transform(X_test )
sub.to_csv('submission.csv', index=False )<save_to_csv>
X_training, X_valid, y_training, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=0 )
sub.to_csv('submission.csv', index=False )<import_modules>
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
pyLDAvis.enable_notebook()
np.random.seed(2018)
 warnings.filterwarnings('ignore' )<load_from_csv>
params = {"C": [0.5, 1.0, 1.5],
"gamma": [0.01, 0.05, 0.1],
"probability": [True]}
svc_accuracy, svc_clf = modeling(params, SVC() )
sample = pd.read_csv('/kaggle/input/google-quest-challenge/sample_submission.csv')
 sample.head(3 )<load_from_csv>
params = {"C": [0.1, 1, 10],
"max_iter": [50, 100, 200]}
lr_accuracy, lr_clf = modeling(params, LogisticRegression() )
train = pd.read_csv('/kaggle/input/google-quest-challenge/train.csv')
 train.head(3 )<load_from_csv>
params = {"n_neighbors": [2, 3, 4, 5, 10, 15],
"leaf_size": [20, 30, 50],
"weights": ["uniform", "distance"],
"algorithm": ["auto", "ball_tree", "kd_tree"]}
knc_accuracy, knc_clf = modeling(params, KNeighborsClassifier() )
test = pd.read_csv('/kaggle/input/google-quest-challenge/test.csv')
 test.head(3 )<define_variables>
params = {}
gnb_accuracy, gnb_clf = modeling(params, GaussianNB() )
targets = [
'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'
] 
<feature_engineering>
params = {"C": [0.005, 0.01, 0.5, 1.0]}
lsvc_accuracy, lsvc_clf = modeling(params, LinearSVC() )
lengths = train['question_title'].apply(len)
 train['lengths'] = lengths
lengths = train.loc[train['lengths']<4000]['lengths']
sns.distplot(lengths, color='b')
 plt.show()<feature_engineering>
params = {"learning_rate": [0.01, 0.03, 0.05, 0.1],
"n_estimators": [30, 50, 100]}
gbc_accuracy, gbc_clf = modeling(params, GradientBoostingClassifier() )
stopwords=stopwords.words('english')
 train['que_stopwords']=train['question_body'].apply(lambda x : [x for x in x.split() if x in stopwords])
 train['ans_stopwords']=train['answer'].apply(lambda x: [x for x in x.split() if x in stopwords] )<count_unique_values>
accuracy = pd.DataFrame({"model": ["RandomForestClassifier", "SVC", "LogisticRegression", "KNeighborsClassifier", "GaussianNB", "LinearSVC", "GradientBoostingClassifier"],
"accuracy": [rfc_accuracy, svc_accuracy, lr_accuracy, knc_accuracy, gnb_accuracy, lsvc_accuracy, gbc_accuracy]})
 accuracy.sort_values(by="accuracy", ascending=False )
def common_ngrams(col,common=10):
corpus=[]
for question in train[col].values:
words=[str(x[0]+' '+x[1])for x in ngrams(question.split() ,2)]
corpus.append(words)
 flatten=[x for one in corpus for x in one]
counter=Counter(flatten)
 most_common=counter.most_common(common)
 string,value=zip(*(most_common))
return string,value 
<set_options>
vt_clf = VotingClassifier(estimators=[("lr", lr_clf),("svc", svc_clf),("gbc", gbc_clf)], voting="soft")
 vt_clf.fit(X_training, y_training)
 print("accuracy:", accuracy_score(vt_clf.predict(X_valid), y_valid))
np.set_printoptions(suppress=True )<load_from_csv>
lr_clf.fit(X_train, y_train)
 submission_prediction = lr_clf.predict(X_test)
 submission = pd.DataFrame({"id": Id, "type": submission_prediction})
 submission.to_csv("submission.csv", index=False )