Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<create_dataframe>
%matplotlib inline
sns.set(style="whitegrid")
 warnings.filterwarnings('ignore')
 pd.set_option('max_colwidth',80 )
train_gen = train_datagen.flow_from_dataframe(dataframe=train_data,
directory=".. /input/train/train",
x_col="filename",
y_col="label",
class_mode="categorical",
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE )<create_dataframe>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df_combine = df_train.append(df_test, sort=False )
val_gen = train_datagen.flow_from_dataframe(dataframe=val_data,
directory=".. /input/train/train",
x_col="filename",
y_col="label",
class_mode="categorical",
shuffle=False,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE )<choose_model_class>
feat_desc = pd.DataFrame({'Description': ['Passenger ID',
'Whether the passenger was survived or not',
'The ticket class that the passenger bought',
'The passenger name',
'The gender of the passenger',
'The age of the passenger',
'The number of siblings/spouses that the passenger has aboard the Titanic',
'The number of parents/children that the passenger has aboard the Titanic',
'The ticket number of the passenger',
'The ticket fare that the passenger paid',
'The cabin number that the passenger boarded',
'The passenger port of embarkation'],
'Values': [df_train[i].unique() for i in df_train.columns],
'Number of unique values': [len(df_train[i].unique())for i in df_train.columns]},
index = df_train.columns)
 feat_desc
base_model = keras.applications.ResNet50(input_shape=(IMAGE_SIZE,IMAGE_SIZE,3),
include_top=False,
weights='imagenet')
 base_model.trainable = True
model = keras.Sequential([
base_model,
keras.layers.GlobalAveragePooling2D() ,
keras.layers.Dense(len(LABELS), activation='sigmoid')
 ])
 model.summary()<compute_test_metric>
df_train.set_index('PassengerId', inplace=True)
 df_test.set_index('PassengerId', inplace=True)
 df_combine = df_train.append(df_test, sort=False )
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))<define_search_model>
pd.DataFrame({'Number of Missing Values(Training)': df_train.isna().sum() ,
'% of Missing Values(Training)':(df_train.isna().sum() /df_train.shape[0] * 100 ).round(2),
'Number of Missing Values(Test)': df_test.isna().sum().round(0),
'% of Missing Values(Test)':(df_test.isna().sum() /df_test.shape[0] * 100 ).round(2)} )
mcp = keras.callbacks.ModelCheckpoint("resnet50.h5", monitor="val_f1", save_best_only=True, save_weights_only=True, verbose=1,mode='max')
 rlr = keras.callbacks.ReduceLROnPlateau(monitor='val_f1', factor=LEARNING_RATE_SCHEDULE_FACTOR, mode='max', patience=LEARNING_RATE_SCHEDULE_PATIENCE, min_lr=1e-8, verbose=1)
 callbacks = [mcp, rlr]<train_model>
df_train['Cabin'].fillna('Z', inplace=True)
 df_test['Cabin'].fillna('Z', inplace=True )
device = '/cpu:0' if tfe.num_gpus() == 0 else '/gpu:0'
with tf.device(device):
steps_per_epoch = train_gen.n // BATCH_SIZE
validation_steps = val_gen.n // BATCH_SIZE
model.compile(optimizer=keras.optimizers.Adam(lr=LEARNING_RATE), loss='binary_crossentropy', metrics=[f1])
 history = model.fit_generator(train_gen,
steps_per_epoch=steps_per_epoch,
epochs= MAX_EPOCHS,
verbose=1,
validation_data=val_gen,
validation_steps=validation_steps,
callbacks=callbacks )<load_from_csv>
df_combine['Cabin'] = df_train['Cabin'].str.get(0)
 df_combine.groupby('Cabin')['Pclass'].value_counts().to_frame('Count' )
test_df = pd.read_csv(".. /input/sample_submission.csv")
 test_df.head()<create_dataframe>
