Kaggle competition: Titanic - Machine Learning from Disaster
test[['id', 'target']].to_csv("submission_rnn.csv", index=False )<choose_model_class>
from sklearn.model_selection import train_test_split
import random
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
def ConvNet(max_sequence_length, num_words, embedding_dim, labels_index):
embedding_layer = Embedding(num_words,
embedding_dim,
input_length=max_sequence_length)
 sequence_input = Input(shape=(max_sequence_length,), dtype='int32')
 embedded_sequences = embedding_layer(sequence_input)
 convs = []
filter_sizes = [3,4,5,6]
for filter_size in filter_sizes:
l_conv = Conv1D(filters=50,
kernel_size=filter_size,
activation='relu' )(embedded_sequences)
 l_conv = Dropout(0.2 )(l_conv)
 l_pool = GlobalMaxPooling1D()(l_conv)
 convs.append(l_pool)
 l_merge = concatenate(convs, axis=1)
 x = Dense(32, activation='relu' )(l_merge)
 x = Dropout(0.2 )(x)
 preds = Dense(labels_index, activation='sigmoid' )(x)
 model = Model(sequence_input, preds)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['acc'])
 model.summary()
return model<choose_model_class>
import itertools
import matplotlib.pyplot as plt
import lightgbm as lgb
from sklearn import metrics
model = ConvNet(max_length, vocab_size, EMBEDDING_DIM, 1 )<train_model>
from sklearn.feature_selection import SelectKBest, f_classif
hist = model.fit(X_train_pad,
y_train,
epochs=25,
batch_size=128,
validation_data=(X_val_pad, y_val),
verbose=2 )<predict_on_test>
import itertools
y_pred = model.predict(X_test_pad)
 y_pred_binary = list(map(lambda x: 1 if x >= 0.5 else 0, y_pred))<save_to_csv>
def Feature_selection(X):
cat_features = [y for y in X.columns if X[y].dtypes == 'object']
cat_features = [col for col in cat_features if col not in ['Name', 'Ticket', 'Cabin']]
interactions = pd.DataFrame(index=X.index)
 for col1, col2 in itertools.combinations(cat_features, 2):
new_col_name = '_'.join([col1, col2])
 new_values = X[col1].map(str)+ "_" + X[col2].map(str)
 encoder = LabelEncoder()
X[new_col_name] = encoder.fit_transform(new_values)
 return X
test['target'] = y_pred_binary
test[['id', 'target']].to_csv("submission_cnn.csv", index=False )<filter>
import category_encoders as ce
data[data['target'] == 0].values<set_options>
stop = set(stopwords.words('english'))
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))<load_from_csv>
def Categorical_Encoding_train(X):
cat_features = [y for y in X.columns if X[y].dtypes == 'object']
cat_features = [col for col in cat_features if col not in ['Name']]
encoder = ce.CatBoostEncoder(cols = cat_features)
 encoder.fit(X[cat_features], X['Survived'])
 numerical_col = [y for y in X.columns if X[y].dtypes != 'object']
data = X[numerical_col].join(encoder.transform(X[cat_features]))
return data, encoder
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 target = train['target']
print("Train shape", train.shape)
 print("Test shape", test.shape )<string_transform>
def Categorical_Encoding_test(X, encoder):
cat_features = [y for y in X.columns if X[y].dtypes == 'object']
print(cat_features)
 cat_features = [col for col in cat_features if col not in ['Name']]
numerical_col = [y for y in X.columns if X[y].dtypes != 'object']
data = X[numerical_col].join(encoder.transform(X[cat_features]))
print(data.head())
 return data
def create_corpus(target):

 corpus = []
for x in train[train['target'] == target]['text'].str.split() :
for i in x:
corpus.append(i)
 return corpus
def filter_specific_word(corpus, filters):

 dic = defaultdict(int)
 for word in corpus:
