Kaggle competition: Titanic - Machine Learning from Disaster
corrs = pd.read_csv(f'{BASE_DIR}/{QUESTION_MODEL_NAME}/corrs.csv')
 print(f'q mean(corr)={corrs["corr"].mean() :.4f}')
 corrs.head(len(QUESTION_LABELS))<load_from_csv>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier,VotingClassifier
import xgboost as xgb
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.linear_model import LassoCV
from sklearn.feature_selection import RFE
from sklearn.svm import SVC,LinearSVC
corrs = pd.read_csv(f'{BASE_DIR}/{ANSWER_MODEL_NAME}/corrs.csv')
 print(f'a mean(corr)={corrs["corr"].mean() :.4f}')
 corrs.head(len(ANSWER_LABELS))<load_from_csv>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 train.head()
hist = pd.read_csv(f'{BASE_DIR}/{QUESTION_MODEL_NAME}/history.csv')
 hist.head(len(hist))<load_from_csv>
test_id=test['PassengerId']
df=pd.concat([train,test],axis=0)
 df.head()
print(df.info() )
hist = pd.read_csv(f'{BASE_DIR}/{ANSWER_MODEL_NAME}/history.csv')
 hist.head(len(hist))<load_from_csv>
df=df.drop(['PassengerId','Cabin','Ticket'],axis=1)
 df['Age'].fillna(df['Age'].median() ,inplace=True)
 df['Fare'].fillna(df['Fare'].median() ,inplace=True)
 df['Embarked'].fillna(df['Embarked'].mode() [0],inplace=True )
%%time
test = pd.read_csv('.. /input/google-quest-challenge/test.csv')
 test.info()<load_pretrained>
df['Familysize']=df['SibSp']+df['Parch']
df['IsAlone']=1
df['IsAlone'].loc[df['Familysize']>=1]=0
df['FareBin']=pd.cut(df['Fare'],4)
 df['AgeBin']=pd.cut(df['Age'].astype(int),5)
 df['Title']=df['Name'].str.split(",",expand=True)[1].str.split('.',expand=True)[0]
min=10
title_names = df['Title'].value_counts() < min
df['Title']=df['Title'].apply(lambda x: 'Misc' if title_names[x]==True else x)
 df.head()
%%time
tokenizer = BertTokenizer.from_pretrained(f'{BASE_DIR}/{QUESTION_MODEL_NAME}')
 ds = gqc.Dataset(key_column='qa_id')
 ds.preprocess(test, tokenizer, first_seq_columns=QUESTION_FIRST_SEQUENCE,
second_seq_columns=QUESTION_SECOND_SEQUENCE,
max_sequence_length=MAX_SEQUENCE_LENGTH, window_length=WINDOW_LENGTH, stride=STRIDE)
 x_test = ds.inputs()
input_ids = pd.DataFrame(x_test[0])
 input_ids.head(10 )<predict_on_test>
label=LabelEncoder()
df['Sex_Code']=label.fit_transform(df['Sex'])
 df['Embarked_Code']=label.fit_transform(df['Embarked'])
 df['Title_Code']=label.fit_transform(df['Title'])
 df['FareBin_Code']=label.fit_transform(df['FareBin'])
 df['Age_Code']=label.fit_transform(df['AgeBin'])
 df.head()
%%time
y_pred = model.predict(x_test)
 print(f'y_pred.shape={np.shape(y_pred)}' )<create_dataframe>
df=df.drop(['Name'],axis=1)
 df=pd.get_dummies(df)
 df.head()
q = pd.DataFrame(y_pred, columns=QUESTION_LABELS)
 q['qa_id'] = ds.df['qa_id'].values
q = q.groupby(['qa_id'], as_index=False)[QUESTION_LABELS].median()
q.info()<load_pretrained>
cX=ctrain.drop(['Survived'],axis=1)
 cy=ctrain[['Survived']]
%%time
tokenizer = BertTokenizer.from_pretrained(f'{BASE_DIR}/{ANSWER_MODEL_NAME}')
 ds = gqc.Dataset(key_column='qa_id')
 ds.preprocess(test, tokenizer, first_seq_columns=ANSWER_FIRST_SEQUENCE,
second_seq_columns=ANSWER_SECOND_SEQUENCE,
max_sequence_length=MAX_SEQUENCE_LENGTH, window_length=WINDOW_LENGTH, stride=STRIDE)
 x_test = ds.inputs()
input_ids = pd.DataFrame(x_test[0])
 input_ids.head(10 )<predict_on_test>
cX_train, cX_test, cy_train, cy_test = train_test_split(cX,cy,stratify=cy,test_size=0.2,random_state=1 )
%%time
y_pred = model.predict(x_test)
 print(f'y_pred.shape={np.shape(y_pred)}' )<create_dataframe>
lcv=LassoCV()
lcv.fit(cX_train,cy_train)
 lcv_mask=lcv.coef_!=0