df_train.loc[df_train['Embarked'].isna() ]
test_gen = train_datagen.flow_from_dataframe(dataframe=test_df,
directory=".. /input/test/test",
x_col="filename",
class_mode=None,
shuffle=False,
target_size=(IMAGE_SIZE,IMAGE_SIZE),
batch_size=BATCH_SIZE )<load_pretrained>
df_train.loc[df_train['Embarked'].isna() , 'Embarked'] = 'S'
model.load_weights("resnet50.h5" )<predict_on_test>
df_test.loc[df_test['Fare'].isna() ]
pred = model.predict_generator(test_gen )<feature_engineering>
df_test.loc[df_test['Fare'].isna() ] = df_train['Fare'].mean()
for idx, row in test_df.iterrows() :
test_df.loc[idx]['predicted'] = probs2label(pred[idx] )<save_to_csv>
df_age = df_train.loc[~df_train['Age'].isna() ]
reg = LinearRegression()
reg.fit(df_age[['SibSp', 'Pclass']], df_age['Age'])
 pred_age_train = pd.Series(reg.predict(df_train[['SibSp', 'Pclass']]), index=df_train.index)
 pred_age_test = pd.Series(reg.predict(df_test[['SibSp', 'Pclass']]), index=df_test.index)
 df_train['Age'].fillna(pred_age_train, inplace=True)
 df_test['Age'].fillna(pred_age_test, inplace=True )
test_df.to_csv("submission.csv", index=False )<load_from_csv>
df_train.loc[df_train['Age'] < 0]
def create_data(x, len_seq):
X = []
y = []
for i in range(len(x)- len_seq):
X.append(x[i:i+len_seq])
 y.append(x[i+len_seq])
 return np.array(X), np.array(y)
 df = pd.read_csv('.. /input/train.csv', index_col=0)
 x = df['Min temp.'].values
len_seq = 121
X, y = create_data(x, len_seq)
 X = np.reshape(X,(X.shape[0], X.shape[1], 1))
print(X.shape)
 print(y.shape )<choose_model_class>
df_test.loc[df_test['Age'] < 0]
model = Sequential()
model.add(LSTM(units=64, return_sequences=False, input_shape=(len_seq, 1)))
 model.add(Dropout(0.2))
