Kaggle competition: Santander Customer Satisfaction
<SOS> metric: AUC Kaggle data source: santander-customer-satisfaction<count_missing_values>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import xgboost as xgb
import seaborn as sns
features = ['keyword','location']
for feat in features :
print("The number of missing values in "+ str(feat)+" is "+str(df_all[feat].isnull().sum())+ " for the combined dataset")
 print("The number of missing values in "+ str(feat)+" is "+str(train[feat].isnull().sum())+ " for the train dataset")
 print("The number of missing values in "+ str(feat)+" is "+str(test[feat].isnull().sum())+ " for the test dataset" )<string_transform>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
sentences = train['text']
train_size = int(7613*0.8)
 train_sentences = sentences[:train_size]
train_labels = target[:train_size]
test_sentences = sentences[train_size:]
test_labels = target[train_size:]
vocab_size = 10000
embedding_dim = 16
max_length = 120
trunc_type='post'
oov_tok = "<OOV>"
tokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)
 tokenizer.fit_on_texts(train_sentences)
 word_index = tokenizer.word_index
sequences = tokenizer.texts_to_sequences(train_sentences)
 padded = pad_sequences(sequences,maxlen=max_length, truncating=trunc_type)
 testing_sequences = tokenizer.texts_to_sequences(test_sentences)
 testing_padded = pad_sequences(testing_sequences,maxlen=max_length)
<choose_model_class>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
tf.keras.layers.GlobalAveragePooling1D() ,
tf.keras.layers.Dense(14, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
 model.summary() 
<train_model>
print('Shapes:')
 print(train.shape)
 print(test.shape)
 print('
IDs únicos:')
 print(train.ID.nunique())
 print(test.ID.nunique())
 print('
Colunas com nulos:')
 print(( train.isnull().sum() !=0 ).sum() )
num_epochs = 10
history = model.fit(padded, train_labels, epochs=num_epochs, validation_data=(testing_padded, test_labels))<string_transform>
train.TARGET.value_counts()
tokenizer_1 = Tokenizer(num_words = vocab_size, oov_token=oov_tok)
 tokenizer_1.fit_on_texts(train['text'])
 word_index = tokenizer_1.word_index
sequences = tokenizer_1.texts_to_sequences(train['text'])
 padded = pad_sequences(sequences,maxlen=max_length, truncating=trunc_type)
 true_test_sentences = test['text']
testing_sequences = tokenizer_1.texts_to_sequences(true_test_sentences)
 testing_padded = pad_sequences(testing_sequences,maxlen=max_length )<train_on_grid>
to_drop = []
n_cols = len(train.drop(['TARGET'],axis=1 ).columns)
 for i in range(n_cols):
v1 = train.iloc[i].values
for j in range(i+1,n_cols):
v2 = train.iloc[j].values
if np.all(v1 == v2):
to_drop.append(train.columns[j])
 train.drop(to_drop, axis=1, inplace=True)
 test.drop(to_drop, axis=1, inplace=True)
 print('dropped: ' + str(len(to_drop)))
 print(train.shape)
 print(test.shape )
model_2 = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
tf.keras.layers.GlobalAveragePooling1D() ,
tf.keras.layers.Dense(24, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model_2.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
 model_2.summary()
num_epochs = 10
history = model_2.fit(padded, target, epochs=num_epochs, verbose=2 )<define_variables>
X = train.drop(['TARGET','ID'],axis=1)
 y = train['TARGET']
X_train, X_test, y_train, y_test = train_test_split(X,y)
 print(X_train.shape)
 print(X_test.shape)
 print(y_train.shape)
 print(y_test.shape)
 X = train.drop(['TARGET','ID'],axis=1)
 y = train.TARGET
X_train, X_test, y_train, y_test = train_test_split(X,y )
final_output = []
for val in pred_plot.target:
if val > 0.5:
final_output.append(1)
 else:
final_output.append(0 )<save_to_csv>
clf = xgb.XGBClassifier(max_depth = 5, n_estimators=1000, learning_rate=0.2, nthread=3, subsample=1.0,
colsample_bytree=0.5, min_child_weight=3, reg_alpha=0.03, eed=1301)
 clf.fit(X_train, y_train, early_stopping_rounds=50, eval_metric="auc", eval_set=[(X_train, y_train),(X_test, y_test)] )
submission['target'] = final_output
submission.to_csv("final.csv", index=False)
 submission.head()<set_options>
submission = test[['ID']]
submission['TARGET'] = clf.predict_proba(test.drop(['ID'],axis=1), ntree_limit=clf.best_iteration)[:,1]
<load_from_csv><EOS> 
submission.to_csv('submission.csv', index=False)
 submission.head()