Kaggle competition: Titanic - Machine Learning from Disaster
df.isnull().sum()<count_missing_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv")
 train['type'] = 'train'
test['type'] = 'test'
alldata = pd.concat([train,test])
 alldata.head()
testset.isnull().sum()<data_type_conversions>
alldata['titles']=alldata['titles'].str.replace('Don','Mr')
 alldata['titles']=alldata['titles'].str.replace('Rev','Mr')
 alldata['titles']=alldata['titles'].str.replace('Dr','Mr')
 alldata['titles']=alldata['titles'].str.replace('Capt','Mr')
 alldata['titles']=alldata['titles'].str.replace('Jonkheer','Mr')
 alldata['titles']=alldata['titles'].str.replace('Lady','Mrs')
 alldata['titles']=alldata['titles'].str.replace('Mme','Mrs')
 alldata['titles']=alldata['titles'].str.replace('Ms','Mrs')
 alldata['titles']=alldata['titles'].str.replace('Mlle','Mrs')
 alldata['titles']=alldata['titles'].str.replace('the Countess','Mrs')
 alldata['titles']=alldata['titles'].str.replace('Sir','Master')
 alldata['titles']=alldata['titles'].str.replace('Major','Master')
 alldata['titles']=alldata['titles'].str.replace('Col','Master' )
df.ingredients = df.ingredients.astype('str')
 testset.ingredients = testset.ingredients.astype('str' )<define_variables>
alldata['titles'].value_counts()
df.ingredients = df.ingredients.str.replace("["," ")
 df.ingredients = df.ingredients.str.replace("]"," ")
 df.ingredients = df.ingredients.str.replace("'"," ")
 df.ingredients = df.ingredients.str.replace(","," " )<feature_engineering>
alldata.drop('Name',axis=1,inplace=True )
testset.ingredients = testset.ingredients.str.replace("["," ")
 testset.ingredients = testset.ingredients.str.replace("]"," ")
 testset.ingredients = testset.ingredients.str.replace("'"," ")
 testset.ingredients = testset.ingredients.str.replace(","," " )<string_transform>
alldata.drop('Cabin',axis=1,inplace=True )
df.ingredients = df.ingredients.apply(lambda x: word_tokenize(x))
testset.ingredients = testset.ingredients.apply(lambda x: word_tokenize(x))<choose_model_class>
alldata.isnull().sum()
lemmatizer = WordNetLemmatizer()<normalization>
alldata['Family_size'] = alldata['SibSp'] + alldata['Parch']
def lemmat(wor):
l = []
for i in wor:
l.append(lemmatizer.lemmatize(i))
return l<feature_engineering>
agegroups = alldata['Age']
agegroups
groups = []
for ages in agegroups:
if ages < 18:
groups.append('Child')
 else:
groups.append('Adult')
 groups = pd.DataFrame(groups,columns=['Child'])
 len(groups)
df.ingredients = df.ingredients.apply(lemmat)
 testset.ingredients = testset.ingredients.apply(lemmat )<data_type_conversions>
alldata['Child'] = groups
alldata.head()
alldata['Child'] = pd.get_dummies(alldata['Child'],drop_first=True)
 alldata.head()
df.ingredients = df.ingredients.astype('str')
 df.ingredients = df.ingredients.str.replace("["," ")
 df.ingredients = df.ingredients.str.replace("]"," ")
 df.ingredients = df.ingredients.str.replace("'"," ")
 df.ingredients = df.ingredients.str.replace(","," " )<data_type_conversions>
alldata['titles'] = pd.Categorical(alldata['titles'])
testset.ingredients = testset.ingredients.astype('str')
 testset.ingredients = testset.ingredients.str.replace("["," ")
 testset.ingredients = testset.ingredients.str.replace("]"," ")
 testset.ingredients = testset.ingredients.str.replace("'"," ")
 testset.ingredients = testset.ingredients.str.replace(","," " )<feature_engineering>
alldata['titles'] = alldata['titles'].cat.codes
vect = TfidfVectorizer()<feature_engineering>
alldata.drop('titles1',axis=1 )
features = vect.fit_transform(df.ingredients )<categorify>
alldata.loc[alldata['Sex']=='male','Embarked'].value_counts()
testfeatures = vect.transform(testset.ingredients )<categorify>
alldata.loc[alldata['Sex']=='female','Embarked'].value_counts()
encoder = LabelEncoder()
labels = encoder.fit_transform(df.cuisine )<split>
alldata.loc[alldata['Pclass']==1,'Embarked'].value_counts()
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2 )<compute_test_metric>
alldata.loc[alldata['Pclass']==2,'Embarked'].value_counts()
<choose_model_class>
alldata.loc[alldata['Pclass']==3,'Embarked'].value_counts()
<compute_test_metric>
alldata['Embarked']=alldata['Embarked'].fillna('S' )
<compute_test_metric>
alldata.isnull().sum()
<split>
pclass_list = list(alldata['Pclass'].unique())
 df_averages = []
for classes in pclass_list:
df_averages.append(( alldata.loc[alldata['Pclass']==classes]['Age'].mean()))
averages = pd.DataFrame(df_averages,index=pclass_list,columns=['Average age in class'])
 averages
<import_modules>
pclass_list = list(alldata['Pclass'].unique())
 df_fares = []
for fares in pclass_list:
df_fares.append(( alldata.loc[alldata['Pclass']==fares]['Fare'].median()))
averages2 = pd.DataFrame(df_fares,index=pclass_list,columns=['Average fare in class'])
 averages2
<compute_test_metric>
alldata.loc[(alldata['Age'].isnull())&(alldata['Pclass']== 1),'Age'] = averages.loc[1,'Average age in class']
alldata.loc[(alldata['Age'].isnull())&(alldata['Pclass']== 2),'Age'] = averages.loc[2,'Average age in class']
alldata.loc[(alldata['Age'].isnull())&(alldata['Pclass']== 3),'Age'] = averages.loc[3,'Average age in class']
alldata.loc[(alldata['Fare'].isnull())&(alldata['Pclass']== 1),'Fare'] = averages2.loc[1,'Average fare in class']
alldata.loc[(alldata['Fare'].isnull())&(alldata['Pclass']== 2),'Fare'] = averages2.loc[2,'Average fare in class']
alldata.loc[(alldata['Fare'].isnull())&(alldata['Pclass']== 3),'Fare'] = averages2.loc[3,'Average fare in class']
alldata.isnull().sum()
<import_modules>
farebandlist = list(alldata['FareBand'].unique())
 farelist =[]