model.add(Dense(units=1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')
 model.summary()<train_model>
df_train.loc[df_train['Age'] < 0, 'Age'] = df_train['Age'].mean()
df_test.loc[df_test['Age'] < 0, 'Age'] = df_train['Age'].mean()
model.fit(X, y, batch_size=365, epochs=100, validation_split=0.1 )<predict_on_test>
df_train['Age'] = df_train['Age'].round().astype('int')
 df_test['Age'] = df_test['Age'].round().astype('int' )
x = df['Min temp.'].values[len(x)-len_seq:]
predict = np.array([])
 for i in range(365):
X = np.reshape(x,(1, len_seq, 1))
p = model.predict(X)[0]
predict = np.append(predict, p)
 x = np.append(np.delete(x, 0), p)
 %matplotlib inline
plt.plot(predict)
 plt.show()<save_to_csv>
df_train['Title'] = df_train['Name'].str.split(',', expand=True)[1].str.split('.' ).str.get(0)
 df_test['Title'] = df_test['Name'].str.split(',', expand=True)[1].str.split('.' ).str.get(0 )
submit = pd.read_csv('.. /input/sampleSubmission.csv')
 submit['Min temp.'] = predict
submit.to_csv('submission.csv', index=False )<set_options>
df_train['Title'].value_counts().to_frame('Number of Passengers' ).T
%matplotlib inline
warnings.simplefilter(action="ignore", category=FutureWarning )<load_pretrained>
df_train['SibSp+Parch'] = df_train['SibSp'] + df_train['Parch']
df_test['SibSp+Parch'] = df_test['SibSp'] + df_test['Parch']
shutil.copyfile(src=".. /input/redcarpet.py", dst=".. /working/redcarpet.py")
<load_pretrained>
df_train['IsAlone'] = df_train['SibSp+Parch'].map(lambda x: 1 if x == 0 else 0)
 df_test['IsAlone'] = df_test['SibSp+Parch'].map(lambda x: 1 if x == 0 else 0 )
item_file = ".. /input/talent.pkl"
item_records, COLUMN_LABELS, READABLE_LABELS, ATTRIBUTES = pickle.load(open(item_file, "rb"))
item_df = pd.DataFrame(item_records)[ATTRIBUTES + COLUMN_LABELS].fillna(value=0)
 ITEM_NAMES = item_df["name"].values
ITEM_IDS = item_df["id"].values
item_df.head()<load_pretrained>
train_size = df_train.shape[0]
test_size = df_test.shape[0]
df_combine = df_train.append(df_test, sort=False)
 df_combine['Last_Name'] = df_combine['Name'].str.split(',', expand=True)[0]
s_items = mat_to_sets(item_df[COLUMN_LABELS].values)
 print("Items", len(s_items))
csr_train, csr_test, csr_input, csr_hidden = pickle.load(open(".. /input/train_test_mat.pkl", "rb"))
m_split = [np.array(csr.todense())for csr in [csr_train, csr_test, csr_input, csr_hidden]]
m_train, m_test, m_input, m_hidden = m_split
print("Matrices", len(m_train), len(m_test), len(m_input), len(m_hidden))
s_train, s_test, s_input, s_hidden = pickle.load(open(".. /input/train_test_set.pkl", "rb"))
print("Sets", len(s_train), len(s_test), len(s_input), len(s_hidden))<prepare_output>
fare_df = df_combine.loc[df_combine['SibSp+Parch'] > 0, ['Last_Name', 'Fare', 'SibSp+Parch']]
fare_diff =(fare_df.groupby(['Last_Name', 'SibSp+Parch'])['Fare'].aggregate('max')- fare_df.groupby(['Last_Name', 'SibSp+Parch'])['Fare'].aggregate('min')).value_counts()
print('Percentage of families with the same fare: {:.2f}%'.format(fare_diff[0]/fare_diff.sum() *100))
like_df = pd.DataFrame(m_train, columns=ITEM_NAMES)
 like_df.head()<import_modules>
train_temp_df = df_combine.iloc[:train_size]
family_group_df = train_temp_df.loc[train_temp_df['SibSp+Parch']>0,
['Last_Name', 'Fare', 'SibSp+Parch', 'Survived']].groupby(['Last_Name', 'Fare'])
 family_df = pd.DataFrame(data=family_group_df.size() , columns=['Size_in_training_dataset'])
 family_df['Survived_Total'] = family_group_df['Survived'].sum().astype('int')
 family_df['SibSp+Parch'] = family_group_df['SibSp+Parch'].mean().astype('int')
 all_survived =(family_df['Size_in_training_dataset'] == family_df['Survived_Total'] ).sum() /len(family_df)*100
print('Families with the whole members survived: {:.1f}%'.format(all_survived))
all_not_survived =(family_df['Survived_Total']==0 ).sum() /len(family_df)*100
print('Families with the whole members not survived: {:.1f}%'.format(all_not_survived))
from redcarpet import mapk_score, uhr_score
from redcarpet import jaccard_sim, cosine_sim
from redcarpet import collaborative_filter, content_filter, weighted_hybrid
from redcarpet import get_recs<import_modules>
df_combine['FamilySurvival'] = 0.5
for _, grp_df in df_combine[['Survived', 'Last_Name', 'Fare']].groupby(['Last_Name', 'Fare']):
if len(grp_df)> 1:
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
if smax == 1:
df_combine.loc[ind, 'FamilySurvival'] = 1
elif smin == 0:
df_combine.loc[ind, 'FamilySurvival'] = 0
from mlxtend.frequent_patterns import apriori<create_dataframe>
train_temp_df = df_combine.iloc[:train_size]
ticket_group_df = train_temp_df.groupby('Ticket')
 ticket_df = pd.DataFrame(data=ticket_group_df.size() , columns=['Size_in_training_dataset'])
 ticket_df['Survived_Total'] = ticket_group_df['Survived'].sum().astype('int')
 ticket_df['Not_Family'] = ticket_group_df['Last_Name'].unique().apply(len)
 ticket_df = ticket_df.loc[(ticket_df['Size_in_training_dataset'] > 1)&(ticket_df['Not_Family'] > 1)]
print('Number of groups in training set that is not family: {}'.format(len(ticket_df)))
 all_survived =(ticket_df['Size_in_training_dataset'] == ticket_df['Survived_Total'] ).sum() /len(ticket_df)*100
print('Families with the whole members survived: {:.1f}%'.format(all_survived))
all_not_survived =(ticket_df['Survived_Total'] == 0 ).sum() /len(ticket_df)*100
print('Families with the whole members not survived: {:.1f}%'.format(all_not_survived))
def mine_association_rules(m_train, min_support=0.5):
freq_is = apriori(pd.DataFrame(m_train), max_len=2, min_support=min_support)
 freq_is["len"] = freq_is["itemsets"].apply(lambda s: len(s))
freq_is = freq_is.query("len == 2")
 if len(freq_is)== 0:
return pd.DataFrame([], columns=["a", "b", "ct", "support"])
 item_counts = m_train.sum(axis=0)
 rules = []
for record in freq_is.to_dict(orient="records"):
fset = record["itemsets"]
a = min(fset)
 b = max(fset)
 n = len(m_train)
 supp = record["support"]
all_a = item_counts[a]
all_b = item_counts[b]
both = supp * n
f11 = int(both)
 f10 = int(all_a - both)
 f01 = int(all_b - both)
 f00 = int(n -(f11 + f10 + f01))
rules.append({"a": a, "b": b, "ct":(f11, f10, f01, f00), "support": supp})
 rules.append({"a": b, "b": a, "ct":(f11, f01, f10, f00), "support": supp})
 rule_df = pd.DataFrame(rules)
 return rule_df<filter>
for grp, grp_df in df_combine.groupby('Ticket'):
if len(grp_df)> 1:
for ind, row in grp_df.iterrows() :
if(row['FamilySurvival'])== 0 or(row['FamilySurvival'] == 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
if smax == 1:
df_combine.loc[ind, 'FamilySurvival'] = 1
elif smin == 0:
df_combine.loc[ind, 'FamilySurvival'] = 0
df_train['FamilySurvival'] = df_combine.iloc[:train_size]['FamilySurvival']
df_test['FamilySurvival'] = df_combine.iloc[train_size:]['FamilySurvival']
used_rules = all_rules.query("support >= 0.03")
 len(all_rules), len(used_rules )<compute_train_metric>
df_combine['RealFare'] = 0
for _, grp_df in df_combine.groupby(['Ticket']):
grp_size = len(grp_df)
 for ind, row in grp_df.iterrows() :
real_fare = row['Fare']/grp_size
df_combine.loc[ind, 'RealFare'] = real_fare
df_train['Fare'] = df_combine.iloc[:train_size]['RealFare']
df_test['Fare'] = df_combine.iloc[train_size:]['RealFare']
def sets_to_contingency(a, b, N):
f11 = len(a.intersection(b))
f10 = len(a)- f11
f01 = len(b)- f11
f00 = N -(f11 + f10 + f01)
 return f11, f10, f01, f00
def rule_support(f11, f10, f01, f00):
N = f11 + f10 + f01 + f00
return f11 / N
def rule_confidence(f11, f10, f01, f00):
return f11 /(f11 + f10)
 def rule_interest_factor(f11, f10, f01, f00):
N = f11 + f10 + f01 + f00
f1p = f11 + f10
fp1 = f11 + f01
return(N * f11)/(f1p * fp1)
 def rule_phi_correlation(f11, f10, f01, f00):
f1p = f11 + f10
f0p = f01 + f00
fp1 = f11 + f01
fp0 = f10 + f00
num =(f11 * f00)-(f01 * f10)
 denom = np.sqrt(f1p * fp1 * f0p * fp0)
 if denom == 0:
return 0.0
return num / denom
def rule_is_score(f11, f10, f01, f00):
intfac = rule_interest_factor(f11, f10, f01, f00)
 supp = support(f11, f10, f01, f00)
 return np.sqrt(intfac * supp )<sort_values>
df_train.dtypes.to_frame(name='Data type' )
def rank_association_rules(mined_rules_df, score_fn, score_name="score"):
rule_df = pd.DataFrame(mined_rules_df.copy())
 rule_df[score_name] = rule_df["ct"].apply(lambda ct: score_fn(*ct))
return rule_df.sort_values(by=score_name, ascending=False )<statistical_test>
df_train.replace({'male': 1, 'female': 0}, inplace=True)
 df_test.replace({'male': 1, 'female': 0}, inplace=True )
def association_filter(rules_df, m_train, s_input, score_fn=rule_support, min_score=0.01, k=10):
score_name = "score"
ranked_rules = rank_association_rules(rules_df, score_fn=score_fn, score_name=score_name)
 top_rules_df = ranked_rules.query("{} >= {}".format(score_name, min_score))
rule_records = top_rules_df.to_dict(orient="records")
 all_recs = []
for likes in s_input:
rec_map = {}
for rule in rule_records:
if rule["a"] in likes and rule["b"] not in likes:
if rule["b"] not in rec_map:
rec_map[rule["b"]] = 0
rec_map[rule["b"]] += rule[score_name]
ranks = sorted(rec_map.items() , key=lambda p: p[1], reverse=True)
 all_recs.append(ranks[0:k])
 return all_recs, top_rules_df<compute_train_metric>
df_train.replace({'S': 0, 'C': 1, 'Q': 2}, inplace=True)
 df_test.replace({'S': 0, 'C': 1, 'Q': 2}, inplace=True )
k_top = 10
print("Metric: Support")
 rec_scores, rule_df = association_filter(used_rules, m_train, s_input, score_fn=rule_support)
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("Used {} association rules.".format(len(rule_df)))
 rule_df.head()<compute_train_metric>
df_train.drop(columns=['Name', 'Ticket'], inplace=True)
 df_test.drop(columns=['Name', 'Ticket'], inplace=True )
print("Metric: Confidence")
 rec_scores, rule_df = association_filter(used_rules, m_train, s_input, score_fn=rule_confidence)
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("Used {} association rules.".format(len(rule_df)))
 rule_df.head()<compute_train_metric>
df_train['Title'].value_counts().to_frame('Number of Passengers' ).T
print("Metric: Phi Correlation")
 rec_scores, rule_df = association_filter(used_rules, m_train, s_input, score_fn=rule_phi_correlation)
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("Used {} association rules.".format(len(rule_df)))
 rule_df.head()<compute_train_metric>
df_test['Title'].value_counts().to_frame('Number of Passengers' ).T
print("Metric: IS Score")
 rec_scores, rule_df = association_filter(used_rules, m_train, s_input, score_fn=rule_is_score)
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("Used {} association rules.".format(len(rule_df)))
 rule_df.head()<find_best_params>
df_train['Title'] = df_train['Title'].str.strip().map(lambda x: x if x == 'Mr' or x == 'Miss' or x == 'Mrs' or x == 'Master' else 'Other')
 df_test['Title'] = df_test['Title'].str.strip().map(lambda x: x if x == 'Mr' or x == 'Miss' or x == 'Mrs' or x == 'Master' else 'Other' )
def get_all_scores(rec_scores):
all_scores = []
for recs in rec_scores:
for(item, score)in recs:
all_scores.append(score)
 return all_scores<feature_engineering>
df_train = df_train.join(pd.get_dummies(df_train['Title'], prefix='Title'), how='outer')
 df_test = df_test.join(pd.get_dummies(df_test['Title'], prefix='Title'), how='outer' )
supp_scores, _ = association_filter(used_rules, m_train, s_input, score_fn=rule_support)
 conf_scores, _ = association_filter(used_rules, m_train, s_input, score_fn=rule_confidence)
 phi_scores, _ = association_filter(used_rules, m_train, s_input, score_fn=rule_phi_correlation)
 is_scores, _ = association_filter(used_rules, m_train, s_input, score_fn=rule_is_score )<compute_test_metric>
df_train.drop(columns=['Title'], inplace=True)
 df_test.drop(columns=['Title'], inplace=True )
rec_scores = weighted_hybrid([ 
(supp_scores, 0.25), 
(conf_scores, 0.25), 
(phi_scores, 0.25), 
(is_scores, 0.25),
])
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)) )<compute_test_metric>
df_train['Age_binned'] = pd.cut(df_train['Age'], np.arange(0, 85, 5), include_lowest=True)
 df_test['Age_binned'] = pd.cut(df_test['Age'], np.arange(0, 85, 5), include_lowest=True )
