Kaggle competition: Titanic - Machine Learning from Disaster
%matplotlib inline<load_from_disk>
pd.options.mode.chained_assignment = None
%matplotlib inline
warnings.filterwarnings('ignore' )
rawdf_te = pd.read_json(path_or_buf='.. /input/test.json')
 rawdf_te.head(n=3)
 rawdf_tr = pd.read_json(path_or_buf='.. /input/train.json')
 rawdf_tr.head(n=3 )<filter>
train_data = pd.read_csv(".. /input/titanic/train.csv", index_col="PassengerId")
 test_data = pd.read_csv(".. /input/titanic/test.csv", index_col="PassengerId" )
print(rawdf_tr['ingredients'].loc[41935])
 print(rawdf_tr['ingredients'].loc[27566])
 print(rawdf_tr['ingredients'].loc[32596])
 print(rawdf_tr['ingredients'].loc[8476] )<create_dataframe>
print(train_data.isna().sum() )
ingredients_tr = rawdf_tr['ingredients']
ingredients_te = rawdf_te['ingredients']<feature_engineering>
print(test_data.isna().sum() )
def sub_match(pattern, sub_pattern, ingredients):
for i in ingredients.index.values:
for j in range(len(ingredients[i])) :
ingredients[i][j] = re.sub(pattern, sub_pattern, ingredients[i][j].strip())
 ingredients[i][j] = ingredients[i][j].strip()
re.purge()
return ingredients
def regex_sub_match(series):
p0 = re.compile(r'\s*(oz|ounc|ounce|pound|lb|inch|inches|kg|to)\s*[^a-z]')
 series = sub_match(p0, ' ', series)
 p1 = re.compile(r'\d+')
 series = sub_match(p1, ' ', series)
 p2 = re.compile('[^\w]')
 series = sub_match(p2, ' ', series)
 return series<feature_engineering>
print(train_data["Pclass"].unique())
 train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
lemmatizer = WordNetLemmatizer()
def lemma(series):
for i in series.index.values:
for j in range(len(series[i])) :
series[i][j] = series[i][j].strip()
token = TK(series[i][j])
 for k in range(len(token)) :
token[k] = lemmatizer.lemmatize(token[k])
 token = ' '.join(token)
 series[i][j] = token
return series<categorify>
train_data.Name[1].split()
ingredients_tr = lemma(ingredients_tr)
 ingredients_te = lemma(ingredients_te )<feature_engineering>
train_data = train_data.assign(fname = train_data.Name.str.split("," ).str[0])
 train_data["title"] = pd.Series([i.split(",")[1].split(".")[0].strip() for i in train_data.Name], index=train_data.index )
rawdf_tr['ingredients_lemma'] = ingredients_tr
rawdf_tr['ingredients_lemma_string'] = [' '.join(_ ).strip() for _ in rawdf_tr['ingredients_lemma']]
rawdf_te['ingredients_lemma'] = ingredients_te
rawdf_te['ingredients_lemma_string'] = [' '.join(_ ).strip() for _ in rawdf_te['ingredients_lemma']]<create_dataframe>
test_data = test_data.assign(fname = test_data.Name.str.split("," ).str[0])
 test_data["title"] = pd.Series([i.split(",")[1].split(".")[0].strip() for i in test_data.Name], index=test_data.index)
 train_data.drop("Name", axis=1, inplace=True)
 test_data.drop("Name", axis=1, inplace=True )
traindf = rawdf_tr[['cuisine', 'ingredients_lemma_string']].reset_index(drop=True)
 testdf = rawdf_te[['ingredients_lemma_string']]<prepare_x_and_y>
print(test_data.fname.nunique())
 print(test_data.title.nunique() )
X_train = traindf['ingredients_lemma_string']
vectorizertr = TfidfVectorizer(stop_words='english', analyzer="word", max_df=0.65, min_df=2, binary=True)
 X_train = vectorizertr.fit_transform(X_train)
 y_train = traindf['cuisine']
le = LE()
y_train_ec = le.fit_transform(y_train)
 X_pred = testdf['ingredients_lemma_string']
vectorizerts = TfidfVectorizer(stop_words='english')
 X_pred = vectorizertr.transform(X_pred)
