Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_output>
class KaggleMember() :
kaggle_member_count=0
def __init__(self, name, surname, level=None):
self.name=name.capitalize()
self.surname=surname.upper()
self._set_level(level)
 KaggleMember.kaggle_member_count+=1
self.kaggle_id=KaggleMember.kaggle_member_count
def display_number_of_member(self):
print("There are {} members in Kaggle community".format(KaggleMember.kaggle_member_count))
def display_member_info(self):
print("Kaggle Member Full Name:{} {}".format(self.name, self.surname))
print("Level:{:15}".format(self.level))
print("Kaggle ID:",self.kaggle_id)
 def _set_level(self, level):
if level is None:
self.level='Novice'
else:
self.level=level.title()
myclass_name='Kaggle Member'
sub['target'] = pred<save_to_csv>
warnings.filterwarnings('ignore')
 print("Warnings were ignored" )
sub.to_csv('submission.csv',index=False )<set_options>
class Information() :
def __init__(self):

 print("Information object created")
 def _get_missing_values(self,data):

 missing_values = data.isnull().sum()
missing_values.sort_values(ascending=False, inplace=True)
 return missing_values
def info(self,data):

 feature_dtypes=data.dtypes
self.missing_values=self._get_missing_values(data)
 print("=" * 50)
 print("{:16} {:16} {:25} {:16}".format("Feature Name".upper() ,
"Data Format".upper() ,
"
"Samples".upper()))
for feature_name, dtype, missing_value in zip(self.missing_values.index.values,
feature_dtypes[self.missing_values.index.values],
self.missing_values.values):
print("{:18} {:19} {:19} ".format(feature_name, str(dtype), str(missing_value)) , end="")
 for v in data[feature_name].values[:10]:
print(v, end=",")
 print()
print("="*50)
SEED = 42
torch.manual_seed(SEED)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False<load_from_csv>
class Preprocess() :
def __init__(self):
print("Preprocess object created")
 def fillna(self, data, fill_strategies):
for column, strategy in fill_strategies.items() :
if strategy == 'None':
data[column] = data[column].fillna('None')
 elif strategy == 'Zero':
data[column] = data[column].fillna(0)
 elif strategy == 'Mode':
data[column] = data[column].fillna(data[column].mode() [0])
 elif strategy == 'Mean':
data[column] = data[column].fillna(data[column].mean())
 elif strategy == 'Median':
data[column] = data[column].fillna(data[column].median())
 else:
print("{}: There is no such thing as preprocess strategy".format(strategy))
return data
def drop(self, data, drop_strategies):
for column, strategy in drop_strategies.items() :
data=data.drop(labels=[column], axis=strategy)
 return data
def feature_engineering(self, data, engineering_strategies=1):
if engineering_strategies==1:
return self._feature_engineering1(data)
 return data
def _feature_engineering1(self,data):
data=self._base_feature_engineering(data)
 data['FareBin'] = pd.qcut(data['Fare'], 4)
 data['AgeBin'] = pd.cut(data['Age'].astype(int), 5)
 drop_strategy = {'Age': 1,
'Name': 1,
'Fare': 1}
data = self.drop(data, drop_strategy)
 return data
def _base_feature_engineering(self,data):
data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['IsAlone'] = 1
data.loc[(data['FamilySize'] > 1), 'IsAlone'] = 0
data['Title'] = data['Name'].str.split(", ", expand=True)[1].str.split('.', expand=True)[0]
min_lengtht = 10
title_names =(data['Title'].value_counts() < min_lengtht)
 data['Title'] = data['Title'].apply(lambda x: 'Misc' if title_names.loc[x] == True else x)
 return data
def _label_encoder(self,data):
labelEncoder=LabelEncoder()
for column in data.columns.values:
if 'int64'==data[column].dtype or 'float64'==data[column].dtype or 'int64'==data[column].dtype:
continue
labelEncoder.fit(data[column])
 data[column]=labelEncoder.transform(data[column])
 return data
def _get_dummies(self, data, prefered_columns=None):
if prefered_columns is None:
columns=data.columns.values
non_dummies=None
else:
non_dummies=[col for col in data.columns.values if col not in prefered_columns ]
columns=prefered_columns
dummies_data=[pd.get_dummies(data[col],prefix=col)for col in columns]
if non_dummies is not None:
for non_dummy in non_dummies:
dummies_data.append(data[non_dummy])
 return pd.concat(dummies_data, axis=1 )
train_data = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 train_data.info()
train_data.sample(10 )<load_from_csv>
class PreprocessStrategy() :

 def __init__(self):