for fares in farebandlist:
farelist.append(alldata.loc[(alldata['type']=='train')&(alldata['FareBand']== fares),'Survived'].value_counts())
 farelist = pd.DataFrame(farelist,index=farebandlist)
 farelist.columns
import lightgbm as lgb<train_model>
farelist[[0.0, 1.0]] = farelist[[0.0, 1.0]].apply(lambda x: x/x.sum() , axis=1)
 farelist
gbm = lgb.LGBMClassifier(objective="mutliclass",n_estimators=10000,num_leaves=512)
 gbm.fit(X_train,y_train,verbose = 300 )<predict_on_test>
alldata['FareBand'] = alldata['FareBand'].astype(np.int64)
 alldata.info()
pred = gbm.predict(testfeatures )<categorify>
sex1 = pd.get_dummies(alldata['Sex'],drop_first=True)
 embarked1 = pd.get_dummies(alldata['Embarked'],drop_first=True)
 alldata.drop(['Sex','Embarked'],axis=1,inplace=True)
predconv = encoder.inverse_transform(pred )<create_dataframe>
alldata = pd.concat([alldata,sex1,embarked1],axis=1 )
sub = pd.DataFrame({'id':testset.id,'cuisine':predconv} )<define_variables>
alldata.drop(['Ticket','Fare','Age'],axis=1,inplace=True )
output = sub[['id','cuisine']]<save_to_csv>
train = alldata.loc[alldata['type']=='train']
test = alldata.loc[alldata['type']=='test']
output.to_csv("outputfile.csv",index = False )<import_modules>
train = train.drop(['type'],axis=1)
%matplotlib inline
init_notebook_mode(connected=True)
 warnings.filterwarnings("ignore")
 notebookstart= time.time()<load_from_disk>
train.isnull().sum()
train_df = pd.read_json('.. /input/train.json')
 test_df = pd.read_json('.. /input/test.json')
 train=train_df
train.head(15 )<sort_values>
train.isnull().sum()
train=train_df
total = train.isnull().sum().sort_values(ascending = False)
 percent =(train.isnull().sum() /train.isnull().count() *100 ).sort_values(ascending = False)
 missing_train_data = pd.concat([total, percent], axis=1, keys=['Total missing', 'Percent missing'])
 print("
print(missing_train_data.head() )<categorify>
test = test.drop(['type'],axis=1 )
train_df['seperated_ingredients'] = train_df['ingredients'].apply(','.join)
 test_df['seperated_ingredients'] = test_df['ingredients'].apply(','.join)
 train_df['for ngrams']=train_df['seperated_ingredients'].str.replace(',',' ')
 def generate_ngrams(text, n):
words = text.split(' ')
 iterations = len(words)- n + 1
for i in range(iterations):
yield words[i:i + n]
def net_diagram(*cuisines):
ngrams = {}
for title in train_df[train_df.cuisine==cuisines[0]]['for ngrams']:
for ngram in generate_ngrams(title, 2):
ngram = ','.join(ngram)
 if ngram in ngrams:
ngrams[ngram] += 1
else:
ngrams[ngram] = 1
ngrams_mws_df = pd.DataFrame.from_dict(ngrams, orient='index')
 ngrams_mws_df.columns = ['count']
ngrams_mws_df['cusine'] = cuisines[0]
ngrams_mws_df.reset_index(level=0, inplace=True)
 ngrams = {}
for title in train_df[train_df.cuisine==cuisines[1]]['for ngrams']:
for ngram in generate_ngrams(title, 2):
ngram = ','.join(ngram)
 if ngram in ngrams:
ngrams[ngram] += 1
else:
ngrams[ngram] = 1
ngrams_mws_df1 = pd.DataFrame.from_dict(ngrams, orient='index')
 ngrams_mws_df1.columns = ['count']
ngrams_mws_df1['cusine'] = cuisines[1]
ngrams_mws_df1.reset_index(level=0, inplace=True)
 cuisine1=ngrams_mws_df.sort_values('count',ascending=False)[:25]
cuisine2=ngrams_mws_df1.sort_values('count',ascending=False)[:25]
df_final=pd.concat([cuisine1,cuisine2])
 g = nx.from_pandas_dataframe(df_final,source='cusine',target='index')
 cmap = plt.cm.RdYlGn
colors = [n for n in range(len(g.nodes())) ]
k = 0.35
pos=nx.spring_layout(g, k=k)
 nx.draw_networkx(g,pos, node_size=df_final['count'].values*8, cmap = cmap, node_color=colors, edge_color='grey', font_size=15, width=3)
 plt.title("Top 25 Bigrams for %s and %s" %(cuisines[0],cuisines[1]), fontsize=30)
 plt.gcf().set_size_inches(30,30)
 plt.show()
plt.savefig('network.png')
 net_diagram('french','cajun_creole' )<prepare_x_and_y>
test.isnull().sum()
df = pd.read_json('.. /input/train.json' ).set_index('id')
 test_df = pd.read_json('.. /input/test.json' ).set_index('id')
 traindex = df.index
testdex = test_df.index
y = df.cuisine.copy()
df = pd.concat([df.drop("cuisine", axis=1), test_df], axis=0)
 df_index = df.index
del test_df; gc.collect() ;
vect = CountVectorizer()
dummies = vect.fit_transform(df.ingredients.str.join(' '))
df = pd.DataFrame(dummies.todense() ,columns=vect.get_feature_names())
 df.index= df_index
X = df.loc[traindex,:]
test_df = df.loc[testdex,:]
del df; gc.collect() ;<load_from_csv>
test.isnull().sum()
def read_dataset(path):
return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json')
 def generate_text(data):
text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
return text_data
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]
tfidf = TfidfVectorizer(binary=True)
 def tfidf_features(txt, flag):
if flag == "train":
x = tfidf.fit_transform(txt)
 else:
x = tfidf.transform(txt)
 x = x.astype('float16')
 return x