<save_to_csv>
train_data["title"] = train_data['title'].replace(other_titles, 'Other')
 train_data["title"] = train_data["title"].map({"Mr":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Master":2, "Other":3})
 test_data["title"] = test_data['title'].replace(other_titles, 'Other')
 test_data["title"] = test_data["title"].map({"Mr":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Master":2, "Other":3} )
clf_ovrc_svm = SVC(C=3.25, cache_size=500, class_weight=None, coef0=0.0,\
decision_function_shape='ovr', degree=3, gamma=1, kernel='rbf',\
max_iter=-1, probability=False, random_state=0, shrinking=True,\
tol=0.001, verbose=False)
 clf_ovrc_svm = clf_ovrc_svm.fit(X_train, y_train)
 y_pred_ovrc_svm = clf_ovrc_svm.predict(X_pred)
 testdf['cuisine'] = y_pred_ovrc_svm
d = pd.DataFrame(data=testdf['cuisine'], index=testdf.index ).sort_index().reset_index().to_csv('submission_ovr_svm.csv', index=False )<prepare_x_and_y>
print(train_data.title)
 print(test_data.title.isna().sum() )
x=pd.read_json('.. /input/train.json')
 y=pd.read_json('.. /input/test.json' )<import_modules>
oh = OneHotEncoder(handle_unknown="ignore", sparse = False)
 train_data = train_data.join(pd.DataFrame(oh.fit_transform(train_data[["fname", "title"]]), index = train_data.index))
test_data = test_data.join(pd.DataFrame(oh.transform(test_data[["fname", "title"]]), index = test_data.index))
train_data.drop("fname", axis = 1, inplace = True)
 test_data.drop("fname", axis = 1, inplace = True )
z=x['cuisine'] 
<string_transform>
print(train_data["Sex"].unique())
 train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
x['separated_ing']=x['ingredients'].map(lambda x: ' '.join(x))
y['separated_ing']=y['ingredients'].map(lambda x: ' '.join(x)) 
<feature_engineering>
interactions = train_data.assign(sex_class = train_data['Sex'] + "_" + train_data['Pclass'].astype("str"))
interactions[['sex_class', 'Survived']].groupby(['sex_class'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
def purify(f):
f=f.lower()
f=re.sub('[%s]' % re.escape(string.punctuation),'',f)
 f=re.sub('\s+',' ',f)
 return f
x['cleared_ing']=x['separated_ing'].map(lambda g :purify(g))
y['cleared_ing']=y['separated_ing'].map(lambda g :purify(g))<feature_engineering>
train_data = train_data.assign(sex_class = train_data['Sex'] + "_" + train_data['Pclass'].astype("str"))
test_data = test_data.assign(sex_class = test_data['Sex'] + "_" + test_data['Pclass'].astype("str"))
sb=SnowballStemmer('english')
 def stemmer(f):
lists=[sb.stem(c)for c in f.split(" ")]
return lists
l=WordNetLemmatizer()
def lemmar(f):
lists=[l.lemmatize(g)for g in f.split(" ")]
return lists
x['separated_ing_stemmed']=[stemmer(l)for l in x['cleared_ing']]
x['separated_ing_stemmed']=x['separated_ing_stemmed'].map(lambda x: ' '.join(x))
x['separated_ing_lemma']=[lemmar(l)for l in x['separated_ing_stemmed']]
x['separated_ing_lemma']=x['separated_ing_lemma'].map(lambda x: ' '.join(x))
y['separated_ing_stemmed']=[stemmer(l)for l in y['cleared_ing']]
y['separated_ing_stemmed']=y['separated_ing_stemmed'].map(lambda x: ' '.join(x))
y['separated_ing_lemma']=[lemmar(l)for l in y['separated_ing_stemmed']]
y['separated_ing_lemma']=y['separated_ing_lemma'].map(lambda x: ' '.join(x))<prepare_x_and_y>
train_data["Sex"] = train_data["Sex"].map({"female":0, "male":1})
 test_data["Sex"] = test_data["Sex"].map({"female":0, "male":1} )
x.columns
x=x.drop(['ingredients','separated_ing','cleared_ing','separated_ing_stemmed'],axis=1)
 y=y.drop(['ingredients','separated_ing','cleared_ing','separated_ing_stemmed'],axis=1 )<string_transform>
train_data["sex_class"] = train_data["sex_class"].map({"female_1":0, "female_2":1, "female_3":2, "male_1":4, "male_2":5, "male_3":6})
 test_data["sex_class"] = test_data["sex_class"].map({"female_1":0, "female_2":1, "female_3":2, "male_1":4, "male_2":5, "male_3":6} )
lists=list(ENGLISH_STOP_WORDS)+stopwords.words()<import_modules>
def find_similar_passengers(id, dataset):
subset = dataset[(dataset.title == dataset.title[id])& 
(dataset.Pclass == dataset.Pclass[id])]
if subset["Age"].mean() == "NaN":
subset = dataset[(dataset["sex_class"] == dataset.iloc[id]["sex_class"])]
if subset["Age"].mean() == "NaN":
subset = dataset[(dataset["sex"] == dataset.iloc[id]["sex"])]
age = subset["Age"].mean()
return age
from sklearn.feature_extraction.text import TfidfVectorizer as tfidf,CountVectorizer as cv<define_variables>
no_ages = train_data[train_data["Age"].isna() ].index
for pid in no_ages:
train_data.Age[pid] = find_similar_passengers(pid, train_data)
 no_ages_test = test_data[test_data["Age"].isna() ].index
for pid2 in no_ages_test:
test_data.Age[pid2] = find_similar_passengers(pid2, test_data )
z=x['cuisine']<categorify>
train_data["age_group"] = pd.cut(train_data["Age"], bins=[0,5,65,100], labels=[0,1,2] ).astype("int64")
 test_data["age_group"] = pd.cut(test_data["Age"], bins=[0,5,65,100], labels=[0,1,2] ).astype("int64" )
tfidf1=tfidf(max_df=0.9,stop_words=lists,analyzer=u'word')
 train=tfidf1.fit_transform(x['separated_ing_lemma'])
 test=tfidf1.transform(y['separated_ing_lemma'] )<import_modules>
train_data[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV as gsc
from sklearn.ensemble import RandomForestClassifier
from xgboost.sklearn import XGBClassifier as xgb
from lightgbm import LGBMClassifier as lgb
from sklearn.linear_model import LogisticRegression as lr<set_options>
train_data[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
svm={'C':[6]} 
<train_model>
train_data["fsize"] = train_data["SibSp"] + train_data["Parch"] + 1
test_data["fsize"] = test_data["SibSp"] + test_data["Parch"] + 1
p=le().fit(z )<categorify>
train_data[['fsize', 'Survived']].groupby(['fsize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
z=p.transform(z )<split>
print(train_data.Ticket.nunique())
 print(train_data.Ticket.tail() )
xtrain,xtest,ztrain,ztest=tts(train,z,train_size=0.7 )<choose_model_class>
train_data["ticket_prefix"] = pd.Series([len(i.split())> 1 for i in train_data.Ticket], index=train_data.index)
r1=lgb(n_estimators=500,max_depth=7,objective='multiclass',metric='multi_logloss',num_classes=20,bagging_fraction=0.6,feature_fraction=0.6 )<set_options>
train_data[['ticket_prefix', 'Survived']].groupby(['ticket_prefix'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
a=gsc(lr() ,svm )<set_options>
train_data.drop("ticket_prefix", axis=1, inplace=True)
 train_data.drop("Ticket", axis=1, inplace=True)
 test_data.drop("Ticket", axis=1, inplace=True )
k={'n_neighbors':[5,7,9]}
k1=gsc(knn() ,k )<import_modules>
train_data["Embarked"] = train_data["Embarked"].fillna("S")
 train_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
from sklearn.multiclass import OneVsRestClassifier as orc
from sklearn.ensemble import VotingClassifier as vc 
<choose_model_class>
train_data["Embarked"] = train_data["Embarked"].fillna("S")
 print(train_data.Embarked.isna().sum() )
v=vc(estimators=[('lr',a),('k1',k1),('lg',r1)],voting='soft' )<train_model>
train_data = train_data.join(pd.get_dummies(train_data['Embarked'], prefix="Embarked_"))
test_data = test_data.join(pd.get_dummies(test_data['Embarked'], prefix="Embarked_"))
v.fit(xtrain,ztrain )<compute_test_metric>
train_data.drop("Embarked", axis=1, inplace=True)
 test_data.drop("Embarked", axis=1, inplace=True )
print(accuracy_score(ztest,v.predict(xtest)) )<predict_on_test>
train_data.drop("Cabin", axis=1, inplace=True)
 test_data.drop("Cabin", axis=1, inplace=True )
z1=v.predict(test )<feature_engineering>
ss = StandardScaler()
train_y = train_data["Survived"]
train_data.drop("Survived", axis=1, inplace=True)
 scoring_method = "f1"
train_scaled = ss.fit_transform(train_data)
 test_scaled = ss.transform(test_data )
z=p.inverse_transform(z1 )<create_dataframe>
print(train_data.isna().sum())
 print(test_data.isna().sum() )
ff=pd.DataFrame(z,index=y['id'],columns=['cuisine'] )<rename_columns>
model = LogisticRegression(random_state=10, max_iter = 1000)
 logit_params = {
"C": [1, 3, 10, 20, 30, 40],
"solver": ["lbfgs", "liblinear"]
}
logit_gs = GridSearchCV(model, logit_params, scoring="f1", cv = 5, n_jobs=4)
ff.index.name='id'<save_to_csv>
logit_gs.fit(train_data, train_y )
ff.to_csv('aagya.csv' )<set_options>
print(logit_gs.best_params_)
 print(logit_gs.best_score_ )
Image(".. /input/ratatouille/ratatouille.jpg" )<set_options>
svc_model = SVC()
test_parameters = {
"C": [1, 3, 10, 30, 100],
"kernel": ["linear", "poly", "rbf" , "sigmoid"],
}
svc_gs = GridSearchCV(svc_model, test_parameters, scoring="f1", cv=5, n_jobs=4 )
%matplotlib inline
plt.rcParams["figure.figsize"] =(15,10 )<load_from_disk>
svc_gs.fit(train_scaled, train_y )
data = pd.read_json(".. /input/whats-cooking-kernels-only/train.json")
 test = pd.read_json(".. /input/whats-cooking-kernels-only/test.json")
 data.head()<count_missing_values>
print(svc_gs.best_params_)
 print(svc_gs.best_score_ )
data.isnull().values.any()
data.dropna(axis=0, how='any',inplace = True)
 data.isnull().values.any()
data.isnull().sum()<string_transform>
lgb_model = LGBMClassifier()
test_parameters = {
"n_estimators": [int(x)for x in np.linspace(5, 30, 6)],
"reg_alpha": [0, 0.75, 1, 1.25],
"learning_rate": [0.5, 0.4, 0.35, 0.3, 0.25, 0.2],
"subsample": [0.5, 0.75, 1]
}
lgb_gs = GridSearchCV(lgb_model, test_parameters, scoring=scoring_method, cv=8, n_jobs=4 )
data.ingredients =data.ingredients.str.join(' ')
 test.ingredients =test.ingredients.str.join(' ' )<feature_engineering>
lgb_gs.fit(train_data, train_y )
vect = HashingVectorizer()
features = vect.fit_transform(data.ingredients)
 testfeatures = vect.transform(test.ingredients )<define_variables>
print(lgb_gs.best_params_)
 print(lgb_gs.best_score_ )
labels = data.cuisine<split>
rf_model = RandomForestClassifier()
rf_params ={
'bootstrap': [True, False],
'max_depth': [10, None],
'max_features': ['auto', 'sqrt'],
'min_samples_leaf': [1, 2, 4],
'min_samples_split': [2, 5, 10],
'n_estimators': [5, 10, 15, 20, 25, 30]}
rf_gs = GridSearchCV(rf_model, rf_params, scoring=scoring_method, cv=8, n_jobs=4 )
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2 )<train_model>
rf_gs.fit(train_data, train_y )
start = time.time()
log_reg = LogisticRegression(C=12)
 log_reg.fit(X_train,y_train)
 print("Accuracy: ",log_reg.score(X_test, y_test))
print("Time: " , time.time() - start )<train_model>
print(rf_gs.best_params_)
 print(rf_gs.best_score_ )
start = time.time()
linear_svm = LinearSVC(random_state=0, max_iter = 1500)
 linear_svm.fit(X_train, y_train)
 print("Accuracy: ",linear_svm.score(X_test, y_test))
print("Time: " , time.time() - start )<train_model>
ensemble_model = VotingClassifier(estimators=[ 
("logit", logit_gs.best_estimator_), 
("rf", rf_gs.best_estimator_), 
("svc", svc_gs.best_estimator_), 
("lgb", lgb_gs.best_estimator_),
], voting = "hard" )
start = time.time()
rbf_svm = SVC(kernel='rbf', gamma=0.8, C=12)
 rbf_svm.fit(X_train, y_train)
 print("Accuracy: ",rbf_svm.score(X_test, y_test))
print("Time: " , time.time() - start )<save_to_csv>
ensemble_model.fit(train_data, train_y )
prediction = rbf_svm.predict(testfeatures)
 sub = pd.DataFrame({'id':test.id,'cuisine':prediction})
 output = sub[['id','cuisine']]
output.to_csv("sample_submission.csv",index = False )<import_modules>
ensemble_model.score(train_data, train_y )
import numpy as np
import pandas as pd
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize
from nltk.stem import PorterStemmer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import LabelEncoder
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfVectorizer,HashingVectorizer<load_from_disk>
preds = ensemble_model.predict(test_data )
df = pd.read_json(".. /input/train.json")
 testset = pd.read_json(".. /input/test.json" )<count_missing_values>
output = pd.DataFrame({'PassengerId': test_data.index,
'Survived': preds})
 output.to_csv('submission.csv', index=False )