self.data=None
self._preprocessor=Preprocess()
def strategy(self, data, strategy_type="strategy1"):
self.data=data
if strategy_type=='strategy1':
self._strategy1()
elif strategy_type=='strategy2':
self._strategy2()
return self.data
def _base_strategy(self):
drop_strategy = {'PassengerId': 1,
'Cabin': 1,
'Ticket': 1}
self.data = self._preprocessor.drop(self.data, drop_strategy)
 fill_strategy = {'Age': 'Median',
'Fare': 'Median',
'Embarked': 'Mode'}
self.data = self._preprocessor.fillna(self.data, fill_strategy)
 self.data = self._preprocessor.feature_engineering(self.data, 1)
 self.data = self._preprocessor._label_encoder(self.data)
 def _strategy1(self):
self._base_strategy()
self.data=self._preprocessor._get_dummies(self.data,
prefered_columns=['Pclass', 'Sex', 'Parch', 'Embarked', 'Title', 'IsAlone'])
 def _strategy2(self):
self._base_strategy()
self.data=self._preprocessor._get_dummies(self.data,
prefered_columns=None )
test_data = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 test_data.info()
test_data.sample(10 )<train_model>
class GridSearchHelper() :
def __init__(self):
print("GridSearchHelper Created")
 self.gridSearchCV=None
self.clf_and_params=list()
self._initialize_clf_and_params()
def _initialize_clf_and_params(self):
clf= KNeighborsClassifier()
params={'n_neighbors':[5,7,9,11,13,15],
'leaf_size':[1,2,3,5],
'weights':['uniform', 'distance']
}
self.clf_and_params.append(( clf, params))
clf=LogisticRegression()
params={'penalty':['l1', 'l2'],
'C':np.logspace(0, 4, 10)
 }
