Kaggle competition: Titanic - Machine Learning from Disaster
%%time
classifier.fit(x_train, y_train )<categorify>
%matplotlib inline
sns.set()
warnings.filterwarnings('ignore' )
y_pred = label_encoder.inverse_transform(classifier.predict(x_train))
y_true = label_encoder.inverse_transform(y_train)
 print(f'accuracy score on train data: {accuracy_score(y_true, y_pred)}' )<save_to_csv>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )
y_pred = label_encoder.inverse_transform(classifier.predict(x_test))
test['cuisine'] = y_pred
test[['id', 'cuisine']].to_csv('submission.csv', index=False)
 test[['id', 'cuisine']].head()<load_from_disk>
train.isnull().sum()
recipe_data = json.loads(open('.. /input/train.json' ).read() )<create_dataframe>
test.isnull().sum()
colnames = list(unique_ing)
 data = pd.DataFrame(0, index=recipe_id, columns=colnames )<feature_engineering>
train.drop(columns=['PassengerId','Name','Ticket','Cabin'], axis=1, inplace=True)
 test.drop(columns=['PassengerId','Name','Ticket','Cabin'], axis=1, inplace=True )
data['cuisine'] = cuisine<feature_engineering>
train['Sex'] = [1 if gender=='male' else 0 for gender in train['Sex']]
test['Sex'] = [1 if gender=='male' else 0 for gender in test['Sex']]
for recipe in recipe_data:
index = recipe['id']
ingredients =recipe['ingredients']
for ingredient in ingredients:
data.at[index, ingredient] = 1<prepare_x_and_y>
train.Age.fillna(train.Age.median() , inplace=True)
 test.Age.fillna(test.Age.median() , inplace=True )
y, label = pd.factorize(data['cuisine'] )<data_type_conversions>
train.drop(columns=['Age'], inplace=True)
 test.drop(columns=['Age'], inplace=True )
X_train = data[colnames].values.astype(float)
 y_train = keras.utils.to_categorical(y, num_classes=20 )<choose_model_class>
test.Fare.fillna(test.Fare.median() , inplace=True )
model = Sequential()
model.add(Dropout(0.3))
model.add(Dense(512, input_dim=6714, activation='linear'))
model.add(LeakyReLU(alpha=.02))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(20, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='Adamax', metrics=['accuracy'] )<train_model>
train.drop(columns=['Fare'], axis=1, inplace=True)
 test.drop(columns=['Fare'], axis=1, inplace=True )
model.fit(X_train, y_train,
epochs=25,
batch_size=250)
<load_from_disk>
train.Embarked.fillna(train.Embarked.mode() , inplace=True )
test_data = json.loads(open('.. /input/test.json' ).read() )<create_dataframe>
train = pd.get_dummies(train, columns=['Pclass','AgeGroup','Embarked'])
 test = pd.get_dummies(test, columns=['Pclass','AgeGroup','Embarked'] )
test_recipe_id = []
for recipe in test_data:
test_recipe_id.append(recipe['id'])
 test_df = pd.DataFrame(0, index=test_recipe_id, columns=colnames )<define_variables>
predictors = train.drop(columns=['Survived'], axis=1 )
ingr_checker = dict.fromkeys(colnames )<feature_engineering>
target = train[['Survived']]
for recipe in test_data:
index = recipe['id']
ingredients = recipe['ingredients']
for ingredient in ingredients:
if ingredient in ingr_checker:
test_df.at[index, ingredient] = 1<data_type_conversions>
from sklearn.model_selection import train_test_split
X_test = test_df[colnames].values.astype(float )<predict_on_test>
from sklearn.model_selection import train_test_split
prediction = model.predict(X_test )<prepare_output>
x_train,x_val,y_train,y_val = train_test_split(predictors,target,test_size=0.2,random_state=123 )
prediction_classes = prediction.argmax(axis=-1 )<prepare_output>
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.svm import SVC
label_names = label[prediction_classes]
df_output = pd.DataFrame({'id' : test_recipe_id, 'cuisine' : label_names})
 df_output.head()<save_to_csv>
from sklearn.metrics import accuracy_score
df_output.to_csv("output.csv", header=True, index=False )<import_modules>
lr = LogisticRegression()
lr.fit(x_train,y_train)
 preds = lr.predict(x_val)
 lr_accuracy = accuracy_score(y_val,preds)
 print(f'Logistic Regression accuracy: {lr_accuracy*100}' )
import os
import json
import re
import pandas as pd
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import FunctionTransformer<categorify>
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
 preds = knn.predict(x_val)
 knn_accuracy = accuracy_score(y_val, preds)
 print(f'KNN accuracy: {knn_accuracy*100}' )
