Kaggle competition: Titanic - Machine Learning from Disaster
X_stack.fillna(-1, inplace = True)
 test_stack_val.fillna(-1,inplace=True)
 y_pred = stack.fit_predict(X_stack, target_stack, test_stack_val )<save_to_csv>
%matplotlib inline
sub = pd.DataFrame()
sub['ids'] = test_id
sub['prob'] = y_pred
sub.to_csv('stacked_main.csv', index=False )<load_from_csv>
train_df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv')
 combine = [train_df , test_df]
data = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 data.head()<train_model>
train_df.groupby('Sex' ).Survived.agg(['mean' , 'count'] ).sort_values('count' , ascending = False )
X = data.drop(['AveragePrice'], axis=1)
 y = data['AveragePrice']
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.33, random_state=42)
<save_to_csv>
train_df.groupby('Ticket' ).Survived.agg(['mean' , 'count'] ).sort_values('count' , ascending = False )
model = XGBRegressor(n_jobs=4, learning_rate=0.05, max_depth=8, min_child_weight=0.5, n_estimators=1300)
 model.fit(X,
y,
verbose=True)
 test_predict_optimal = model.predict(test)
 new_df = pd.DataFrame({"id": test['id'] , "AveragePrice":test_predict_optimal})
 new_df.to_csv("XGB_manual_custom_5.csv", index=False )<compute_train_metric>
train_df.groupby('Embarked' ).Survived.agg(['mean' , 'count'] ).sort_values('count' , ascending = False )
predictions = model.predict(X_val)
 scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error', cv=5)
 print(scores)
 print('Mean Absolute Error: %2f' %(-1 * scores.mean()))<compute_test_metric>
train_df.Cabin.value_counts()
mae = mean_absolute_error(predictions, y_val)
 print("Mean Absolute Error : " + str(mae))
error_percent = mae/data['AveragePrice'].mean() *100
print(str(error_percent)+ ' %')
<import_modules>
features_df = train_df.drop('Survived', axis=1)
 num_features = features_df.select_dtypes(np.number)
 num_features.sample(5 )
import lightgbm as lgb
from sklearn.metrics import mean_squared_log_error
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
train_df.groupby('Pclass' ).Survived.agg(['mean' , 'count'] ).sort_values('count' , ascending = False )
train = pd.read_csv('/kaggle/input/bike-sharing-demand-for-education/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand-for-education/test.csv')
 sample_submission = pd.read_csv('/kaggle/input/bike-sharing-demand-for-education/sample_submission.csv' )<prepare_x_and_y>
train_df.groupby('Sex' ).Pclass.agg(['mean' , 'count'] ).sort_values('count' , ascending = False )
X = train.drop(['datetime','casual','registered','cnt'],axis = 1)
 X_test = test.drop(['datetime'],axis = 1)
 y = train[['year','cnt']]<train_model>
train_df.groupby('Fare' ).Survived.agg(['mean' , 'count'] ).sort_values('count' , ascending = False )
%%time
columns = X.columns
y_preds = np.zeros(X_test.shape[0])
 feature_importances = pd.DataFrame()
feature_importances['feature'] = columns
X_train = X.query("year == 2011")
 X_valid = X.query("year == 2012")
 y_train = y.query("year == 2011")
 y_valid = y.query("year == 2012")
 y_train.drop('year',axis=1, inplace=True)
 y_valid.drop('year',axis=1, inplace=True)
 dtrain = lgb.Dataset(X_train, label=y_train)
 params = {
'objective': 'mean_squared_error',
'metric': 'rmse'
}
LGBM = lgb.train(params, dtrain, 1000, verbose_eval=100)
 feature_importances['importance'] = LGBM.feature_importance(importance_type='gain')
 y_pred_valid = LGBM.predict(X_valid)
 y_pred_valid = np.where(y_pred_valid < 0, 0, y_pred_valid)
 print("RMSLE:", mean_squared_log_error(y_valid, y_pred_valid))
print('******************************************************')
 y_preds = LGBM.predict(X_test)
 y_preds = np.where(y_preds < 0, 0, y_preds )<concatenate>
features_df = train_df.drop('Survived', axis=1)
 FEATURE_COLUMNS = features_df.columns
NUM_FEATURES = features_df.select_dtypes(include=[np.number] ).columns
CAT_FEATURES = ['Sex', 'Ticket']
numeric_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)) , 
('scaler', StandardScaler()),
])
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant', fill_value='missing')) , 
('one_hot', OneHotEncoder(handle_unknown='ignore')) ,
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numeric_transformer, NUM_FEATURES), 
('cat', categorical_transformer, CAT_FEATURES)
 ] )
submission = pd.concat([test['datetime'], pd.Series(y_preds)], axis=1)
 submission = submission.rename(columns={0:'cnt'} )<save_to_csv>
x_train, x_test, y_train, y_test = train_test_split(features_df, target, test_size=0.25 )
submission.to_csv('base_submission.csv',header=True, index=False )<train_model>
baseline = DummyClassifier(strategy='most_frequent')
 model = RandomForestClassifier()
pca = PCA(n_components=19)
 pca.fit(data)
 data = pca.transform(data)
 data_for_submission = pca.transform(data_for_submission)
 skf = StratifiedKFold(n_splits=5, shuffle=False)
 classifier = MLPClassifier(alpha=1, learning_rate='adaptive', max_iter=750, hidden_layer_sizes=(38, 76))