X2 = tfidf_features(train_text, flag="train")
 X_test3 = tfidf_features(test_text, flag="test")
 lb = LabelEncoder()
y2 = lb.fit_transform(target )<train_on_grid>
X = train.drop(['Survived','PassengerId'],axis=1)
 y = train['Survived']
def compareAccuracy(a, b):
print('
Compare Multiple Classifiers:
')
 print('K-Fold Cross-Validation Accuracy:
')
 names = []
models = []
resultsAccuracy = []
models.append(( 'LR', LogisticRegression()))
models.append(( 'LSVM', LinearSVC()))
models.append(( 'RF', RandomForestClassifier()))
for name, model in models:
model.fit(a, b)
 kfold = model_selection.KFold(n_splits=10, random_state=7)
 accuracy_results = model_selection.cross_val_score(model, a,b, cv=kfold, scoring='accuracy')
 resultsAccuracy.append(accuracy_results)
 names.append(name)
 accuracyMessage = "%s: %f(%f)" %(name, accuracy_results.mean() , accuracy_results.std())
 print(accuracyMessage)
 fig = plt.figure()
fig.suptitle('Algorithm Comparison: Accuracy')
 ax = fig.add_subplot(111)
 plt.boxplot(resultsAccuracy)
 ax.set_xticklabels(names)
 ax.set_ylabel('Cross-Validation: Accuracy Score')
 plt.show()
def defineModels() :
print('
LR = LogisticRegression')
 print('LSVM = LinearSVM')
 print('RF = RandomForestClassifier' )<compute_test_metric>
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size =
0.25, random_state = 0 )
compareAccuracy(X2,y2)
 defineModels()<save_to_csv>
from sklearn import preprocessing
model = LinearSVC()
model.fit(X, y)
 submission = model.predict(test_df)
 submission_df = pd.Series(submission, index=testdex ).rename('cuisine')
 submission_df.to_csv("recipe_submission.csv", index=True, header=True)
 model.fit(X2, y2)
 y_test3 = model.predict(X_test3)
 y_pred = lb.inverse_transform(y_test3)
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('recipe_submission2.csv', index=False )<set_options>
scaler = preprocessing.StandardScaler().fit(x_train )
os.environ['PYTHONHASHSEED'] = '10000'
np.random.seed(10001)
 random.seed(10002)
 tf.set_random_seed(10003)
 wordnet_lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words('english')) 
<define_variables>
X_scaled = scaler.transform(x_train )
path = '.. /input/'
embedding_path = '.. /input/embeddings/'
cores = 4
max_text_length=50
do_submission = False
min_df_one=1
keep_only_words_in_embedding = True
keep_unknown_words_in_keras_sequence_as_zeros = True
contraction_mapping = {u"ain't": u"is not", u"aren't": u"are not",u"can't": u"cannot", u"'cause": u"because",
u"could've": u"could have", u"couldn't": u"could not", u"didn't": u"did not",
u"doesn't": u"does not", u"don't": u"do not", u"hadn't": u"had not",
u"hasn't": u"has not", u"haven't": u"have not", u"he'd": u"he would",
u"he'll": u"he will", u"he's": u"he is", u"how'd": u"how did", u"how'd'y": u"how do you",
u"how'll": u"how will", u"how's": u"how is", u"I'd": u"I would",
u"I'd've": u"I would have", u"I'll": u"I will", u"I'll've": u"I will have",
u"I'm": u"I am", u"I've": u"I have", u"i'd": u"i would", u"i'd've": u"i would have",
u"i'll": u"i will", u"i'll've": u"i will have",u"i'm": u"i am", u"i've": u"i have",
u"isn't": u"is not", u"it'd": u"it would", u"it'd've": u"it would have",
u"it'll": u"it will", u"it'll've": u"it will have",u"it's": u"it is",
u"let's": u"let us", u"ma'am": u"madam", u"mayn't": u"may not",
u"might've": u"might have",u"mightn't": u"might not",u"mightn't've": u"might not have",
u"must've": u"must have", u"mustn't": u"must not", u"mustn't've": u"must not have",
u"needn't": u"need not", u"needn't've": u"need not have",u"o'clock": u"of the clock",
u"oughtn't": u"ought not", u"oughtn't've": u"ought not have", u"shan't": u"shall not",
u"sha'n't": u"shall not", u"shan't've": u"shall not have", u"she'd": u"she would",
u"she'd've": u"she would have", u"she'll": u"she will", u"she'll've": u"she will have",
u"she's": u"she is", u"should've": u"should have", u"shouldn't": u"should not",
u"shouldn't've": u"should not have", u"so've": u"so have",u"so's": u"so as",
u"this's": u"this is",u"that'd": u"that would", u"that'd've": u"that would have",
u"that's": u"that is", u"there'd": u"there would", u"there'd've": u"there would have",
u"there's": u"there is", u"here's": u"here is",u"they'd": u"they would",
u"they'd've": u"they would have", u"they'll": u"they will",
u"they'll've": u"they will have", u"they're": u"they are", u"they've": u"they have",
u"to've": u"to have", u"wasn't": u"was not", u"we'd": u"we would",
u"we'd've": u"we would have", u"we'll": u"we will", u"we'll've": u"we will have",
u"we're": u"we are", u"we've": u"we have", u"weren't": u"were not",
u"what'll": u"what will", u"what'll've": u"what will have", u"what're": u"what are",
u"what's": u"what is", u"what've": u"what have", u"when's": u"when is",
u"when've": u"when have", u"where'd": u"where did", u"where's": u"where is",
u"where've": u"where have", u"who'll": u"who will", u"who'll've": u"who will have",
u"who's": u"who is", u"who've": u"who have", u"why's": u"why is", u"why've": u"why have",
u"will've": u"will have", u"won't": u"will not", u"won't've": u"will not have",
u"would've": u"would have", u"wouldn't": u"would not", u"wouldn't've": u"would not have",
u"y'all": u"you all", u"y'all'd": u"you all would",u"y'all'd've": u"you all would have",
u"y'all're": u"you all are",u"y'all've": u"you all have",u"you'd": u"you would",
u"you'd've": u"you would have", u"you'll": u"you will", u"you'll've": u"you will have",
u"you're": u"you are", u"you've": u"you have" }<string_transform>
scaler = preprocessing.StandardScaler().fit(x_test )
def load_glove_words() :
EMBEDDING_FILE = embedding_path+'glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, 1
embeddings_index1 = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
EMBEDDING_FILE = embedding_path+'paragram_300_sl999/paragram_300_sl999.txt'
embeddings_index2 = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding='utf-8', errors='ignore'))
return(set(list(embeddings_index1.keys())) ).union(set(list(embeddings_index2.keys())))
 embeddings_index = load_glove_words()
