Kaggle competition: Titanic - Machine Learning from Disaster
df_test[u].nunique()<groupby>
data_train = pd.read_csv("/kaggle/input/titanic/train.csv")
 data_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 y = data_train.Survived
u = 'CurrencySymbol'
df_train.groupby(u ).size()<groupby>
data_train.groupby('Sex' ).Survived.mean()
df_test.groupby(u ).size().index<groupby>
data_train.groupby('SibSp' ).Survived.agg(['mean','count'] )
df_train.groupby(u)['ConvertedSalary'].mean()<groupby>
data_train.groupby('Parch' ).Survived.agg(['mean','count'] )
nf = 'count_CurrencySymbol'
df_train[nf] = df_train[u].map(df_train.groupby(u ).ConvertedSalary.count())
 df_test[nf] = df_test[u].map(df_train.groupby(u ).ConvertedSalary.count() )<categorify>
X_train = data_train.drop(['Name','Ticket','PassengerId'],axis=1)
 X_test = data_test.drop(['Name','Ticket','PassengerId'],axis=1 )
cats = []
for col in df_train.columns:
if df_train[col].dtype == 'object':
cats.append(col)
 print(col, df_train[col].nunique())
 encoder = OrdinalEncoder(cols=cats)
 df_train[cats] = encoder.fit_transform(df_train[cats])
 df_test[cats] = encoder.transform(df_test[cats] )<define_variables>
X_train = X_train.drop(['Cabin'],axis=1)
 X_test = X_test.drop(['Cabin'],axis=1 )
feature = ['CompanySize',
'LastNewJob',
'CurrencySymbol',
'YearsCoding',
'count_Employment',
'SalaryType',
'Currency',
'Employment',
'MilitaryUS',
'YearsCodingProf',
'count_CurrencySymbol',
'Age',
'RaceEthnicity',
'DevType',
'Student',
'AssessBenefits2',
'JobContactPriorities3',
'CareerSatisfaction',
'FrameworkWorkedWith',
'NumberMonitors',
'CheckInCode',
'AssessBenefits6',
'AssessJob1',
'AssessBenefits11',
'JobEmailPriorities5',
'AssessBenefits8',
'OperatingSystem',
'AssessBenefits9',
'FormalEducation',
'AssessBenefits7',
'AssessJob5',
'AssessJob4',
'AssessBenefits4',
'EducationParents',
'WakeTime',
'AssessBenefits1',
'JobEmailPriorities6',
'AssessBenefits10',
'AssessJob10',
'CommunicationTools',
'JobContactPriorities4',
'JobContactPriorities1',
'JobContactPriorities2',
'AdsPriorities5',
'UndergradMajor',
'AssessJob3',
'AssessJob8',
'AssessJob6',
'AssessJob2',
'AssessJob7',
'AssessBenefits5',
'AdsActions',
'AssessBenefits3',
'Exercise',
'AdsPriorities2',
'AdsPriorities3',
'JobEmailPriorities1',
'AdsAgreeDisagree3',
'AssessJob9',
'StackOverflowVisit',
'JobEmailPriorities2',
'AdsPriorities1',
'AdsPriorities6',
'Gender',
'JobEmailPriorities7',
'JobEmailPriorities3',
'AdsPriorities7',
'UpdateCV',
'AdsPriorities4',
'StackOverflowDevStory',
'HopeFiveYears',
'ErgonomicDevices']<prepare_x_and_y>
X_train[X_train.Embarked.isnull() ]
y_train = df_train.ConvertedSalary
X_train = df_train.drop(['ConvertedSalary'],axis=1)
 X_test = df_test.copy() 
