Kaggle competition: DonorsChoose.org Application Screening
<SOS> metric: AUC Kaggle data source: donorschoose.org-application-screening<choose_model_class>
nltk.download("punkt")
gb_clf = GradientBoostingClassifier()<predict_on_test>
%matplotlib inline
warnings.filterwarnings('ignore' )
gb_clf.fit(X[:,select],y)
 prediction = gb_clf.predict(test[:, select] ).astype(int )<create_dataframe>
df_train = pd.read_csv('.. /input/donorschoose-application-screening/train.csv')
 df_test = pd.read_csv('.. /input/donorschoose-application-screening/test.csv')
 df_resources = pd.read_csv('.. /input/donorschoose-application-screening/resources.csv' )
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )<save_to_csv>
embedding_path = ".. /input/glove6b300dtxt/glove.6B.300d.txt"
def read_embedding_vec(embedding_file_path):
embedding_list = []
embedding_word_dict = dict()
f = open(embedding_file_path)
 for index, line in enumerate(f):
values = line.split()
word = values[0]
try:
coefs = np.asarray(values[1:], dtype='float32')
 except:
continue
embedding_list.append(coefs)
 embedding_word_dict[word] = len(embedding_word_dict)
 f.close()
embedding_list = np.array(embedding_list)
 return embedding_list, embedding_word_dict
submission.to_csv('./Submission_GradientBoosting_clf.csv', columns=['Id', 'Target'], index= False )<choose_model_class>
embedding_list, embedding_word_dict = read_embedding_vec(embedding_path)
tree_clf = DecisionTreeClassifier()<save_to_csv>
try:
embedding_word_dict['nanword']
except:
embedding_word_dict['nanword'] = len(embedding_word_dict)
 embedding_list = np.append(embedding_list, [np.zeros_like(embedding_list[0,:])], axis = 0)
 try:
embedding_word_dict['unknownword']
except:
embedding_word_dict['unknownword'] = len(embedding_word_dict)
 embedding_list = np.append(embedding_list, [np.zeros_like(embedding_list[0,:])], axis = 0)
<import_modules>
def preprocess(string):

 string = string.lower()
string = re.sub(r'(\")', '', string)
 string = re.sub(r'(\\r\ 
)', ' ', string)
 string = re.sub(r'(\\r)', ' ', string)
 string = re.sub(r'(\ 
)', ' ', string)
 string = re.sub(r'(\\)', ' ', string)
 string = re.sub(r'\\t', ' ', string)
 string = re.sub(r'\:', ' ', string)
 string = re.sub(r'
from sklearn.ensemble import VotingClassifier<choose_model_class>
words_dict = dict()
voting_clf = VotingClassifier(estimators= [('tree', tree_clf),('gradiantBoost', gb_clf),('logR', clf)], voting= 'hard' )<compute_test_metric>
def combine_text(df):
try:
if len(df['text'])== len(df): return df
except: pass
df['text'] = ""
text_col = ['project_title', 'project_essay_1',
'project_essay_2', 'project_essay_3',
'project_essay_4', 'project_resource_summary']
df[text_col] = df[text_col].fillna('NANWORD')
 df['text'] = df.apply(lambda x: " ".join([str(x[col] ).strip() for col in text_col]), axis=1)
 return df
cross_val_score(voting_clf, X[:, select], y, scoring = 'f1_macro', cv= kfold )<predict_on_test>
df_train = combine_text(df_train)
 df_train.head()
prediction = cross_val_predict(voting_clf, X[:, select], y, cv=kfold)
 confusion_matrix(y, prediction )<predict_on_test>
def tokenize_sentences(sentences, words_dict, early_stopping = False):

 tokenized_sentences = []
for sentence in tqdm.tqdm(sentences):
if hasattr(sentence, "decode"):
sentence = sentence.decode("utf-8")
 sentence = preprocess(sentence)
 tokens = nltk.tokenize.word_tokenize(sentence)
 result = []
for word in tokens:
word = word.lower()
if word not in words_dict:
words_dict[word] = len(words_dict)
 word_index = words_dict[word]
result.append(word_index)
 tokenized_sentences.append(result)
 if early_stopping:
print(sentence)
 if len(tokenized_sentences)== 50:
return tokenized_sentences, words_dict
return tokenized_sentences, words_dict
voting_clf.fit(X[:, select],y)
 prediction = voting_clf.predict(test[:, select] ).astype(int )<create_dataframe>
df_train['tokenized_text'], words_dict = tokenize_sentences(df_train['text'], words_dict )
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )<save_to_csv>
df_train["tokenized_text_length"] = df_train['tokenized_text'].apply(lambda x: len(x))
submission.to_csv('./Submission_voting_clfHardVote.csv', columns=['Id', 'Target'], index= False )<choose_model_class>
len(df_train[(df_train["project_is_approved"] == True)&(df_train["tokenized_text_length"]>= 630)] )
voting_clf = VotingClassifier(estimators= [('tree', tree_clf),('gradiantBoost', gb_clf),('logR', clf)], voting= 'soft' )<compute_test_metric>
def clear_embedding_list(embedding_list, embedding_word_dict, words_dict):

 cleared_embedding_list = []