rec_scores = weighted_hybrid([ 
(supp_scores, 2.0), 
(conf_scores, 0.25), 
(phi_scores, 0.25), 
(is_scores, 0.25),
])
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)) )<compute_test_metric>
df_train['Age_binned'] = pd.cut(df_train['Age'], [0, 5, 30, 60, 80], labels=[0, 1, 2, 3], retbins=False, include_lowest=True)
 df_train['Age_binned'] = df_train['Age_binned'].astype('int')
 df_test['Age_binned'] = pd.cut(df_test['Age'], [0, 5, 30, 60, 80], labels=[0, 1, 2, 3], retbins=False, include_lowest=True)
 df_test['Age_binned'] = df_test['Age_binned'].astype('int' )
rec_scores = weighted_hybrid([ 
(supp_scores, 3.0), 
(conf_scores, 1.5), 
(phi_scores, 1.5), 
(is_scores, 1.0),
])
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)) )<compute_train_metric>
df_train.drop(columns='Age', inplace=True)
 df_test.drop(columns='Age', inplace=True )
cos_scores = collaborative_filter(s_train, s_input, sim_fn=cosine_sim, j=30)
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(cos_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(cos_scores), k=k_top)) )<compute_test_metric>
df_train['SibSp'] = df_train['SibSp'].map(lambda x: 3 if x == 4 or x == 5 or x == 8 else x)
 df_test['SibSp'] = df_test['SibSp'].map(lambda x: 3 if x == 4 or x == 5 or x == 8 else x )