print(len(embeddings_index)) 
<string_transform>
X_testscaled = scaler.transform(x_test )
def clean_str(text):
text = re.sub(u"\[math\].*\[\/math\]", u" math ", text)
 text = re.sub(u"\S*@\S*\.\S*", u" email ", text)
 text = u" ".join(re.sub(u"^\d+(?:[.,]\d*)?$", u"number", w)for w in text.split(" "))
specials = [u"’", u"‘", u"´", u"`", u"\u2019"]
for s in specials:
text = u" ".join(w.replace(s, u"'")for w in text.split(" "))
text = u" ".join([w.strip() if w in embeddings_index else clean(w ).strip() for w in text.split(" ")])
 text = re.sub(u"\s+", u" ", text ).strip()
text = u" ".join([w.strip() for w in text.split(" ")if w in embeddings_index ])
 return text
def clean(text):
try:
if text in contraction_mapping:
return contraction_mapping[text]
for i, j in [(u"é", u"e"),(u"ē", u"e"),(u"è", u"e"),(u"ê", u"e"),(u"à", u"a"), 
(u"â", u"a"),(u"ô", u"o"),(u"ō", u"o"),(u"ü", u"u"),(u"ï", u"i"), 
(u"ç", u"c"),(u"\xed", u"i")]:
text = re.sub(i, j, text)
 if text in embeddings_index:
return text
text = text.lower()
if text in embeddings_index:
return text
text = re.sub(u"[^a-z\s0-9]", u" ", text)
 text = u" ".join(re.sub(u"^\d+(?:[.,]\d*)?$", u"number", w)for w in text.split(" "))
text = re.sub(u"[^a-z\s]", u" ", text)
 text = re.sub(u"\s+", u" ", text ).strip()
text = u" ".join([ wordnet_lemmatizer.lemmatize(w)if w not in embeddings_index else w for w in text.split() ])
 except:
print('ERROR')
 text = ''
return text
def parallelize_dataframe(df, func):
df_split = np.array_split(df, cores)
 pool = Pool(cores)
 df = pd.concat(pool.map(func, df_split))
pool.close()
pool.join()
return df
def clean_str_df(df):
return df.apply(lambda s : clean_str(s))
def prepare_data(df_data, train=True):
print('Prepare data.... ')
 df_data['question_text'] = parallelize_dataframe(df_data['question_text'], clean_str_df)
 return df_data
def create_vocabulary(df):
start_time = time.time()
word_frequency_dc=defaultdict(np.uint32)
 def word_count(text):
text = set(text.split(' '))
if keep_only_words_in_embedding:
for w in text:
if w in embeddings_index:
word_frequency_dc[w]+=1
else:
for w in text:
word_frequency_dc[w]+=1
df['question_text'].apply(lambda x : word_count(x))
print('[{}] Finished COUNTING WORDS FOR question_text...'.format(time.time() - start_time))
start_time = time.time()
vocabulary_dc = word_frequency_dc.copy()
cpt=1
for key in vocabulary_dc:
vocabulary_dc[key]=cpt
cpt+=1
print('[{}] Finished CREATING VOCABULARY...'.format(time.time() - start_time))
return word_frequency_dc, vocabulary_dc
def tokenize(text):
return [w for w in text.split() ]
def load_glove(word_index):
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 EMBEDDING_FILE = embedding_path+'glove.840B.300d/glove.840B.300d.txt'
embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean1,emb_std1 = all_embs.mean() , all_embs.std()
EMBEDDING_FILE = embedding_path+'paragram_300_sl999/paragram_300_sl999.txt'
embeddings_index2 = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding='utf-8', errors='ignore'))
all_embs = np.stack(embeddings_index2.values())
 emb_mean2,emb_std2 = all_embs.mean() , all_embs.std()
embedding_matrix = np.zeros(( len(word_index)+2, 300), dtype=np.float32)
 for word, i in word_index.items() :
embedding_vector = None
if(word in embeddings_index)&(word in embeddings_index2):
embedding_vector =(embeddings_index.get(word)+(embeddings_index2.get(word)-emb_mean2+emb_mean1)) /2.
 else:
if(word in embeddings_index):
embedding_vector = embeddings_index.get(word)
 else:
if(word in embeddings_index2):
embedding_vector = embeddings_index2.get(word)-emb_mean2+emb_mean1
if embedding_vector is not None: embedding_matrix[i] = embedding_vector.astype(np.float32)
 return embedding_matrix
def preprocess_keras(text):
if keep_unknown_words_in_keras_sequence_as_zeros:
return [ vocabulary_dc[w] if(( w in embeddings_index)&(word_frequency_dc[w]>=min_df_one)) else 0 for w in(text.split())]
else:
return [ vocabulary_dc[w] for w in(text.split())if w in embeddings_index ]
def preprocess_keras_df(df):
return df.apply(preprocess_keras)
 def get_keras_data(df):
X = {
'num_data' : num_FE(df),
'question_text_in_glove': pad_sequences(df['seq_question_text_in_glove'], maxlen=max_text_length, value=0, padding='post'),
'question_text_mask_in_glove':(pad_sequences(df['seq_question_text_in_glove'], maxlen=max_text_length, value=0, padding='post')>0 ).astype(int ).reshape(-1,max_text_length,1),
'starting_words': pad_sequences(df['seq_question_text_in_glove'].apply(lambda x : x[:10]), maxlen=10, value=0, padding='post'),
'ending_words': pad_sequences(df['seq_question_text_in_glove'].apply(lambda x : x[-10:]), maxlen=10, value=0, padding='post')
 }
return X
def num_FE(temp_df):
df = pd.DataFrame()
df['total_length'] = temp_df['question_text_original'].apply(len)
 df['capitals'] = temp_df['question_text_original'].apply(lambda comment: sum(1 for c in comment if c.isupper()))