def get_replacements() :
return {'wasabe': 'wasabi', '-': '', 'sauc': 'sauce',
'baby spinach': 'babyspinach', 'coconut cream': 'coconutcream',
'coriander seeds': 'corianderseeds', 'corn tortillas': 'corntortillas',
'cream cheese': 'creamcheese', 'fish sauce': 'fishsauce',
'purple onion': 'purpleonion','refried beans': 'refriedbeans',
'rice cakes': 'ricecakes', 'rice syrup': 'ricesyrup',
'sour cream': 'sourcream', 'toasted sesame seeds': 'toastedsesameseeds',
'toasted sesame oil': 'toastedsesameoil', 'yellow onion': 'yellowonion'}<string_transform>
dt = DecisionTreeClassifier()
dt.fit(x_train,y_train)
 preds = dt.predict(x_val)
 dt_accuracy = accuracy_score(y_val, preds)
 print(f'Decission Tree accuracy: {dt_accuracy*100}' )
def tranform_to_single_string(ingredients, lemmatizer, replacements, stop_pattern):
ingredients_text = ' '.join(iter(ingredients))
for key, value in replacements.items() :
ingredients_text = ingredients_text.replace(key, value)
 words = []
for word in ingredients_text.split() :
if not stop_pattern.match(word)and len(word)> 2:
word = lemmatizer.lemmatize(word)
 words.append(word)
 return ' '.join(words )<choose_model_class>
rf = RandomForestClassifier()
rf.fit(x_train,y_train)
 preds = rf.predict(x_val)
 rf_accuracy = accuracy_score(y_val, preds)
 print(f'RandomForest accuracy: {rf_accuracy*100}' )
def get_estimator() :
return SVC(C=300,
kernel='rbf',
gamma=1.5,
shrinking=True,
tol=0.001,
cache_size=1000,
class_weight=None,
max_iter=-1,
decision_function_shape='ovr',
random_state=42 )<feature_engineering>
gbc = GradientBoostingClassifier()
gbc.fit(x_train,y_train)
 preds = gbc.predict(x_val)
 gbc_accuracy = accuracy_score(y_val, preds)
 print(f'GradientBoostClassifier accuracy: {gbc_accuracy*100}' )
def show_unique_ingredients(train):
ingredients = {}
for idx, row in train.iterrows() :
for ingredient in row['ingredients']:
if ingredient not in ingredients:
ingredients[ingredient] = {'sum': 0}
previous = ingredients[ingredient][row['cuisine']] if row['cuisine'] in ingredients[ingredient] else 0
ingredients[ingredient][row['cuisine']] = 1 + previous
ingredients[ingredient]['sum'] += 1
for ingredient in sorted(ingredients):
for cuisine in sorted(ingredients[ingredient], key=ingredients[ingredient].get, reverse=True):
print(f'{ingredient}:{cuisine}:{ingredients[ingredient][cuisine]}' )<feature_engineering>
svc = SVC()
svc.fit(x_train,y_train)
 preds = svc.predict(x_val)
 svc_accuracy = accuracy_score(y_val, preds)
 print(f'SVC accuracy: {svc_accuracy*100}' )
def preprocess(train, test):
lemmatizer = WordNetLemmatizer()
replacements = get_replacements()
train['ingredients'] = train['ingredients'].apply(lambda x: list(map(lambda y: y.lower() , x)))
 test['ingredients'] = test['ingredients'].apply(lambda x: list(map(lambda y: y.lower() , x)))
 stop_pattern = re.compile('[\d’%]')
 transform = lambda ingredients: tranform_to_single_string(ingredients, lemmatizer, replacements, stop_pattern)
 train['x'] = train['ingredients'].apply(transform)
 test['x'] = test['ingredients'].apply(transform)
 vectorizer = make_pipeline(
 TfidfVectorizer(sublinear_tf=True),
FunctionTransformer(lambda x: x.astype('float'), validate=False)
)
 x_train = vectorizer.fit_transform(train['x'].values)
 x_train.sort_indices()
x_test = vectorizer.transform(test['x'].values)
 return x_train, x_test<choose_model_class>
models = pd.DataFrame({'Model':['LogisticRegression','KNN','DecissionTree','RandomForest','GradientBoostClassifier','SVM'],
'Accuracy':[lr_accuracy*100,knn_accuracy*100,dt_accuracy*100,rf_accuracy*100,gbc_accuracy*100,svc_accuracy*100]})
 models
%%time
def main() :
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 train['num_ingredients'] = train['ingredients'].apply(lambda x: len(x))
test['num_ingredients'] = test['ingredients'].apply(lambda x: len(x))
train = train[train['num_ingredients'] > 2]
x_train, x_test = preprocess(train, test)
 estimator = get_estimator()
y_train = train['cuisine'].values
classifier = OneVsRestClassifier(estimator, n_jobs=-1)
 classifier.fit(x_train, y_train)
 test['cuisine'] = classifier.predict(x_test)
 test[['id', 'cuisine']].to_csv('submission.csv', index=False)
 main()<set_options>
data = pd.read_csv('/kaggle/input/titanic/test.csv')
 ids = data['PassengerId']
%matplotlib inline
init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 print(os.listdir(".. /input"))<load_from_disk>
preds = dt.predict(test )
train_data = pd.read_json('.. /input/train.json')
 test_data = pd.read_json('.. /input/test.json' )<train_model>
output = pd.DataFrame({'PassengerId':ids, 'Survived':preds} )
print("The training data consists of {} recipes".format(len(train_data)) )<count_unique_values>
output.to_csv('submission.csv', index=False )