rec_scores = weighted_hybrid([ 
(supp_scores, 1.0), 
(conf_scores, 1.0), 
(phi_scores, 1.0), 
(is_scores, 1.0), 
(cos_scores, 10.0)
 ])
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(rec_scores), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(rec_scores), k=k_top)) )<import_modules>
df_train['Parch'] = df_train['Parch'].map(lambda x: x if x == 0 else 1)
 df_test['Parch'] = df_test['Parch'].map(lambda x: x if x == 0 else 1 )
from redcarpet import write_kaggle_recs<load_pretrained>
df_train['SibSp+Parch'] = df_train['SibSp+Parch'].map(lambda x: 1 if x == 1 or x == 2 or x == 3 else x)
 df_test['SibSp+Parch'] = df_test['SibSp+Parch'].map(lambda x: 1 if x == 1 or x == 2 or x == 3 else x)
 df_train['SibSp+Parch'] = df_train['SibSp+Parch'].map(lambda x: 2 if x == 4 or x == 5 or x == 6 or x == 7 or x == 10 else x)
 df_test['SibSp+Parch'] = df_test['SibSp+Parch'].map(lambda x: 2 if x == 4 or x == 5 or x == 6 or x == 7 or x == 10 else x )
s_hold_input = pickle.load(open(".. /input/hold_set.pkl", "rb"))
print("Hold Out Set: N = {}".format(len(s_hold_input)))
 s_all_input = s_input + s_hold_input