df['caps_vs_length'] = df.apply(lambda row: float(row['capitals'])/float(row['total_length']),
axis=1)
 df['num_exclamation_marks'] = temp_df['question_text_original'].apply(lambda comment: comment.count('!'))
df['num_question_marks'] = temp_df['question_text_original'].apply(lambda comment: comment.count('?'))
df['num_punctuation'] = temp_df['question_text_original'].apply(
 lambda comment: sum(comment.count(w)for w in '.,;:'))
df['num_symbols'] = temp_df['question_text_original'].apply(
 lambda comment: sum(comment.count(w)for w in '*
df['num_words'] = temp_df['question_text_original'].apply(lambda comment: len(comment.split()))
df['num_unique_words'] = temp_df['question_text_original'].apply(
 lambda comment: len(set(w for w in comment.split())))
 df['words_vs_unique'] = df['num_unique_words'] / df['num_words']
return df.values.astype(np.float32 )<compute_train_metric>
params_to_test = {
'n_estimators':[50,100,150,170,180,190,200,210],
'max_depth':[3,5,6]
}
rf_model = RandomForestClassifier(random_state=42)
 grid_search = GridSearchCV(rf_model, param_grid=params_to_test, cv=10, scoring='f1_macro', n_jobs=4)
 grid_search.fit(X_scaled, y_train)
 best_params = grid_search.best_params_
best_model = RandomForestClassifier(**best_params )
def threshold_search(y_true, y_proba):
best_threshold = 0
best_score = 0
for threshold in [i * 0.01 for i in range(10,70)]:
score = f1_score(y_true=y_true, y_pred=y_proba > threshold)
 if score > best_score:
best_threshold = threshold
best_score = score
search_result = {'threshold': best_threshold, 'f1': best_score}
return search_result
class My_Callback(Callback):
def __init__(self, save_path, factor=0.5, patience_reduce=10, patience_stop=30, min_lr=1e-4):
self.scores_list = []
self.scores_list_stop = []
self.thresh_list = []
self.all_preds = None
self.best_score = 0.
 self.best_threshold = 0.5
self.best_epoch = 0
self.best_lr = 0
self.save_path = save_path
self.factor = factor
self.min_lr = min_lr
self.patience_reduce = patience_reduce
self.patience_stop = patience_stop
def on_epoch_end(self, epoch, logs={}):
all_preds = self.all_preds
ep = epoch
best_score = self.best_score
preds_valid = self.model.predict(test_keras , batch_size=2048)
 thresholds = threshold_search(dtest_y, preds_valid)
 best_thres = thresholds['threshold']
score_all = thresholds['f1']
print(ep+1, ' F1 : ', "{0:.4f}".format(score_all), "{0:.4f}".format(best_thres),
' AUC : ', "{0:.4f}".format(roc_auc_score(dtest_y, preds_valid>=best_thres)))
 self.scores_list.append(score_all)
 self.thresh_list.append(best_thres)
 self.scores_list_stop.append(score_all)
 if score_all > best_score:
self.best_score = score_all
self.best_threshold = best_thres
self.best_epoch = ep+1
self.best_lr = np.float32(K.get_value(self.model.optimizer.lr))
self.model.save_weights(self.save_path, overwrite=True)
 print("Score improved from ", "{0:.4f}".format(best_score), ' to ', "{0:.4f}".format(score_all),\
'threshold : ', "{0:.4f}".format(self.best_threshold), ' lr : ', self.best_lr, ' epoch : ', self.best_epoch)
 else:
print("Score didnt improve, current best score is : ", "{0:.4f}".format(best_score),\
'with threshold : ', "{0:.4f}".format(self.best_threshold), ' lr : ', self.best_lr, ' epoch : ', self.best_epoch)
 pos = np.argmax(self.scores_list)
 if len(self.scores_list)- pos>self.patience_reduce:
old_lr = np.float32(K.get_value(self.model.optimizer.lr))
if old_lr > self.min_lr:
new_lr = old_lr * self.factor
new_lr = max(new_lr, self.min_lr)
 self.model.load_weights(self.save_path)
 K.set_value(self.model.optimizer.lr, new_lr)
 self.scores_list = self.scores_list[:pos+1]
self.thresh_list = self.thresh_list[:pos+1]
print('*'*70)
 print('Reducing LR from ', old_lr, ' to ', new_lr)
 print('Loading Last best score : ', self.scores_list[-1], 'with threshold : ',self.thresh_list[-1])
 print('*'*70)
 pos = np.argmax(self.scores_list_stop)
 if len(self.scores_list_stop)- pos>self.patience_stop:
print('TRAINING STOP, NO MORE IMPROVEMENT')
 print('LOADING BEST WEIGHTS.... ')
 self.model.load_weights(self.save_path)
 self.stopped_epoch = ep
self.model.stop_training = True
print('')
 return
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, data, labels, shuffle=True, seed=None):
self.epoch = 0
self.seed = seed
if self.seed is None:
self.seed = self.epoch
np.random.seed(self.seed)
 self.list_IDs = np.random.permutation(len(labels))
self.data_num = data['num_data']
self.data_start = data['starting_words']
self.data_end = data['ending_words']
self.data_glove = data['question_text_in_glove']
self.data_mask = data['question_text_mask_in_glove']
self.labels = labels
self.shuffle = shuffle
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs)/ batch_size))
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.indexes[index*batch_size:(index+1)*batch_size]
list_IDs_temp = [self.list_IDs[k] for k in indexes]
X, y = self.__data_generation(list_IDs_temp)
 return(X, y)
 def on_epoch_end(self):
'Updates indexes after each epoch'
self.epoch += 1
self.seed += 1
np.random.seed(self.seed)
 self.list_IDs = np.random.permutation(len(self.labels))
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
 def __data_generation(self, list_IDs_temp):