accuracy_list = []
for train_index, test_index in skf.split(data, label):
train_data = data[train_index]
train_label = label.iloc[train_index]
test_data = data[test_index]
test_label = label.iloc[test_index]
classifier.fit(train_data, train_label)
 accuracy_list.append(classifier.score(test_data, test_label))
prediction_labels = classifier.predict(data_for_submission)
 predicted_data = pd.DataFrame([data_for_submission_id,prediction_labels] ).transpose()
predicted_data.columns = ['id ','label']
predicted_data.to_csv('submission.csv',index=False,header=True )<feature_engineering>
baseline.fit(x_train, y_train)
 model.fit(x_train, y_train )
class Dictionary(object):
def __init__(self):
self.token2idx = {}
self.idx2token = []
def add_token(self, token):
if token not in self.token2idx:
self.idx2token.append(token)
 self.token2idx[token] = len(self.idx2token)- 1
return self.token2idx[token]
def __len__(self):
return len(self.idx2token )<string_transform>
baseline_predictions = baseline.predict(x_test)
 model_predictions = model.predict(x_test )
char_vocab = Dictionary()
pad_token = '<pad>'
unk_token = '<unk>'
pad_index = char_vocab.add_token(pad_token)
 unk_index = char_vocab.add_token(unk_token)
 chars = set(''.join(x_train_full))
for char in sorted(chars):
char_vocab.add_token(char)
 print("Vocabulary:", len(char_vocab), "UTF characters")
 lang_vocab = Dictionary()
languages = set(y_train_full)
 for lang in sorted(languages):
lang_vocab.add_token(lang)
 print("Labels:", len(lang_vocab), "languages" )<prepare_x_and_y>
print(classification_report(y_test, baseline_predictions))
print('a ->', char_vocab.token2idx['a'])
 print('cat ->', lang_vocab.token2idx['cat'])
 print(y_train_full[0], x_train_full[0][:10])
 x_train_idx = [np.array([char_vocab.token2idx[c] for c in line])for line in x_train_full]
y_train_idx = np.array([lang_vocab.token2idx[lang] for lang in y_train_full])
 print(y_train_idx[0], x_train_idx[0][:10] )<split>
print(classification_report(y_test, model_predictions))
x_train, x_val, y_train, y_val = train_test_split(x_train_idx, y_train_idx, test_size=0.15, random_state=seed)
 train_data = [(x, y)for x, y in zip(x_train, y_train)]
val_data = [(x, y)for x, y in zip(x_val, y_val)]
print(len(train_data), "training samples")
 print(len(val_data), "validation samples" )<define_variables>
test_df = pd.read_csv('.. /input/titanic/test.csv' )
def batch_generator(data, batch_size, token_size):

 minibatch, sequences_so_far, tokens_so_far = [], 0, 0
for ex in data:
minibatch.append(ex)
 seq_len = len(ex[0])
 if seq_len > token_size:
ex =(ex[0][:token_size], ex[1])
 seq_len = token_size
sequences_so_far += 1
tokens_so_far += seq_len
if sequences_so_far == batch_size or tokens_so_far == token_size:
yield minibatch
minibatch, sequences_so_far, tokens_so_far = [], 0, 0
elif sequences_so_far > batch_size or tokens_so_far > token_size:
yield minibatch[:-1]
minibatch, sequences_so_far, tokens_so_far = minibatch[-1:], 1, len(minibatch[-1][0])
 if minibatch:
yield minibatch<define_variables>
sub_model = RandomForestClassifier()
sub_model.fit(features_df, target)
 test_features = preprocessor.transform(test_df[train_df.drop('Survived', axis=1 ).columns])
 predictions = sub_model.predict(test_features )
def pool_generator(data, batch_size, token_size, shuffle=False):

 for p in batch_generator(data, batch_size * 100, token_size * 100):
p_batch = batch_generator(sorted(p, key=lambda t: len(t[0]), reverse=True), batch_size, token_size)
 p_list = list(p_batch)
 if shuffle:
for b in random.sample(p_list, len(p_list)) :
yield b
else:
for b in p_list:
yield b<choose_model_class>
sub_df = pd.DataFrame({'PassengerId' : test_df.PassengerId, 'Survived': predictions})
 sub_df.head()
class CharRNNClassifier(torch.nn.Module):
def __init__(self, input_size, embedding_size, hidden_size, output_size, model="lstm", num_layers=1, bidirectional=False, pad_idx=0):
super().__init__()
self.model = model.lower()
self.hidden_size = hidden_size
self.embed = torch.nn.Embedding(input_size, embedding_size, padding_idx=pad_idx)
 if self.model == "gru":
self.rnn = torch.nn.GRU(embedding_size, hidden_size, num_layers, bidirectional=bidirectional)
 elif self.model == "lstm":
self.rnn = torch.nn.LSTM(embedding_size, hidden_size, num_layers, bidirectional=bidirectional)
 self.linear1 = torch.nn.Linear(hidden_size, output_size)
 self.linear2 = torch.nn.Linear(output_size, input_size)
 def forward(self, input, input_lengths):
encoded = self.embed(input)
 packed = torch.nn.utils.rnn.pack_padded_sequence(encoded, input_lengths)
 output, _ = self.rnn(packed)
 padded, _ = torch.nn.utils.rnn.pad_packed_sequence(output, padding_value=float('-inf'))
padded = padded.permute(1,2,0)
 output = F.adaptive_max_pool1d(padded, 1 ).view(-1, self.hidden_size)
 output = self.linear1(output)
 output = self.linear2(output)
 return output<set_options>
sub_df.to_csv('titanic_gender_submission.csv', index=False )