print("All Input: N = {}".format(len(s_all_input)) )<statistical_test>
df_train['Fare_binned'] = pd.cut(df_train['Fare'], bins=[0,25,75,513], labels=[0, 1, 2], retbins=False, include_lowest=True)
 df_train['Fare_binned'] = df_train['Fare_binned'].astype('int')
 df_test['Fare_binned'] = pd.cut(df_test['Fare'], bins=[0,25,75,513], labels=[0, 1, 2], retbins=False, include_lowest=True)
 df_test['Fare_binned'] = df_test['Fare_binned'].astype('int' )
print("Final Model")
 print("Strategy: Association Rules")
 print("Scoring: Hybrid")
 supp_scores, _ = association_filter(used_rules, m_train, s_all_input, score_fn=rule_support)
 conf_scores, _ = association_filter(used_rules, m_train, s_all_input, score_fn=rule_confidence)
 phi_scores, _ = association_filter(used_rules, m_train, s_all_input, score_fn=rule_phi_correlation)
 is_scores, _ = association_filter(used_rules, m_train, s_all_input, score_fn=rule_is_score)
 final_scores = weighted_hybrid([ 
(supp_scores, 0.25), 
(conf_scores, 0.25), 
(phi_scores, 0.25), 
(is_scores, 0.25),
])
 final_recs = get_recs(final_scores )<save_to_csv>
df_train.drop(columns='Fare', inplace=True)
 df_test.drop(columns='Fare', inplace=True )
outfile = "kaggle_submission_association_rules_hybrid.csv"
n_lines = write_kaggle_recs(final_recs, outfile)
 print("Wrote predictions for {} users to {}.".format(n_lines, outfile))<import_modules>
df_train['Cabin'] = df_train['Cabin'].str.get(0 )
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier<load_from_csv>
df_test['HaveCabin'] = df_test['Cabin'].str.get(0)
 df_train['HaveCabin'] = df_train['Cabin'].map(lambda x: 0 if x == 'Z' else 1)
 df_test['HaveCabin'] = df_test['HaveCabin'].map(lambda x: 0 if x == 'Z' else 1 )
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 sample_sub = pd.read_csv('.. /input/sampleSubmission.csv' )<prepare_x_and_y>
df_train.drop(columns=['Cabin'], inplace=True)
 df_test.drop(columns=['Cabin'], inplace=True )