print(sum(lcv_mask))
a = pd.DataFrame(y_pred, columns=ANSWER_LABELS)
 a['qa_id'] = ds.df['qa_id'].values
a = a.groupby(['qa_id'], as_index=False)[ANSWER_LABELS].median()
a.info()<concatenate>
rfe_rf=RFE(estimator=RandomForestClassifier() ,n_features_to_select=12,verbose=1)
 rfe_rf.fit(cX_train,cy_train)
 rf_mask=rfe_rf.support_
qa = pd.concat([q, a], axis=1)
 qa.head()<load_from_csv>
rfe_gb=RFE(estimator=GradientBoostingClassifier() ,n_features_to_select=12,verbose=1)
 rfe_gb.fit(cX_train,cy_train)
 gb_mask=rfe_gb.support_
sub = pd.read_csv('.. /input/google-quest-challenge/sample_submission.csv')
 sub.iloc[:, 1:] = qa[QUESTION_LABELS + ANSWER_LABELS].values
gqc.check_submission(sub, shape=(476, 31), exclude={'qa_id'})
 sub.head()<save_to_csv>
votes=np.sum([lcv_mask,rf_mask,gb_mask],axis=0)
 print(votes)
 mask=votes>1
sub.to_csv('submission.csv', index=False )<save_to_csv>
lr=LogisticRegression()
lr.fit(ccX_train,cy_train)
 y_pred=lr.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
sub.to_csv('submission.csv', index=False )<install_modules>
steps=[('scaler',StandardScaler()),('lr',LogisticRegression())]
lr_pipe=Pipeline(steps)
 lr_pipe.fit(ccX_train,cy_train)
 y_pred=lr_pipe.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
!pip list<import_modules>
knn=KNeighborsClassifier(n_neighbors=9)
 knn.fit(cX_train,cy_train)
 y_pred=knn.predict(cX_test)
 print(accuracy_score(cy_test,y_pred))
pyLDAvis.enable_notebook()
warnings.filterwarnings('ignore' )<load_from_csv>
param={'knn__n_neighbors':np.arange(1,20)}
steps=[('scaler',StandardScaler()),('knn',KNeighborsClassifier())]
knn_pipe=Pipeline(steps)
 grid_knn=GridSearchCV(estimator=knn_pipe,param_grid=param,cv=10,n_jobs=-1)
 grid_knn.fit(ccX_train,cy_train)
 y_pred=grid_knn.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_knn.best_estimator_ )
sample = pd.read_csv('/kaggle/input/google-quest-challenge/sample_submission.csv')
 sample.head(3 )<load_from_csv>
param={'max_depth':np.arange(3,8),'min_samples_leaf':[0.04,0.06,0.08],'max_features':[0.2,0.4,0.6,0.8]}
dt=DecisionTreeClassifier(random_state=12)
 grid_dt=GridSearchCV(estimator=dt,param_grid=param,cv=10,n_jobs=-1)
 grid_dt.fit(ccX_train,cy_train)
 y_pred=grid_dt.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_dt.best_params_ )
train = pd.read_csv('/kaggle/input/google-quest-challenge/train.csv')
 train.head(3 )<load_from_csv>
param={'n_estimators':[200],'max_depth':np.arange(3,6),'min_samples_leaf':[0.04,0.06,0.08],'max_features':[0.2,0.4,0.6,0.8]}
rf=RandomForestClassifier(random_state=12)
 grid_rf=GridSearchCV(estimator=rf,param_grid=param,cv=10,n_jobs=-1)
 grid_rf.fit(ccX_train,cy_train)
 y_pred=grid_rf.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_rf.best_params_ )
test = pd.read_csv('/kaggle/input/google-quest-challenge/test.csv')
 test.head(3 )<define_variables>
xg_cl=xgb.XGBClassifier(objective='binary:logistic',n_estimators=4,seed=123)
 xg_cl.fit(ccX_train,cy_train)
 y_pred=xg_cl.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
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>
xg=xgb.XGBClassifier(objective='reg:logistic',seed=123)
 params={'n_estimators':[100,200],'max_depth':np.arange(2,6),'alpha':[0.01,0.1,1,10]}
grid_xg=GridSearchCV(estimator=xg,param_grid=params,cv=10,n_jobs=-1)
 grid_xg.fit(ccX_train,cy_train)
 y_pred=grid_xg.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_xg.best_params_ )
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>
dt=DecisionTreeClassifier(max_depth=1,random_state=1)
 ada=AdaBoostClassifier(base_estimator=dt,n_estimators=300,learning_rate=0.05)
 ada.fit(ccX_train,cy_train)
 y_pred=ada.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
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>
grad=GradientBoostingClassifier(n_estimators=500,learning_rate=0.01)
 grad.fit(ccX_train,cy_train)
 y_pred=grad.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
np.set_printoptions(suppress=True )<load_from_csv>
svc=SVC(C=100,random_state=12)
 svc.fit(ccX_train,cy_train)
 y_pred=svc.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