X = {
'num_data' : self.data_num[list_IDs_temp],
'starting_words': self.data_start[list_IDs_temp],
'ending_words': self.data_end[list_IDs_temp],
'question_text_in_glove': self.data_glove[list_IDs_temp],
'question_text_mask_in_glove': self.data_mask[list_IDs_temp].reshape(-1,max_text_length,1),
}
return X , self.labels[list_IDs_temp]
def new_rnn_model() :
sequence_in_glove = Input(shape=[train_keras["question_text_in_glove"].shape[1]], name="question_text_in_glove")
 mask_in_glove = Input(shape=[train_keras["question_text_mask_in_glove"].shape[1],1], name="question_text_mask_in_glove")
 starting_words = Input(shape=[train_keras["starting_words"].shape[1]], name="starting_words")
 ending_words = Input(shape=[train_keras["ending_words"].shape[1]], name="ending_words")
 num_data = Input(shape=[train_keras["num_data"].shape[1]], name="num_data")
 num_data_normed = BatchNormalization()(num_data)
 shared_embedding = Embedding(glove_weights.shape[0], glove_weights.shape[1], weights=[glove_weights], trainable=False, name='emb')
 emb_sequence_in_glove = shared_embedding(sequence_in_glove)
 emb_sequence_in_glove = multiply([emb_sequence_in_glove, mask_in_glove])
 emb_sequence_in_glove = SpatialDropout1D(0.1 )(emb_sequence_in_glove)
 shared_embedding2 = Embedding(glove_weights.shape[0], 20, trainable=True, name='emb2')
 emb_sequence_in_glove2 = shared_embedding(sequence_in_glove)
 emb_sequence_in_glove2 = multiply([emb_sequence_in_glove2, mask_in_glove])
 emb_sequence_in_glove2 = SpatialDropout1D(0.2 )(emb_sequence_in_glove2)
 lstm2 = CuDNNLSTM(64, return_sequences=True )(emb_sequence_in_glove2)
 lstm2 = SpatialDropout1D(0.2 )(lstm2)
 max_lstm2 = GlobalMaxPooling1D()(lstm2)
 avg_lstm2 = Lambda(lambda x : K.sum(x, axis=1))(lstm2)
 avg_lstm2 = BatchNormalization()(avg_lstm2)
 starting_emb = shared_embedding(starting_words)
 ending_emb = shared_embedding(ending_words)
 avg_emb_start = Lambda(lambda x : K.sum(x, axis=1))(starting_emb)
 avg_emb_start = BatchNormalization()(avg_emb_start)
 avg_emb_end = Lambda(lambda x : K.sum(x, axis=1))(ending_emb)
 avg_emb_end = BatchNormalization()(avg_emb_end)
 avg_emb = Lambda(lambda x : K.sum(x, axis=1))(emb_sequence_in_glove)
 avg_emb = BatchNormalization()(avg_emb)
 lstm = CuDNNLSTM(128, return_sequences=True )(emb_sequence_in_glove)
 lstm = SpatialDropout1D(0.1 )(lstm)
 max_lstm = GlobalMaxPooling1D()(lstm)
 avg_lstm = Lambda(lambda x : K.sum(x, axis=1))(lstm)
 avg_lstm = BatchNormalization()(avg_lstm)
 main_l = concatenate([ max_lstm, avg_lstm, num_data_normed, avg_emb])
 main_l = Dense(196 )(main_l)
 main_l = Activation('relu' )(main_l)
 main_l = Dense(64 )(main_l)
 main_l = Activation('relu' )(main_l)
 output = Dense(1, activation="sigmoid" )(main_l)
 model = Model([sequence_in_glove, mask_in_glove, starting_words, ending_words, num_data], output)
 return model<load_from_csv>
best_model.fit(X_scaled,y_train )
df_sub = pd.read_csv(path+'test.csv', encoding='utf-8', engine='python')
 df_sub['target'] = -99
df_sub['question_text'].fillna(u'unknownstring', inplace=True)
 df_train_all = pd.read_csv(path+'train.csv', encoding='utf-8', engine='python')
 df_train_all['question_text'].fillna(u'unknownstring', inplace=True)
 df_sub['question_text_original'] = df_sub['question_text'].copy()
df_sub = prepare_data(df_sub)
 df_train_all['question_text_original'] = df_train_all['question_text'].copy()
df_train_all = prepare_data(df_train_all)
 word_frequency_dc, vocabulary_dc = create_vocabulary(df_train_all[['question_text']].append(df_sub[['question_text']]))
print(len(vocabulary_dc))
glove_weights = load_glove(vocabulary_dc)
 print(glove_weights.shape )<create_dataframe>
betterpred = best_model.predict(X_testscaled )
df_train_all['seq_question_text_in_glove'] = parallelize_dataframe(df_train_all['question_text'], preprocess_keras_df)
 train_keras = get_keras_data(df_train_all)
 df_sub['seq_question_text_in_glove'] = parallelize_dataframe(df_sub['question_text'], preprocess_keras_df)
 sub_keras = get_keras_data(df_sub)
<train_model>
from sklearn.metrics import accuracy_score
scale = 1.
 BATCH_SIZE = int(scale*1024)
 lr1 = scale*2e-3
lr2 = scale*1e-3
batch_size = BATCH_SIZE
epochs = 5
save_model_name='./model32.h5'
all_preds_sub = [ ]
print("Fitting RNN model...")
 for bag in range(7):
train_generator = DataGenerator(train_keras, df_train_all['target'].values, shuffle=True, seed=bag)
 rnn_model = new_rnn_model()
rnn_model.get_layer('emb' ).trainable=False
optimizer = Adam(lr=lr1)
 rnn_model.compile(loss="binary_crossentropy", optimizer=optimizer)
 rnn_model.fit_generator(generator=train_generator, workers=3, verbose=2, epochs=epochs)
 finetune=True
if finetune:
rnn_model.get_layer('emb' ).trainable=True
optimizer = Adam(lr=lr2)
 rnn_model.compile(loss="binary_crossentropy", optimizer=optimizer)
 rnn_model.fit_generator(generator=train_generator, workers=3, verbose=2,epochs=1)
 preds_sub = rnn_model.predict(sub_keras , batch_size=2048 ).squeeze()
all_preds_sub.append(preds_sub)
 del rnn_model
gc.collect()
print('*******************************************************')
<save_to_csv>
predacc = round(accuracy_score(betterpred, y_test)* 100, 2)
 print(predacc,'%' )