X = df_train.profession.values
y = df_train.target.values
X_test = df_test.profession.values<predict_on_test>
estimator = [('Logistic Regression', LogisticRegression),('Ridge Classifier', RidgeClassifier), 
('SGD Classifier', SGDClassifier),('Passive Aggressive Classifier', PassiveAggressiveClassifier), 
('SVC', SVC),('Linear SVC', LinearSVC),('Nu SVC', NuSVC), 
('K-Neighbors Classifier', KNeighborsClassifier), 
('Gaussian Naive Bayes', GaussianNB),('Multinomial Naive Bayes', MultinomialNB), 
('Bernoulli Naive Bayes', BernoulliNB),('Complement Naive Bayes', ComplementNB), 
('Decision Tree Classifier', DecisionTreeClassifier), 
('Random Forest Classifier', RandomForestClassifier),('AdaBoost Classifier', AdaBoostClassifier), 
('Gradient Boosting Classifier', GradientBoostingClassifier),('Bagging Classifier', BaggingClassifier), 
('Extra Trees Classifier', ExtraTreesClassifier),('XGBoost', XGBClassifier)]
X_train = df_train.drop(columns='Survived')
 y_train = df_train['Survived']
comparison_cols = ['Algorithm', 'Training Time(Avg)', 'Accuracy(Avg)', 'Accuracy(3xSTD)']
comparison_df = pd.DataFrame(columns=comparison_cols)
 cv_split = StratifiedShuffleSplit(n_splits=10, test_size=0.3, random_state=0)
 for idx, est in enumerate(estimator):
cv_results = cross_validate(est[1]() , X_train, y_train, cv=cv_split)
 comparison_df.loc[idx, 'Algorithm'] = est[0]
comparison_df.loc[idx, 'Training Time(Avg)'] = cv_results['fit_time'].mean()
comparison_df.loc[idx, 'Accuracy(Avg)'] = cv_results['test_score'].mean()
comparison_df.loc[idx, 'Accuracy(3xSTD)'] = cv_results['test_score'].std() * 3
comparison_df.set_index(keys='Algorithm', inplace=True)
 comparison_df.sort_values(by='Accuracy(Avg)', ascending=False, inplace=True)
 fig, ax = plt.subplots(figsize=(12,10))
y_pos = np.arange(len(comparison_df))
ax.barh(y_pos, comparison_df['Accuracy(Avg)'], xerr=comparison_df['Accuracy(3xSTD)'], color='skyblue')
 ax.set_yticks(y_pos)
 ax.set_yticklabels(comparison_df.index)
 ax.set_xlabel('Accuracy Score(Average)')
 ax.set_title('Performance Comparison After Simple Modelling', size=13)
 ax.set_xlim(0, 1)
 plt.show()
model = DecisionTreeClassifier(max_depth=4)
 model.fit(X.reshape(-1,1),y)
 y_hat = model.predict_proba(X_test.reshape(-1,1)) [:,1]<prepare_output>
estimator = [('Logistic Regression', LogisticRegression),('Ridge Classifier', RidgeClassifier),('SVC', SVC), 
('Linear SVC', LinearSVC),('Nu SVC', NuSVC),('Random Forest Classifier', RandomForestClassifier), 
('AdaBoost Classifier', AdaBoostClassifier), 
('Gradient Boosting Classifier', GradientBoostingClassifier), 
('Bagging Classifier', BaggingClassifier),('XGBoost', XGBClassifier)
 ]
index = [est[0] for est in estimator]
grid_params = {'SVC': {'C': np.arange(1,21,1), 'gamma': [0.005, 0.01, 0.015, 0.02], 'random_state': [0]},
'Ridge Classifier': {'alpha': [0.001, 0.0025, 0.005], 'random_state': [0]},
'Nu SVC': {'nu': [0.5], 'gamma': [0.001, 0.01, 0.1, 1], 'random_state': [0]},
'Gradient Boosting Classifier': {'learning_rate': [0.001, 0.005, 0.01, 0.015], 'random_state': [0],
'max_depth': [1,2,3,4,5], 'n_estimators': [300, 350, 400, 450, 500]},
'Linear SVC': {'C': [1, 5, 10], 'random_state': [0]},
'Logistic Regression': {'C': np.arange(2,7.5,0.25),
'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],
'random_state': [0]},
'AdaBoost Classifier': {'learning_rate': np.arange(0.05, 0.21, 0.01), 'n_estimators': [50, 75, 100, 125, 150],
'random_state': [0]},
'Random Forest Classifier': {'n_estimators': [200, 250, 300, 350], 'max_depth': [1,2,3,4,5,6],
'criterion': ['gini', 'entropy'], 'random_state': [0]},
'Bagging Classifier': {'n_estimators': np.arange(200, 300, 10), 'random_state': [0]},
'XGBoost': {'learning_rate': [0.001, 0.005, 0.01, 0.015], 'random_state': [0],
'max_depth': [1,2,3,4,5], 'n_estimators': [300, 350, 400, 450, 500]}
}
best_params_df = pd.DataFrame(columns=['Optimized Hyperparameters', 'Accuracy'], index=index)
 for idx, est in enumerate(estimator):
