Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fit(12,model,optimizer,criteria )<load_from_csv>
train_dataset = pd.read_csv('.. /input/titanic/train.csv')
 train_dataset.head()
class prediction(Dataset):
def __init__(self,csv,transform):
self.test_data = pd.read_csv(csv)
 self.transform = transform
def __len__(self):
return len(self.test_data)
 def __getitem__(self,idx):
try:
image_path = os.path.join('.. /input/widsdatathon2019/leaderboard_test_data/leaderboard_test_data/',self.test_data.loc[idx]['image_id'])
 image = Image.open(image_path)
 except:
image_path = os.path.join('.. /input/widsdatathon2019/leaderboard_holdout_data/leaderboard_holdout_data/',self.test_data.loc[idx]['image_id'])
 image = Image.open(image_path)
 image = self.transform(image)
 name = self.test_data.loc[idx]['image_id']
return {'images':image,'names':name}<define_variables>
test_dataset = pd.read_csv('.. /input/titanic/test.csv')
 test_dataset.head()
predict_dataset = prediction('.. /input/widsdatathon2019/SampleSubmission.csv',simple_transform )<load_pretrained>
X = train_dataset[['Pclass','Sex','Age','Fare','SibSp','Parch', 'Embarked', 'Name', 'Cabin']]
print(X)
 y = train_dataset.iloc[:, 1].values
print(y[0:10])
 X_test = test_dataset[['Pclass','Sex','Age','Fare','SibSp','Parch', 'Embarked','Name','Cabin']]
print(X_test)
prediction_loader = DataLoader(predict_dataset )<set_options>
X['Age'] = X['Age'].fillna(( X['Age'].mean()))
X["Embarked"] = X["Embarked"].fillna("S")
 X.loc[X["Embarked"] == "S", "Embarked"] = 0
X.loc[X["Embarked"] == "C", "Embarked"] = 1
X.loc[X["Embarked"] == "Q", "Embarked"] = 2
X_test['Age'] = X_test['Age'].fillna(( X_test['Age'].mean()))
X_test["Embarked"] = X_test["Embarked"].fillna("S")
 X_test.loc[X_test["Embarked"] == "S", "Embarked"] = 0
X_test.loc[X_test["Embarked"] == "C", "Embarked"] = 1
X_test.loc[X_test["Embarked"] == "Q", "Embarked"] = 2
data = [X, X_test]
titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr','Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 dataset['Title'] = dataset['Title'].map(titles)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
dataset['Age'] = dataset['Age'].astype(int)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['Fare'] = dataset['Fare'].fillna(dataset['Fare'].median())
 dataset['Cabin'] = dataset['Cabin'].str[:1]
X = X.drop(['Name'], axis=1)
 X_test = X_test.drop(['Name'], axis=1)
 print(X)
 print(X_test)
gc.collect()<predict_on_test>
cabin_mapping = {"A": 0, "B": 0.4, "C": 0.8, "D": 1.2, "E": 1.6, "F": 2, "G": 2.4, "T": 2.8}
data = [X, X_test]
for dataset in data:
dataset['Cabin'] = dataset['Cabin'].map(cabin_mapping)
 X["Cabin"].fillna(X.groupby("Pclass")["Cabin"].transform("median"), inplace=True)
 X_test["Cabin"].fillna(X_test.groupby("Pclass")["Cabin"].transform("median"), inplace=True )
predict = []
model.eval()
for i, d in enumerate(prediction_loader):
data = d['images'].cuda()
output = model(data)
 output1 = output.cpu().detach().numpy()
predict.append(output1[0] )<load_from_csv>
X['FareBand'] = pd.qcut(X['Fare'], 4)
 X_test['FareBand'] = pd.qcut(X_test['Fare'], 4)
 data = [X, X_test]
for dataset in data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
submission = pd.read_csv('.. /input/widsdatathon2019/SampleSubmission.csv')
 submission['has_oilpalm'] = np.argmax(predict,1 )<save_to_csv>
data = [X, X_test]
for dataset in data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
print(X)
 print(X_test )
submission.to_csv('sample_submission.csv',index = False )<import_modules>
features_drop = [ 'SibSp', 'Parch', 'FamilySize', 'FareBand']
X = X.drop(features_drop, axis=1)
 X_test = X_test.drop(features_drop, axis=1)
 print(X)
 print(X_test )
import fastai
from fastai.vision import *<define_variables>
sc = StandardScaler()
X = sc.fit_transform(X)
 print('X')
 print(X)
 X_test = sc.fit_transform(X_test)
 print('X_test')
 print(X_test )
work_dir = Path('/kaggle/working/')
 path = Path('.. /input' )<define_variables>
classifier = LogisticRegression(random_state = 0)
 classifier.fit(X, y)
 y_pred_l_reg = classifier.predict(X_test)
 acc_l_reg = round(classifier.score(X, y)* 100, 2)
 print(str(acc_l_reg)+ ' percent')
train = 'train_images/train_images'
test = path/'leaderboard_test_data/leaderboard_test_data'
holdout = path/'leaderboard_holdout_data/leaderboard_holdout_data'
sample_sub = path/'SampleSubmission.csv'
labels = path/'traininglabels.csv'<load_from_csv>
clf = SVC()
clf.fit(X, y)
 y_pred_svc = clf.predict(X_test)
 acc_svc = round(clf.score(X, y)* 100, 2)
 print(acc_svc )
df = pd.read_csv(labels)
 df_sample = pd.read_csv(sample_sub )<filter>
clf = KNeighborsClassifier(n_neighbors = 3)
 clf.fit(X, y)
 y_pred_knn = clf.predict(X_test)
 acc_knn = round(clf.score(X, y)* 100, 2)
 print(acc_knn)
df[df['score']<0.75]<define_variables>
clf = DecisionTreeClassifier()
clf.fit(X, y)
 y_pred_decision_tree = clf.predict(X_test)
 acc_decision_tree = round(clf.score(X, y)* 100, 2)
 print(acc_decision_tree)
test_names = [f for f in test.iterdir() ]
holdout_names = [f for f in holdout.iterdir() ]<normalization>
clf = RandomForestClassifier(n_estimators=100)
 clf.fit(X, y)
 y_pred_random_forest = clf.predict(X_test)
 acc_random_forest = round(clf.score(X, y)* 100, 2)
 print(acc_random_forest )
data =(src.transform(get_transforms() , size=128)
.databunch(bs=64)
.normalize(imagenet_stats))<compute_test_metric>
sgd = SGDClassifier()
sgd.fit(X, y)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X, y)* 100, 2)
 print(acc_sgd )
<choose_model_class><EOS> 
output = pd.DataFrame({'PassengerId': test_dataset.PassengerId, 'Survived': y_pred_l_reg})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved" )