subThreshold = 0.33
mean_preds = np.array(all_preds_sub ).transpose()
mean_preds = mean_preds.mean(axis=1)
 sub_df = pd.DataFrame()
sub_df['qid'] = df_sub.qid.values
sub_df['prediction'] =(mean_preds>subThreshold ).astype(int)
 sub_df.to_csv('submission.csv', index=False )<set_options>
scale1 = preprocessing.StandardScaler().fit(X)
 X_scaled1 = scale1.transform(X )
warnings.filterwarnings('ignore' )<import_modules>
X_test = test.drop(['Survived','PassengerId'],axis=1 )
from nltk.tokenize import TweetTokenizer
from gensim.models import KeyedVectors
from sklearn.metrics import f1_score
from sklearn.model_selection import StratifiedKFold, train_test_split<import_modules>
predscale1 = preprocessing.StandardScaler().fit(X_test)
 pred_scale = predscale1.transform(X_test )
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch
import torchtext
from torchtext import data, vocab
from torchtext.data import Dataset<import_modules>
testpred = best_model.predict(pred_scale )
torchtext.vocab.tqdm = tqdm_notebook<define_variables>
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': testpred})
 output.info()
output['Survived'] = output['Survived'].astype('int64')
 output.head()
path = ".. /input"
emb_path = ".. /input/embeddings"
n_folds = 5
bs = 512
device = 'cuda'<set_options>
output['Survived'].value_counts()
seed = 7777
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.deterministic = True<choose_model_class>
from sklearn.svm import SVC
tknzr = TweetTokenizer(strip_handles=True, reduce_len=True )<define_variables>
model = SVC()
mispell_dict = {
"can't" : "can not", "tryin'":"trying",
"'m": " am", "'ll": " 'll", "'d" : " 'd'", ".. ": " ",".": ".", ",":" , ",
"'ve" : " have", "n't": " not","'s": " 's", "'re": " are", "$": " $","’": " ' ",
"y'all": "you all", 'metoo': 'me too',
'colour': 'color', 'centre': 'center', 'favourite': 'favorite',
'travelling': 'traveling', 'counselling': 'counseling',
'centerd': 'centered',
'theatre': 'theater','cancelled': 'canceled','labour': 'labor',
'organisation': 'organization','wwii': 'world war 2', 'citicise': 'criticize',
'youtu ': 'youtube ','Qoura': 'Quora','sallary': 'salary','Whta': 'What',
'narcisist': 'narcissist','howdo': 'how do','whatare': 'what are',
'howcan': 'how can','howmuch': 'how much','howmany': 'how many', 'whydo': 'why do',
'theBest': 'the best', 'howdoes': 'how does', 'mastrubation': 'masturbation',
'mastrubate': 'masturbate', "mastrubating": 'masturbating', 'doI': 'do I',
'pennis': 'penis', 'Etherium': 'Ethereum', 'narcissit': 'narcissist',
'2k17': '2017', '2k18': '2018','qouta': 'quota', 'exboyfriend': 'ex boyfriend',
'airhostess': 'air hostess', "whst": 'what', 'watsapp':'whatsapp',
'demonitisation': 'demonetization', 'demonitization': 'demonetization',
'demonetisation': 'demonetization','bigdata': 'big data',
'Quorans': 'Questions','quorans': 'questions','quoran':'question','Quoran':'Question',
'Skripal':'russian spy','Doklam':'Tibet',
'BNBR':'Be Nice Be Respectful', 'Brexit': 'British exit',
'Bhakts':'fascists','bhakts':'fascists','Bhakt':'fascist','bhakt':'fascist',
'SJWs':'Social Justice Warrior','SJW':'Social Justice Warrior',
'Modiji':'Prime Minister of India', 'Ra apist': 'Rapist', ' apist ':' ape ',
'wumao':'commenters','cucks': 'cuck', 'Strzok':'stupid phrase','strzok':'stupid phrase',
' s.p ': ' ', ' S.P ': ' ', 'U.s.p': '', 'U.S.A.': 'USA', 'u.s.a.': 'USA', 'U.S.A': 'USA',
'u.s.a': 'USA', 'U.S.': 'USA', 'u.s.': 'USA', ' U.S ': ' USA ', ' u.s ': ' USA ', 'U.s.': 'USA',
' U.s ': 'USA', ' u.S ': ' USA ', ' fu.k': ' fuck', 'U.K.': 'UK', ' u.k ': ' UK ',
' don t ': ' do not ', 'bacteries': 'batteries', ' yr old ': ' years old ', 'Ph.D': 'PhD',
'cau.sing': 'causing', 'Kim Jong-Un': 'The president of North Korea', 'savegely': 'savagely',
'2fifth': 'twenty fifth', '2third': 'twenty third',
'2nineth': 'twenty nineth', '2fourth': 'twenty fourth', '
'Trumpcare': 'Trump health care system', '4fifth': 'forty fifth', 'Remainers': 'remainder',
'Terroristan': 'terrorist', 'antibrahmin': 'anti brahmin','culturr': 'culture',
'fuckboys': 'fuckboy', 'Fuckboys': 'fuckboy', 'Fuckboy': 'fuckboy', 'fuckgirls': 'fuck girls',
'fuckgirl': 'fuck girl', 'Trumpsters': 'Trump supporters', '4sixth': 'forty sixth',
'weatern': 'western', '4fourth': 'forty fourth', 'emiratis': 'emirates', 'trumpers': 'Trumpster',
'indans': 'indians', 'mastuburate': 'masturbate', ' f**k': ' fuck', ' F**k': ' fuck', ' F**K': ' fuck',
' u r ': ' you are ', ' u ': ' you ', '操你妈 ': 'fuck your mother', ' e.g.': ' for example',
'i.e.': 'in other words', '...': '.', 'et.al': 'elsewhere', 'anti-Semitic': 'anti-semitic',
' f***': ' fuck', ' f**': ' fuc', ' F***': ' fuck', ' F**': ' fuck',
' a****': ' assho', 'a**': 'ass', ' h***': ' hole', 'A****': 'assho', ' A**': ' ass', ' H***': ' hole',
' s***': ' shit', ' s**': 'shi', ' S***': ' shit', ' S**': ' shi', ' Sh**': 'shit',
' p****': ' pussy', ' p*ssy': ' pussy', ' P****': ' pussy',
' p***': ' porn', ' p*rn': ' porn', ' P***': ' porn',' Fck': ' Fuck',' fck': ' fuck',
' st*up*id': ' stupid', ' d***': 'dick', ' di**': ' dick', ' h*ck': ' hack',
' b*tch': ' bitch', 'bi*ch': ' bitch', ' bit*h': ' bitch', ' bitc*': ' bitch', ' b****': ' bitch',
' b***': ' bitc', ' b**': ' bit', ' b*ll': ' bull',' FATF': 'Western summit conference',
'Terroristan': 'terrorist Pakistan', 'terroristan': 'terrorist Pakistan',
' incel': ' involuntary celibates', ' incels': ' involuntary celibates', 'emiratis': 'Emiratis',
'weatern': 'western', 'westernise': 'westernize', 'Pizzagate': 'debunked conspiracy theory', 'naïve': 'naive',
' HYPSM': ' Harvard, Yale, Princeton, Stanford, MIT', ' HYPS': ' Harvard, Yale, Princeton, Stanford',
'kompromat': 'compromising material', ' Tharki': ' pervert', ' tharki': 'pervert',
'Naxali ': 'Naxalite ', 'Naxalities': 'Naxalites','Mewani': 'Indian politician Jignesh Mevani', ' Wjy': ' Why',
'Fadnavis': 'Indian politician Devendra Fadnavis', 'Awadesh': 'Indian engineer Awdhesh Singh',
'Awdhesh': 'Indian engineer Awdhesh Singh', 'Khalistanis': 'Sikh separatist movement',
'madheshi': 'Madheshi','Stupead': 'stupid', 'narcissit': 'narcissist',
}
def clean_latex(text):

 text = re.sub(r'\[math\]', ' LaTex math ', text)
 text = re.sub(r'\[\/math\]', ' LaTex math ', text)
 pattern_to_sub = {
r'\\mathrm': ' LaTex math mode ',
r'\\mathbb': ' LaTex math mode ',
r'\\boxed': ' LaTex equation ',
r'\\begin': ' LaTex equation ',
r'\\end': ' LaTex equation ',
r'\\left': ' LaTex equation ',
r'\\right': ' LaTex equation ',
r'\\(over|under)brace': ' LaTex equation ',
r'\\text': ' LaTex equation ',
r'\\vec': ' vector ',
r'\\var': ' variable ',
r'\\theta': ' theta ',
r'\\mu': ' average ',
r'\\min': ' minimum ',
r'\\max': ' maximum ',
r'\\sum': ' + ',
r'\\times': ' * ',
r'\\cdot': ' * ',
r'\\hat': ' ^ ',
r'\\frac': ' / ',
r'\\div': ' / ',
r'\\sin': ' Sine ',
r'\\cos': ' Cosine ',
r'\\tan': ' Tangent ',
r'\\infty': ' infinity ',
r'\\int': ' integer ',
r'\\in': ' in ',
}
pattern_dict = {k.strip('\'): v for k, v in pattern_to_sub.items() }
patterns = pattern_to_sub.keys()
pattern_re = re.compile('(%s)' % '|'.join(patterns))
def _replace(match):

 try:
word = pattern_dict.get(match.group(0 ).strip('\'))
except KeyError:
word = match.group(0)
 print('!!Error: Could Not Find Key: {}'.format(word))
return word
return pattern_re.sub(_replace, text)
 def correct_spelling(s, dic):
for key, corr in dic.items() :
s = s.replace(key, dic[key])
 return s
def tweet_clean(text):
text = re.sub(r'[^A-Za-z0-9!.,?$'"]+', ' ', text)
 return text
def tokenizer(s):
s = clean_latex(s)
 s = correct_spelling(s, mispell_dict)
 s = tweet_clean(s)
 return tknzr.tokenize(s )<compute_test_metric>
model.fit(X_scaled,y_train )
def find_threshold(y_t, y_p, floor=-1., ceil=1., steps=41):
thresholds = np.linspace(floor, ceil, steps)
 best_val = 0.0
for threshold in thresholds:
val_predict =(y_p > threshold)
 score = f1_score(y_t, val_predict)
 if score > best_val:
best_threshold = threshold
best_val = score
return best_threshold<split>
predictions = model.predict(X_testscaled )
def splits_cv(data, cv, y=None):
for indices in cv.split(range(len(data)) , y): 
(train_data, val_data)= tuple([data.examples[i] for i in index] for index in indices)
 yield tuple(Dataset(d, data.fields)for d in(train_data, val_data)if d )<load_from_csv>
svcacc = round(accuracy_score(predictions, y_test)* 100, 2)
 print(svcacc,'%' )
skf = StratifiedKFold(n_splits = n_folds, shuffle = True, random_state = seed)
 scores = pd.read_csv('.. /input/train.csv')
 target = scores.target.values
scores = scores.set_index('qid')
 scores.drop(columns=['question_text'], inplace=True)
 subm = pd.read_csv('.. /input/test.csv')
 subm = subm.set_index('qid')
 subm.drop(columns='question_text', inplace=True )<define_variables>
testpredictions = model.predict(pred_scale )
txt_field = data.Field(sequential=True, tokenize=tokenizer, include_lengths=False, use_vocab=True)
 label_field = data.Field(sequential=False, use_vocab=False, is_target=True)
 qid_field = data.RawField()
train_fields = [ 
('qid', qid_field), 
('question_text', txt_field), 
('target', label_field)
 ]
test_fields = [ 
('qid', qid_field), 
('question_text', txt_field),
]<load_from_csv>
output2 = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': testpredictions})
 output2.info()
output2['Survived'] = output['Survived'].astype('int64')
 output2.head()
train_ds = data.TabularDataset(path=os.path.join(path, 'train.csv'),
format='csv',
fields=train_fields,
skip_header=True)
 test_ds = data.TabularDataset(path=os.path.join(path, 'test.csv'),
format='csv',
fields=test_fields,
skip_header=True )<define_variables>
output['Survived'].value_counts()
test_ds.fields['qid'].is_target = False
train_ds.fields['qid'].is_target = False<define_variables>
output2['Survived'].value_counts()
<choose_model_class><EOS> 
output2.to_csv('my_submission.csv', index=False )