Kaggle competition: Titanic - Machine Learning from Disaster
print("Number of cuisine categories: {}".format(len(train_data.cuisine.unique())))
 train_data.cuisine.unique()<randomize_order>
warnings.filterwarnings("ignore")
def random_colours(number_of_colors):
colors = []
for i in range(number_of_colors):
colors.append("
return colors<count_values>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 train.info()
labelpercents = []
for i in train_data.cuisine.value_counts() :
percent =(i/sum(train_data.cuisine.value_counts())) *100
percent = "%.2f" % percent
percent = str(percent + '%')
 labelpercents.append(percent )<set_options>


def substrings_in_string(big_string, substrings):
for substring in substrings:
if big_string.find(substring)!= -1:
return substring
print(big_string)
 return np.nan

 def phase1clean(df):
df.Fare = df.Fare.map(lambda x: np.nan if x==0 else x)
 df.Cabin = df.Cabin.fillna('Unknown')
 cabin_list = ['A', 'B', 'C', 'D', 'E', 'F', 'T', 'G', 'Unknown']
df['Deck']=df['Cabin'].map(lambda x: substrings_in_string(x, cabin_list))
title_list=['Mrs', 'Mr', 'Master', 'Miss', 'Major', 'Rev',
'Dr', 'Ms', 'Mlle','Col', 'Capt', 'Mme', 'Countess',
'Don', 'Jonkheer']
df['Title']=df['Name'].map(lambda x: substrings_in_string(x, title_list))
def replace_titles(x):
title=x['Title']
if title in ['Countess','Don', 'Major', 'Capt', 'Jonkheer', 'Rev', 'Col']:
return 'Rare'
elif title in ['Countess', 'Mme']:
return 'Mrs'
elif title in ['Mlle', 'Ms']:
return 'Miss'
elif title =='Dr':
if x['Sex']=='Male':
return 'Mr'
else:
return 'Mrs'
else:
return title
df['Title']=df.apply(replace_titles, axis=1)
 df['Family_Size']=df['SibSp']+df['Parch']
return df

 def fare_grouping(x):
bins = [-1, 7.91, 14.454, 31, 99, 250, np.inf]
names = ['a', 'b','c', 'd', 'e', 'f']
names = [1, 2, 3, 4, 5, 6]
x['Fare_Bin'] = pd.cut(x['Fare'], bins ,labels=names ).astype('int')
 dict_age={1 : 'a' , 2 : 'b' , 3 : 'c' , 4 : 'd' , 5: 'e' , 6 : 'f'}
x['Fare_Bin']=x['Fare_Bin'].map(dict_age)

return x

 def fill_nan(x):

 null_ind=x.loc[x['Age'].isnull() ,:].index
null_count=x.loc[x['Age'].isnull() ,]['PassengerId'].count()
num_ages=x.groupby('Title')['Age'].mean().to_dict()
x.loc[x['Age'].isnull() ,'Age']=x.loc[x['Age'].isnull() ,'Title'].map(num_ages)

null_ind=x.loc[x['Fare'].isnull() ,:].index
null_count=x.loc[x['Fare'].isnull() ,]['PassengerId'].count()
num_fare=x.groupby('Pclass')['Fare'].mean().to_dict()
x.loc[x['Fare'].isnull() ,'Fare']=x.loc[x['Fare'].isnull() ,'Pclass'].map(num_fare)
 fare_grouping(x)

f_index=x[x['Embarked'].isnull() ].index
x=x.drop(f_index,axis=0)
 return x

 def age_grouping(x):

 k_data=pd.concat([x['Age'],x['Survived']],axis=1)
 k_data.rename(columns={ 0 :'Age' , 1 :'Survived'}, inplace=True)

model = KMeans()
sil = []
for k in range(2, 9):
kmeans = KMeans(n_clusters = k ).fit(k_data)
 labels = kmeans.labels_
sil.append(silhouette_score(k_data, labels, metric = 'euclidean'))
plt_data=pd.concat([pd.Series(range(2,9)) ,pd.Series(sil)],axis=1)
 plt_data.rename(columns={ 0 :'clusters' , 1 :'silhouette scores'}, inplace=True)

kmeans = KMeans(n_clusters=4, random_state=0 ).fit(k_data)
 labels_=kmeans.labels_
k_data['age_grp']=labels_+1
k_data['Passenger_Id']=x['PassengerId']



 agelist=[]
for i in range(0, 15):
agelist.append('a')
 for i in range(15, 29):
agelist.append('b')
 for i in range(29, 45):
agelist.append('c')
 for i in range(45, 90):
agelist.append('d')
 age_dict={v: k for v, k in enumerate(agelist)}
x['Age_Grp']=x['Age'].astype(int ).map(age_dict)
 return x,age_dict

 def survived_fams(df):
df['Last_Name'] = df['Name'].apply(
 lambda df: str.split(df, ",")[0])
 default_survival_rate = 0.5
df['Family_Survival'] = default_survival_rate
for grp, grp_df in df[['Survived', 'Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId','SibSp', 'Parch', 'Age', 'Cabin']].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()
passID = row['PassengerId']
if(smax == 1.0):
df.loc[df['PassengerId'] ==
passID, 'Family_Survival'] = 1
elif(smin == 0.0):
df.loc[df['PassengerId'] ==
passID, 'Family_Survival'] = 0
for _, grp_df in df.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(
 row['Family_Survival'] == 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df.loc[df['PassengerId'] ==
passID, 'Family_Survival'] = 1
elif(smin == 0.0):
df.loc[df['PassengerId'] ==
passID, 'Family_Survival'] = 0
return df

 def is_alone(x):
fam_list={False : 0 , True : 1}
x['is_alone'] =(x['Family_Size']==0 ).map(fam_list)

return x

 def has_cabin(x):
fam_list={False : 0 , True : 1}
x['has_cabin'] =(x['Deck']!='Unknown' ).map(fam_list)

return x

 def is_3stclass(x):
class_list={False : 0 , True : 1}
x['is_3rdclass'] =(x['Pclass']== 3 ).map(class_list)

return x

 def class_categorizer(x):
dict_class={1 : 'a' , 2 : 'b' , 3 : 'c' }
x['Pclass']=x['Pclass'].map(dict_class)
 return x

 def phase2clean(train, test):
train=fill_nan(train)
 train,age_dict=age_grouping(train)
 test=fill_nan(test)
 test['Age_Grp']=test['Age'].astype(int ).map(age_dict)
 for df in [train, test]:
df['Fare_Per_Person']=df['Fare']/(df['Family_Size']+1)
 for df in [train, test]:
df['Age*Class']=df['Age']*df['Pclass']
combined=pd.concat([train,test])
 combined=survived_fams(combined)
 combined=is_alone(combined)
 combined=has_cabin(combined)
 combined=is_3stclass(combined)
 train=combined.iloc[:len(train),:]
test=combined.iloc[len(train):,:]
test=test.drop(['Survived'],axis=1)
 return [train,test]


def get_data() :
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 submit_x =pd.read_csv('/kaggle/input/titanic/test.csv')
 original_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 original_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 x=phase1clean(train_data)
 pred_set=phase1clean(submit_x)
 x,pred_set=phase2clean(x, pred_set)
 x=x.drop(['PassengerId','Name','Ticket','Cabin','Last_Name'],axis=1)
 pred_set=pred_set.drop(['PassengerId','Name','Ticket','Cabin','Last_Name'],axis=1)
 y = x.Survived
x = x.loc[:,x.columns!='Survived']
return x,y,pred_set,original_train,original_test
print("Word Cloud Function.. ")
 stopwords = set(STOPWORDS)
 size =(20,10)
 def cloud(text, title, stopwords=stopwords, size=size):

 mpl.rcParams['figure.figsize']=(10.0,10.0)
 mpl.rcParams['font.size']=12
mpl.rcParams['savefig.dpi']=100
mpl.rcParams['figure.subplot.bottom']=.1
wordcloud = WordCloud(width=1600, height=800,
background_color='black',
stopwords=stopwords,
).generate(str(text))
fig = plt.figure(figsize=size, dpi=80, facecolor='k',edgecolor='k')
 plt.imshow(wordcloud,interpolation='bilinear')
 plt.axis('off')
 plt.title(title, fontsize=50,color='y')
 plt.tight_layout(pad=0)
 plt.show()
train_data["ing"] = train_data.ingredients.apply(lambda x: list(map(str, x)) , 1 ).str.join(' ')
 cloud(train_data["ing"].values, title="All Cuisine", size=[8,5] )<string_transform>
x,y,pred_set,original_train,pred_set_original=get_data()
x.info()
x.head()
train_data['seperated_ingredients'] = train_data['ingredients'].apply(','.join)
 test_data['seperated_ingredients'] = test_data['ingredients'].apply(','.join )<import_modules>

 class_cat=FunctionTransformer(class_categorizer)
 quantile_transformer = FunctionTransformer(quantile_transform)

cat_features=['Title', 'Deck' ,'Pclass' , 'Sex' , 'Embarked' , 'Age_Grp' , 'Fare_Bin']
num_features = list(x.select_dtypes(include=['int64','float64'] ).columns)
 categorical_transformer = pip.Pipeline(steps=[('class_cat',class_cat), 
('enc', CatBoostEncoder())
 ])
 numerical_transformer = pip.Pipeline([('just','passthrough')])
 preprocessor = ColumnTransformer(transformers=[('cat', categorical_transformer , cat_features), 
('num' , numerical_transformer , num_features)
 ])
 pipeline_xgb =pip.Pipeline(steps=[('preprocessor', preprocessor), 
('feature_select', SelectKBest(chi2 , k = 15)) , 
('classifier',XGBClassifier(learning_rate=0.01 ,
n_estimators=860,
max_depth=3,
subsample=1,
colsample_bytree=1,
gamma=6,
reg_alpha = 14,
reg_lambda = 3))
])

cv = StratifiedKFold(5, shuffle=True, random_state=42)
 accuracies = cross_val_score(pipeline_xgb, x , y , cv = cv)
 print("5 fold cross validation accuracies {}".format(accuracies))
import nltk
from collections import Counter<feature_engineering>

 params=[{
'feature_select' : [SelectKBest(chi2)],
'feature_select__k' : [i for i in range(5,19)]
}]
cv = StratifiedKFold(5, shuffle=True, random_state=42)
 search=GridSearchCV(estimator=pipeline_xgb,
param_grid=params,
n_jobs=-1,
cv=cv)
 search.fit(x, y)
 print("best score : {} , best params : {} ".format(search.best_score_ , search.best_params_))
train_data['for ngrams']=train_data['seperated_ingredients'].str.replace(',',' ' )<define_search_model>

 cat_features=['Title', 'Deck' ,'Pclass' , 'Sex' , 'Embarked' , 'Age_Grp' , 'Fare_Bin']
num_features = list(x.select_dtypes(include=['int64','float64'] ).columns)
 categorical_transformer = pip.Pipeline(steps=[('class_cat',class_cat), 
('enc', CatBoostEncoder())
 ])
 numerical_transformer = pip.Pipeline([('normal_trans',quantile_transformer)])
 preprocessor = ColumnTransformer(transformers=[('cat', categorical_transformer , cat_features), 
('num' , numerical_transformer , num_features)])
 pipeline_log =pip.Pipeline(steps=[('preprocessor', preprocessor), 
('feature_select',SelectKBest(chi2, k = 17)) , 
('classifier',LogisticRegression(penalty = 'l2',
solver = 'liblinear',
C = 0.25))
])

cv = StratifiedKFold(5, shuffle=True, random_state=42)
 accuracies = cross_val_score(pipeline_log, x , y , cv = cv)
 print("5 fold cross validation accuracies {}".format(accuracies))
def generate_ngrams(text, n):
words = text.split(' ')
 iterations = len(words)- n + 1
for i in range(iterations):
yield words[i:i + n]
def net_diagram(*cuisines):
ngrams = {}
for title in train_data[train_data.cuisine==cuisines[0]]['for ngrams']:
for ngram in generate_ngrams(title, 2):
ngram = ','.join(ngram)
 if ngram in ngrams:
ngrams[ngram] += 1
else:
ngrams[ngram] = 1
ngrams_mws_df = pd.DataFrame.from_dict(ngrams, orient='index')
 ngrams_mws_df.columns = ['count']
ngrams_mws_df['cusine'] = cuisines[0]
ngrams_mws_df.reset_index(level=0, inplace=True)
 ngrams = {}
for title in train_data[train_data.cuisine==cuisines[1]]['for ngrams']:
for ngram in generate_ngrams(title, 2):
ngram = ','.join(ngram)
 if ngram in ngrams:
ngrams[ngram] += 1
else:
ngrams[ngram] = 1
ngrams_mws_df1 = pd.DataFrame.from_dict(ngrams, orient='index')
 ngrams_mws_df1.columns = ['count']
ngrams_mws_df1['cusine'] = cuisines[1]
ngrams_mws_df1.reset_index(level=0, inplace=True)
 cuisine1=ngrams_mws_df.sort_values('count',ascending=False)[:25]
cuisine2=ngrams_mws_df1.sort_values('count',ascending=False)[:25]
df_final=pd.concat([cuisine1,cuisine2])
 g = nx.from_pandas_edgelist(df_final,source='cusine',target='index')
 cmap = plt.cm.RdYlGn
colors = [n for n in range(len(g.nodes())) ]
k = 0.35
pos=nx.spring_layout(g, k=k)
 nx.draw_networkx(g,pos, node_size=df_final['count'].values*8, cmap = cmap, node_color=colors, edge_color='grey', font_size=15, width=3)
 plt.title("Top 25 Bigrams for %s and %s" %(cuisines[0],cuisines[1]), fontsize=30)
 plt.gcf().set_size_inches(30,30)
 plt.show()
plt.savefig('network.png' )<define_variables>
classifier = VotingClassifier(estimators=[('XGB', pipeline_xgb),('LOG', pipeline_log)])

cv = StratifiedKFold(5, shuffle=True, random_state=42)
 accuracies = cross_val_score(classifier, x , y , cv = cv)
 print("5 fold cross validation accuracies {}".format(accuracies))
<define_variables><EOS> 
classifier.fit(x,y)
 y_submit=pd.Series(( classifier.predict(pred_set)))
 y_submit=y_submit.astype(int)
 y_1=pred_set_original.PassengerId
y_submit_f=pd.concat([y_1,y_submit],axis=1)
 y_submit_f.rename(columns={ 0 :'Survived'}, inplace=True)
 y_submit_f.to_csv('submission.csv',index=False )