cleared_embedding_word_dict = {}
for word in words_dict:
if word not in embedding_word_dict:
continue
word_id = embedding_word_dict[word]
row = embedding_list[word_id]
cleared_embedding_list.append(row)
 cleared_embedding_word_dict[word] = len(cleared_embedding_word_dict)
 return cleared_embedding_list, cleared_embedding_word_dict
<predict_on_test>
def convert_tokens_to_embedded(tokenized_sentences, words_to_ids, embedding_word_dict,
sentences_length = 630, padding = embedding_word_dict['nanword'],
early_stopping = False):

 words_train = []
for sentence in tokenized_sentences:
current_words = []
for word_index in sentence:
word = words_to_ids[word_index]
word_id = embedding_word_dict.get(word, embedding_word_dict['unknownword'])
 current_words.append(word_id)
 if len(current_words)>= sentences_length:
current_words = current_words[:sentences_length]
else:
current_words += [padding] *(sentences_length - len(current_words))
if early_stopping and len(words_train)>= 10:
break
words_train.append(current_words)
 return words_train
voting_clf.fit(X[:, select],y)
 prediction = voting_clf.predict(test[:, select] ).astype(int )<create_dataframe>
words_to_ids = dict()
for key, value in words_dict.items() :
words_to_ids[value] = key
submission = pd.DataFrame({'Id': in_test['Id'], 'idhogar': in_test['idhogar'], 'isHead': in_test['parentesco1'] , 'Target':prediction})
 submission['Target'].value_counts(normalize = True )<save_to_csv>
train_x_words = np.array(convert_tokens_to_embedded(df_train['tokenized_text'],
words_to_ids, embedding_word_dict,
sentences_length = 630))
submission.to_csv('./Submission_voting_clfSoftVote.csv', columns=['Id', 'Target'], index= False )<set_options>
df_train.drop(['project_title', 'project_essay_1', 'project_essay_2', 'project_essay_3', 'project_essay_4',
'project_resource_summary', 'text'], axis = 1, inplace = True)
 gc.collect()
plt.style.use('ggplot')
 %matplotlib inline
plt.rcParams['font.size'] = 12<load_from_csv>
df_resources['total_price'] = df_resources['quantity'] * df_resources['price']
sum_total_price = pd.DataFrame(df_resources.groupby('id' ).total_price.sum() ).reset_index()
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print('train data shape :',train_df.shape)
 print('test data shape :',test_df.shape )<count_values>
df_train = pd.merge(df_train, sum_total_price, on='id')
 df_test = pd.merge(df_test, sum_total_price, on = 'id' )
y=train_df['Target']
y.value_counts()<count_missing_values>
train_x_num = np.array(df_train[['total_price', 'teacher_number_of_previously_posted_projects', 'tokenized_text_length']])
 scaler = MinMaxScaler()
train_x_num = scaler.fit_transform(train_x_num )
train_null = train_df.isnull().sum()
train_null[train_null > 0]<count_missing_values>
GlobalMaxPool1D, GlobalAveragePooling1D, SpatialDropout1D, CuDNNGRU, Dropout,\
Bidirectional, GRU, BatchNormalization, AveragePooling1D, MaxPooling1D
test_null = test_df.isnull().sum()
test_null[test_null > 0]<count_unique_values>
def get_model(embedding_matrix = embedding_list, num_features = 3,
maxlen = 630, dropout_rate = 0.4, learning_rate = 5e-3,
recurrent_units = 64):

 input_words = Input(( maxlen,))
input_nums = Input(( num_features,))
embedding_dim = 300
max_features = len(embedding_matrix)
 x_words = Embedding(max_features, embedding_dim,
weights = [embedding_matrix],
input_length = maxlen,
trainable=False )(input_words)
 x_words = SpatialDropout1D(dropout_rate )(x_words)
 x_words1 = Bidirectional(GRU(recurrent_units, return_sequences=True))(x_words)
 x_words1 = Convolution1D(64, 5, activation="relu" )(x_words1)
 x_words1_1 = MaxPooling1D(pool_size = 2, strides = 2, padding = 'same' )(x_words1)
 x_words1_1 = GlobalAveragePooling1D()(x_words1)
 x = BatchNormalization()(x_words1_1)
 dense_units = [64, 16]