best_clf = GridSearchCV(est[1]() , param_grid=grid_params[est[0]], cv=cv_split, scoring='accuracy', n_jobs=12)
 best_clf.fit(X_train, y_train)
 best_params_df.loc[est[0], 'Optimized Hyperparameters'] = [best_clf.best_params_]
best_params_df.loc[est[0], 'Accuracy'] = best_clf.best_score_
sample_sub['target'] = y_hat 
<save_to_csv>
xg = GradientBoostingClassifier(learning_rate=0.005, max_depth=2, n_estimators=450, random_state=0)
 selector = RFECV(xg, step=1, cv=cv_split, scoring='accuracy', n_jobs=8)
 selector = selector.fit(X_train, y_train )
sample_sub.to_csv('v_01.csv', index=False )<load_pretrained>
pd.DataFrame([X_train.columns, selector.ranking_], index=['Features', 'Ranking'] ).T.sort_values(by='Ranking' )
class SentimentDataset(Dataset):

 def __init__(self,datalines):
self.xydata = datalines
def __len__(self):
return len(self.xydata)
 def __getitem__(self,idx):
return self.xydata[idx]
def load_data_set(filename):

 istream = open(filename)
 istream.readline()
xydataset = [ ]
for line in istream:
fields = line.split(',')
 label = fields[0]
text = ','.join(fields[1:])
 xydataset.append(( text,label))
istream.close()
return SentimentDataset(xydataset)
 train_set = load_data_set('.. /input/sentimentIMDB_train.csv')
 print('Loaded %d examples as train set.'%(len(train_set)))
 print('Train data Sample(1st batch only)')
 train_loader = DataLoader(train_set, batch_size=4, shuffle=True)
 for text_batch, label_batch in train_loader:
for text,label in zip(text_batch, label_batch):
print(' ',text[:50],'...',label)
 break
test_set = load_data_set('.. /input/sentimentIMDB_test.csv')
 print('Loaded %d examples as test set.'%(len(test_set)) )<feature_engineering>
selected_features = ['Pclass', 'Sex', 'SibSp+Parch', 'FamilySurvival', 'Title_Mr', 'Fare_binned']
gb = GradientBoostingClassifier(learning_rate=0.005, max_depth=2, n_estimators=450, random_state=0)
 gb.fit(X_train[selected_features], y_train)
 gb_acc_score = cross_val_score(gb, X_train[selected_features], y_train, cv=cv_split, scoring='accuracy')
 print('The performance of the model using the selected features: {:.2f}%'.format(gb_acc_score.mean() *100))
def make_w2idx(dataset):

 wordset = set([])
 for text,label in dataset:
words = text.split()
wordset.update(words)
 return dict(zip(wordset,range(len(wordset))))
def vectorize_text(text,w2idx):
counts = Counter(text.split())
 xvec = torch.zeros(len(w2idx))
for word in counts:
if word in w2idx:
xvec[w2idx[word]] = counts[word]
return xvec.squeeze()
def vectorize_target(ylabel):
return torch.tensor(float(ylabel))<train_model>
gb = GradientBoostingClassifier(learning_rate=0.005, max_depth=2, n_estimators=450, random_state=0)
 gb.fit(X_train[selected_features], y_train)
 test_Survived = pd.DataFrame(gb.predict(df_test[selected_features]), columns=['Survived'], index=np.arange(892,1310,1))
test_Survived = test_Survived.reset_index()
test_Survived.rename(columns={'index': 'PassengerID'}, inplace=True)
 test_Survived.to_csv("gb.csv",index=False )