if word in filters:
dic[word] += 1
return dic<feature_engineering>
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
def get_top_tweet_bigrams(corpus, n=None):
vec = CountVectorizer(ngram_range=(2, 2)).fit(corpus)
 bag_of_words = vec.transform(corpus)
 sum_words = bag_of_words.sum(axis=0)
 words_freq = [(word, sum_words[0, idx])for word, idx in vec.vocabulary_.items() ]
words_freq = sorted(words_freq, key=lambda x: x[1], reverse=True)
 return words_freq<concatenate>
def iterative_imputer(X):
numerical_feature = [y for y in X.columns if X[y].dtypes != 'object'
and X[y].isnull().sum() != 0]
imp_mean = IterativeImputer(max_iter=10, verbose=0)
 X[numerical_feature] = imp_mean.fit_transform(X[numerical_feature])
 return X
df = pd.concat([train, test], axis=0, sort=False)
 df.shape<drop_column>
def numerical_imputer(X):
numerical_feature = [y for y in X.columns if X[y].dtypes != 'object'
and X[y].isnull().sum() != 0]
my_imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
 X[numerical_feature] = pd.DataFrame(my_imputer.fit_transform(X[numerical_feature]))
return X
def remove_url(text):
url = re.compile('https?://\S+|www\.\S+')
 return url.sub(r'', text)
 remove_url('http://www.kaggle.com/rakkaalhazimi/nlp-disaster-classification/edit?rvi=1' )<feature_engineering>
import lightgbm as lgb
df['text'] = df['text'].apply(lambda x: remove_url(x))
retain = df['text'].str.contains(r'http[s]*' ).sum()
print("{} words were left behind".format(retain))<string_transform>
def train_and_predict(train_X, train_y, valid_X, valid_y, X_test):
dtrain = lgb.Dataset(train_X, label=train_y)
 dvalid = lgb.Dataset(valid_X, label=valid_y)
 param = {'num_leaves': 64, 'objective': 'binary'}
param['metric'] = 'auc'
num_round = 1000
bst = lgb.train(param, dtrain, num_round,
valid_sets=[dvalid], early_stopping_rounds=10, verbose_eval=False)
 y_pred = bst.predict(X_test)
 return y_pred
residual = df[df['text'].str.contains(r'http[s]*')]
left_word = []
for i in range(len(residual)) :
print(residual['text'].values[i])
 left_word.append(residual['text'].values[i] )<categorify>
if __name__ == '__main__':
seed = 123
random.seed(seed)
 print('Loading Training Data')
 baseline_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 baseline_data = Feature_selection(baseline_data)
 baseline_data = numerical_imputer(baseline_data)
 encoded_data, encoder = Categorical_Encoding_train(baseline_data)
 cols = [col for col in encoded_data.columns if col not in ['Survived','PassengerId']]
X = encoded_data[cols]
y = baseline_data['Survived']
valid_fraction = 0.1
valid_size = int(len(encoded_data)* valid_fraction)
 train_X = X[:-2 * valid_size]
train_y = y[:-2 * valid_size]
valid_X = X[-2 * valid_size:]
valid_y = y[-2 * valid_size:]
print('Loading Testing Data')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data = Feature_selection(test_data)
 test_data = numerical_imputer(test_data)
 encoded_data_test = Categorical_Encoding_test(test_data, encoder)
 X_test = encoded_data_test[cols].iloc[:,:]
print(X_test.head())
 y_pred = train_and_predict(train_X, train_y, valid_X, valid_y, X_test)
 y_pred = np.around(y_pred)
 y_pred = y_pred.astype(int)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId,
'Survived': y_pred})
 output.to_csv('submission_grid_search.csv', index=False)
for word in left_word:
compiler = re.compile(r'.http.+')
 result = compiler.sub('', word)
 print(result )<feature_engineering>
df['text'] = df['text'].str.replace(r'.http.+', '')
 print("http words found {}".format(df['text'].str.contains('http' ).sum()))<drop_column>
baseline_data.groupby('Pclass')['Survived'].count()
<feature_engineering><EOS> 
baseline_data.groupby('Embarked')['Survived'].count()