x = Dropout(dropout_rate )(x)
 x = Dense(dense_units[0], activation="relu" )(x)
 x = Dropout(dropout_rate )(x)
 x = Dense(dense_units[1], activation="relu" )(x)
 x_nums = input_nums
x = BatchNormalization()(x)
 x = concatenate([x, x_nums])
 predictions = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=[input_words, input_nums], outputs=predictions)
 model.compile(optimizer=optimizers.Adam(learning_rate, decay=1e-6),
loss='binary_crossentropy',
metrics=['accuracy'])
 return model
train_grphh = train_df.groupby('idhogar')['Target'].apply(lambda x: x.nunique() == 1)
 err_train = train_grphh[train_grphh !=True]
print('Number of households with incorrect poverty level :',len(err_train))<feature_engineering>
file_path = 'best.h5'
for household in err_train.index:
target = int(train_df[(train_df['idhogar']==household)&(train_df['parentesco1']==1.0)]['Target'])
 train_df.loc[train_df['idhogar']==household,'Target'] = target<train_model>
checkpoint = ModelCheckpoint(file_path, monitor='val_loss', verbose=1, save_best_only=True,
save_weights_only=True, mode='min')
 early = EarlyStopping(monitor="val_loss", mode="min", patience=4)
 lr_reduced = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=1,
epsilon=1e-4, mode='min')
 callbacks_list = [checkpoint, early, lr_reduced]
history = model.fit([train_x_words, train_x_num],
df_train['project_is_approved'], validation_split=0.03,
verbose=1, callbacks=callbacks_list, epochs=10, batch_size=256 )
train_df,test_df = train_df.align(test_df,join='inner',axis=1)
 print('Training Features shape: ', train_df.shape)
 print('Testing Features shape: ', test_df.shape )<concatenate>
del df_train
del train_x_words
gc.collect()
data = pd.concat([train_df,test_df],axis=0)
 data.head()<count_unique_values>
df_test = combine_text(df_test)
 df_test.drop(['project_title', 'project_essay_1', 'project_essay_2', 'project_essay_3', 'project_essay_4',
'project_resource_summary'], axis = 1, inplace = True)
 gc.collect()
cat_cols = data.nunique() ==2
cat_cols = list(cat_cols[cat_cols].keys() )<feature_engineering>
df_test['tokenized_text'], words_dict = tokenize_sentences(df_test['text'], words_dict)
 df_test["tokenized_text_length"] = df_test['tokenized_text'].apply(lambda x: len(x))
cols = ['edjefe', 'edjefa','dependency']
data[cols] = data[cols].replace({'no': 0, 'yes':1} ).astype(float)
 data['hogar_mid'] = data['hogar_adul'] - data['hogar_mayor']
data['bedroom%'] = data['bedrooms']/data['rooms']
data['person/rooms'] = data['rooms']/data['hhsize']
data['male_ratio'] = data['r4h3']/data['r4t3']
data['female_ratio'] = data['r4m3']/data['r4t3']
data['female_per_room'] = data['r4m3']/data['rooms']
data['female_per_bedroom'] = data['r4m3']/data['bedrooms']
data['hogarmid_per_bedroom'] = data['hogar_mid']/data['bedrooms']<correct_missing_values>
test_x_num = np.array(df_test[['total_price', 'teacher_number_of_previously_posted_projects', 'tokenized_text_length']])
 test_x_num = scaler.transform(test_x_num )
data.fillna(-1,inplace=True )<train_model>
for key, value in words_dict.items() :
words_to_ids[value] = key
train_df = data[:len(train_df)]
test_df = data[len(train_df):]
print('Training data shape: ', train_df.shape)
 print('Testing data shape: ', test_df.shape )<import_modules>
test_x_words = np.array(convert_tokens_to_embedded(df_test['tokenized_text'],
words_to_ids, embedding_word_dict,
padding = embedding_word_dict['nanword'],
sentences_length = 630))
<compute_train_metric><EOS> 
model.load_weights(file_path)
 pred_test = model.predict([test_x_words, test_x_num], batch_size=1024, verbose=1)
 df_test["project_is_approved"] = pred_test
df_test[['id', 'project_is_approved']].to_csv("submission.csv", index=False )