<split>
X_train.groupby('Embarked' ).Embarked.count()
scores = []
y_pred_test = 0
skf = KFold(n_splits=5, random_state=60, shuffle=True)
 for i,(train_ix, test_ix)in tqdm(enumerate(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train.values[train_ix], y_train.values[train_ix]
X_val, y_val = X_train.values[test_ix], y_train.values[test_ix]
clf = LGBMRegressor(boosting_type='gbdt', class_weight=None, colsample_bytree=0.9,
importance_type='split', learning_rate=0.05, max_depth=-1,
min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,
n_estimators=9999, n_jobs=-1, num_leaves=15, objective=None,
random_state=71, reg_alpha=0.0, reg_lambda=0.0, silent=True,
subsample=1.0, subsample_for_bin=200000, subsample_freq=0)
 clf.fit(X_train_, y_train_, early_stopping_rounds=50, eval_metric='rmse', eval_set=[(X_val, y_val)])
 y_pred = clf.predict(X_val)
 y_pred_test += clf.predict(X_test)
 score = mean_squared_error(y_val, y_pred)
 scores.append(score)
 print('CV Score of Fold_%d is %f' %(i, score))<split>
X_train.Embarked=X_train.Embarked.fillna('S' )
skf = KFold(n_splits=5, random_state=40, shuffle=True)
 for i,(train_ix, test_ix)in tqdm(enumerate(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train.values[train_ix], y_train.values[train_ix]
X_val, y_val = X_train.values[test_ix], y_train.values[test_ix]
clf = LGBMRegressor(boosting_type='gbdt', class_weight=None, colsample_bytree=0.9,
importance_type='split', learning_rate=0.05, max_depth=-1,
min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,
n_estimators=9999, n_jobs=-1, num_leaves=15, objective=None,
random_state=71, reg_alpha=0.0, reg_lambda=0.0, silent=True,
subsample=1.0, subsample_for_bin=200000, subsample_freq=0)
 clf.fit(X_train_, y_train_, early_stopping_rounds=50, eval_metric='rmse', eval_set=[(X_val, y_val)])
 y_pred = clf.predict(X_val)
 y_pred_test += clf.predict(X_test)
 score = mean_squared_error(y_val, y_pred)
 scores.append(score)
 print('CV Score of Fold_%d is %f' %(i, score))<save_to_csv>
X_train[X_train.Embarked.isnull() ]
y_pred = y_pred_test/10
y_pred = np.expm1(y_pred)
 submission = pd.read_csv('.. /input/exam-for-students20200129/sample_submission.csv', index_col=0)
 submission.ConvertedSalary = y_pred
submission.to_csv('submission.csv' )<set_options>
def impute(cols):
Age = cols[0]
Pclass = cols[1]
if(pd.isnull(Age)) :
if Pclass==1:
return 38
elif Pclass==2:
return 30
else:
return 25
return Age
def reset_tf_session() :
curr_session = tf.get_default_session()
if curr_session is not None:
curr_session.close()
K.clear_session()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
s = tf.InteractiveSession(config=config)
 K.set_session(s)
 return s<load_from_csv>
X_train.Age = X_train[['Age','Pclass']].apply(impute,axis=1)
 X_test.Age = X_test[['Age','Pclass']].apply(impute,axis=1 )
train_data_np = pd.read_csv(".. /input/train_data.txt",delimiter=' ::: ',header=None,names=['id','title','genre','desc'])
 predict_data_np = pd.read_csv(".. /input/test_data.txt",delimiter=' ::: ',header=None,names=['id','title','desc'])
 train_data_np.shape 
<prepare_x_and_y>
X_test[X_test.Fare.isnull() ]
lb = LabelBinarizer()
lb.fit(genres)
 y = lb.transform(train_data_np.genre)
 print(lb.classes_)
 print(train_data_np.genre[0])
 y[0]<init_hyperparams>
X_test['Fare']=X_test['Fare'].fillna(13 )
desc = train_data_np['desc'].values
tokenizer = Tokenizer(num_words=None,lower=True)
 tokenizer.fit_on_texts(desc)
 deleted = 0
high_count_words = [w for w,c in tokenizer.word_counts.items() if c > 10.0*train_data_np.shape[0]]
for w in high_count_words:
del tokenizer.word_index[w]
del tokenizer.word_docs[w]
del tokenizer.word_counts[w]
deleted+=1
print("Delete ", w)
 print("Delete ", deleted, " words from tokenizer")
 desc_train, desc_test, y_train, y_test = train_test_split(desc, y, test_size=0.05, random_state=1000)
 X_train = tokenizer.texts_to_sequences(desc_train)
 X_test = tokenizer.texts_to_sequences(desc_test)
 vocab_size = len(tokenizer.word_index)+ 1
print(desc_train[2])
 print(X_train[2])
<categorify>
X_train = X_train.drop(['Survived'],axis=1 )
maxlen = 100
X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
 X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)
 print(X_train[0, :] )<choose_model_class>
X_test.groupby('Sex' ).Sex.count()
def make_model_8() :
s = reset_tf_session()
embedding_dim = 50
model = Sequential()
model.add(layers.Embedding(input_dim=vocab_size,
output_dim=embedding_dim,
input_length=maxlen))
model.add(keras.layers.GlobalAveragePooling1D())
 model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(len(genres), activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 model.summary()
return model 
<choose_model_class>
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder , LabelEncoder
def make_model_9() :
s = reset_tf_session()
embedding_dim = 50
model = Sequential()
model.add(layers.Embedding(input_dim=vocab_size,
output_dim=embedding_dim,
input_length=maxlen))
model.add(layers.Bidirectional(layers.LSTM(100, return_sequences=True, dropout=0.7,recurrent_dropout=0.7)))
 model.add(layers.Flatten())
 model.add(layers.Dense(len(genres), activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 model.summary()
return model<train_model>
le =LabelEncoder()
X_train['Sex']=le.fit_transform(X_train['Sex'])
 X_test['Sex'] = le.fit_transform(X_test['Sex'])
 X_train.head()
model = make_model_8()
history = model.fit(X_train, y_train,
epochs=20,
verbose=True,
validation_data=(X_test, y_test),
batch_size=1000)
 plot_history(history )<predict_on_test>
onh = OneHotEncoder(handle_unknown='ignore', sparse=False)
 X_train_trans = pd.DataFrame(onh.fit_transform(X_train[['Embarked']]))
X_test_trans = pd.DataFrame(onh.fit_transform(X_test[['Embarked']]))
X_train_trans.index = X_train.index
X_test_trans.index = X_test.index
X_train_conc = X_train.drop(['Embarked'],axis=1)
 X_test_conc = X_test.drop(['Embarked'],axis=1)
 X_train_final = pd.concat([X_train_conc,X_train_trans],axis=1)
 X_test_final = pd.concat([X_test_conc,X_test_trans],axis=1 )
y_predict_train = model.predict_proba(X_train )<find_best_params>
sc= StandardScaler()
X_train_final = sc.fit_transform(X_train_final)
 X_test_final = sc.transform(X_test_final )
T=201
y_predict = y_predict_train
print(y_predict[T,:])
 y_predict_max = np.argmax(y_predict[T,:])
 print(y_predict_max, lb.classes_[y_predict_max], y_predict[T,y_predict_max])
 print(np.argmax(y_train[T]),y_train[T])
 print(train_data_np.genre[T])
<predict_on_test>
clf = SVC(kernel='rbf', degree = 5)
 clf.fit(X_train_final,y )
<find_best_params><EOS> 
pred = clf.predict(X_test_final)
 output = pd.DataFrame({'PassengerId':data_test.PassengerId,'Survived':pred})
 output.to_csv('submission.csv',index=False )