PATH = '.. /input/google-quest-challenge/'
BERT_PATH = '.. /input/bert-base-from-tfhub/bert_en_uncased_L-12_H-768_A-12'
tokenizer = FullTokenizer(BERT_PATH+'/assets/vocab.txt', True)
 MAX_SEQUENCE_LENGTH = 512
df_train = pd.read_csv(PATH+'train.csv')
 df_test = pd.read_csv(PATH+'test.csv')
 df_sub = pd.read_csv(PATH+'sample_submission.csv')
 print('train shape =', df_train.shape)
 print('test shape =', df_test.shape)
 output_categories = list(df_train.columns[11:])
 input_categories = list(df_train.columns[[1,2,5]])
 print('
output categories:
\t', output_categories)
 print('
input categories:
\t', input_categories )<string_transform>
lr=LogisticRegression(random_state=12)
 knn=KNeighborsClassifier()
dt=DecisionTreeClassifier(random_state=12)
 classifiers=[('Logistic',lr_pipe), 
('knn',grid_knn), 
('dt',grid_dt), 
('gradient',grad), 
('RF',grid_rf), 
('Ada',ada), 
('XGb',xg_cl), 
('XgbGrid',grid_xg)]
vc=VotingClassifier(estimators=classifiers)
 vc.fit(ccX_train,cy_train)
 y_pred=vc.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
def _get_masks(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 return [1]*len(tokens)+ [0] *(max_seq_length - len(tokens))
def _get_segments(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 segments = []
first_sep = True
current_segment_id = 0
for token in tokens:
segments.append(current_segment_id)
 if token == "[SEP]":
if first_sep:
first_sep = False
else:
current_segment_id = 1
return segments + [0] *(max_seq_length - len(tokens))
def _get_ids(tokens, tokenizer, max_seq_length):

 token_ids = tokenizer.convert_tokens_to_ids(tokens)
 input_ids = token_ids + [0] *(max_seq_length-len(token_ids))
return input_ids
def _trim_input(title, question, answer, max_sequence_length,
t_max_len=30, q_max_len=239, a_max_len=239):
t = tokenizer.tokenize(title)
 q = tokenizer.tokenize(question)
 a = tokenizer.tokenize(answer)
 t_len = len(t)
 q_len = len(q)
 a_len = len(a)
 if(t_len+q_len+a_len+4)> max_sequence_length:
if t_max_len > t_len:
t_new_len = t_len
a_max_len = a_max_len + floor(( t_max_len - t_len)/2)
 q_max_len = q_max_len + ceil(( t_max_len - t_len)/2)
 else:
t_new_len = t_max_len
if a_max_len > a_len:
a_new_len = a_len
q_new_len = q_max_len +(a_max_len - a_len)
 elif q_max_len > q_len:
a_new_len = a_max_len +(q_max_len - q_len)
 q_new_len = q_len
else:
a_new_len = a_max_len
q_new_len = q_max_len
if t_new_len+a_new_len+q_new_len+4 != max_sequence_length:
raise ValueError("New sequence length should be %d, but is %d"
%(max_sequence_length,(t_new_len+a_new_len+q_new_len+4)))
 t = t[:t_new_len]
q = q[:q_new_len]
a = a[:a_new_len]
return t, q, a
def _convert_to_bert_inputs(title, question, answer, tokenizer, max_sequence_length):

 stoken = ["[CLS]"] + title + ["[SEP]"] + question + ["[SEP]"] + answer + ["[SEP]"]
input_ids = _get_ids(stoken, tokenizer, max_sequence_length)
 input_masks = _get_masks(stoken, max_sequence_length)
 input_segments = _get_segments(stoken, max_sequence_length)
 return [input_ids, input_masks, input_segments]
def compute_input_arrays(df, columns, tokenizer, max_sequence_length):
input_ids, input_masks, input_segments = [], [], []
for _, instance in tqdm(df[columns].iterrows()):
t, q, a = instance.question_title, instance.question_body, instance.answer
t, q, a = _trim_input(t, q, a, max_sequence_length)
 ids, masks, segments = _convert_to_bert_inputs(t, q, a, tokenizer, max_sequence_length)
 input_ids.append(ids)
 input_masks.append(masks)
 input_segments.append(segments)
 return [np.asarray(input_ids, dtype=np.int32),
np.asarray(input_masks, dtype=np.int32),
np.asarray(input_segments, dtype=np.int32)]
def compute_output_arrays(df, columns):
return np.asarray(df[columns] )<compute_test_metric>
test_1=cctest
test_2=pd.DataFrame(test_1,columns=test_1.columns)
 ans=vc.predict(test_2)
 sub=pd.DataFrame({
'PassengerId':test_id.astype(int),
'Survived':ans.astype(int)
 })
 print(sub.head())
 sub.to_csv('submissions.csv',index=False )