self.clf_and_params.append(( clf, params))
clf = SVC()
params = [ {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']}]
self.clf_and_params.append(( clf, params))
clf=DecisionTreeClassifier()
params={'max_features': ['auto', 'sqrt', 'log2'],
'min_samples_split': [2,3,4,5,6,7,8,9,10,11,12,13,14,15],
'min_samples_leaf':[1],
'random_state':[123]}
self.clf_and_params.append(( clf,params))
clf = RandomForestClassifier()
params = {'n_estimators': [4, 6, 9],
'max_features': ['log2', 'sqrt','auto'],
'criterion': ['entropy', 'gini'],
'max_depth': [2, 3, 5, 10],
'min_samples_split': [2, 3, 5],
'min_samples_leaf': [1,5,8]
}
self.clf_and_params.append(( clf, params))
def fit_predict_save(self, X_train, X_test, y_train, submission_id, strategy_type):
self.X_train=X_train
self.X_test=X_test
self.y_train=y_train
self.submission_id=submission_id
self.strategy_type=strategy_type
clf_and_params = self.get_clf_and_params()
models=[]
self.results={}
for clf, params in clf_and_params:
self.current_clf_name = clf.__class__.__name__
grid_search_clf = GridSearchCV(clf, params, cv=5)
 grid_search_clf.fit(self.X_train, self.y_train)
 self.Y_pred = grid_search_clf.predict(self.X_test)
 clf_train_acc = round(grid_search_clf.score(self.X_train, self.y_train)* 100, 2)
 print(self.current_clf_name, " trained and used for prediction on test data...")
 self.results[self.current_clf_name]=clf_train_acc
models.append(clf)
 self.save_result()
print()

 def show_result(self):
for clf_name, train_acc in self.results.items() :
print("{} train accuracy is {:.3f}".format(clf_name, train_acc))
def save_result(self):
Submission = pd.DataFrame({'PassengerId': self.submission_id,
'Survived': self.Y_pred})
 file_name="{}_{}.csv".format(self.strategy_type,self.current_clf_name.lower())
 Submission.to_csv(file_name, index=False)
 print("Submission saved file name: ",file_name)
 def get_clf_and_params(self):
return self.clf_and_params
def add(self,clf, params):
self.clf_and_params.append(( clf, params))
print('Training Set Shape = {}'.format(train_data.shape))
print('Test Set Shape = {}'.format(test_data.shape))<count_unique_values>
class Visualizer:
def __init__(self):
print("Visualizer object created!")
 def RandianViz(self, X, y, number_of_features):
if number_of_features is None:
features=X.columns.values
else:
features=X.columns.values[:number_of_features]
fig, ax=plt.subplots(1, figsize=(15,12))
radViz=RadViz(classes=['survived', 'not survived'], features=features)
 radViz.fit(X, y)
 radViz.transform(X)
 radViz.poof()
mislabeled_df = train_data.groupby(['text'] ).nunique().sort_values(by='target', ascending=False)
 mislabeled_df = mislabeled_df[mislabeled_df['target'] > 1]['target']
mislabeled_list = mislabeled_df.index.tolist()
mislabeled_list<feature_engineering>
class ObjectOrientedTitanic() :
def __init__(self, train, test):

 print("ObjectOrientedTitanic object created")
 self.testPassengerID=test['PassengerId']
self.number_of_train=train.shape[0]
self.y_train=train['Survived']
self.train=train.drop('Survived', axis=1)
 self.test=test
self.all_data=self._get_all_data()
self._info=Information()
self.preprocessStrategy = PreprocessStrategy()
self.visualizer=Visualizer()
self.gridSearchHelper = GridSearchHelper()
def _get_all_data(self):
return pd.concat([self.train, self.test])
 def information(self):

 self._info.info(self.all_data)
 def preprocessing(self, strategy_type):

 self.strategy_type=strategy_type
self.all_data = self.preprocessStrategy.strategy(self._get_all_data() , strategy_type)
 def visualize(self, visualizer_type, number_of_features=None):
self._get_train_and_test()
if visualizer_type=="RadViz":
self.visualizer.RandianViz(X=self.X_train,
y=self.y_train,
number_of_features=number_of_features)
 def machine_learning(self):

 self._get_train_and_test()
self.gridSearchHelper.fit_predict_save(self.X_train,
self.X_test,
self.y_train,
self.testPassengerID,
self.strategy_type)
 def show_result(self):
self.gridSearchHelper.show_result()
def _get_train_and_test(self):

 self.X_train=self.all_data[:self.number_of_train]
self.X_test=self.all_data[self.number_of_train:]
train_data['target_relabeled'] = train_data['target'].copy()
train_data.loc[train_data['text'] == 'like for the music video I want some real action shit like burning buildings and police chases not some weak ben winston shit', 'target_relabeled'] = 0
train_data.loc[train_data['text'] == 'Hellfire is surrounded by desires so be careful and donÛªt let your desires control you!
train_data.loc[train_data['text'] == 'To fight bioterrorism sir.', 'target_relabeled'] = 0
train_data.loc[train_data['text'] == '.POTUS
train_data.loc[train_data['text'] == 'CLEARED:incident with injury:I-495 inner loop Exit 31 - MD 97/Georgia Ave Silver Spring', 'target_relabeled'] = 1
train_data.loc[train_data['text'] == '
train_data.loc[train_data['text'] == 'In
train_data.loc[train_data['text'] == 'Who is bringing the tornadoes and floods.Who is bringing the climate change.God is after America He is plaguing her
train_data.loc[train_data['text'] == 'RT NotExplained: The only known image of infamous hijacker D.B.Cooper.http://t.co/JlzK2HdeTG', 'target_relabeled'] = 1
train_data.loc[train_data['text'] == "Mmmmmm I'm burning.... I'm burning buildings I'm building.... Oooooohhhh oooh ooh...", 'target_relabeled'] = 0
train_data.loc[train_data['text'] == "wowo--=== 12000 Nigerian refugees repatriated from Cameroon", 'target_relabeled'] = 0
train_data.loc[train_data['text'] == "He came to a land which was engulfed in tribal war and turned it into a land of peace i.e.Madinah.
 train_data.loc[train_data['text'] == "Hellfire! We donÛªt even want to think about it or mention it so letÛªs not do anything that leads to it
train_data.loc[train_data['text'] == "The Prophet(peace be upon him)said 'Save yourself from Hellfire even if it is by giving half a date in charity.'", 'target_relabeled'] = 0
train_data.loc[train_data['text'] == "Caution: breathing may be hazardous to your health.", 'target_relabeled'] = 1
train_data.loc[train_data['text'] == "I Pledge Allegiance To The P.O.P.E.And The Burning Buildings of Epic City.??????", 'target_relabeled'] = 0
train_data.loc[train_data['text'] == "
train_data.loc[train_data['text'] == "that horrible sinking feeling when youÛªve been at home on your phone for a while and you realise its been on 3G this whole time", 'target_relabeled'] = 0<categorify>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 objectOrientedTitanic=ObjectOrientedTitanic(train, test)
<split><EOS> 
objectOrientedTitanic.machine_learning()