Kaggle competition: Titanic - Machine Learning from Disaster
submit_predictions(pred )<set_options>
print(f'Training path:{__training_path}
Test path:{__test_path}' )
%matplotlib inline<set_options>
!{sys.executable} -m pip install --upgrade scikit-learn=="0.24.2"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )<load_from_csv>
import sklearn; sklearn.show_versions()
df = pd.read_csv('.. /input/widsdatathon2019/traininglabels.csv')
 df.head()<feature_engineering>
def __load__data(__training_path, __test_path, concat=False):
	
	
	
	__train_dataset = pd.read_csv(__training_path, delimiter=',')
 	__test_dataset = pd.read_csv(__test_path, delimiter=',')
 	return __train_dataset, __test_dataset
__train_dataset, __test_dataset = __load__data(__training_path, __test_path, concat=True)
 __train_dataset.head()
class OilDataset(Dataset):
def __init__(self,csv,transform):
self.data = pd.read_csv(csv)
 self.transform = transform
self.label = torch.eye(2)[self.data['has_oilpalm']]
def __len__(self):
return len(self.data)
 def __getitem__(self,idx):
image_path = os.path.join('.. /input/widsdatathon2019/train_images/train_images/'+self.data.loc[idx,'image_id'])
 image = Image.open(image_path)
 image = self.transform(image)
 label = torch.tensor(self.data.loc[idx,'has_oilpalm'])
 return {'image':image,'labels':label}<load_pretrained>
__test_dataset_submission_columns = __test_dataset['PassengerId']
k = Image.open('.. /input/widsdatathon2019/train_images/train_images/img_000012017.jpg')
 k.show()<categorify>
__train_dataset.drop(['PassengerId'], axis=1, inplace=True)
 __test_dataset.drop(['PassengerId'], axis=1, inplace=True )
simple_transform = transforms.Compose([transforms.Resize(( 224,224)) ,transforms.RandomHorizontalFlip() ,transforms.RandomRotation(45),
transforms.ToTensor()
,transforms.Normalize([0.486,0.456,0.406],[0.229,0.225,0.224])] )<create_dataframe>
_NUMERIC_COLS_WITH_MISSING_VALUES = ['Age', 'Fare', 'Parch', 'Pclass', 'SibSp']
for _col in _NUMERIC_COLS_WITH_MISSING_VALUES:
__simple_imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
 __train_dataset[_col] = __simple_imputer.fit_transform(__train_dataset[_col].values.reshape(-1,1)) [:,0]
if _col in __test_dataset:
__test_dataset[_col] = __simple_imputer.transform(__test_dataset[_col].astype(__train_dataset[_col].dtypes ).values.reshape(-1,1)) [:,0]
train_dataset = OilDataset('.. /input/widsdatathon2019/traininglabels.csv',simple_transform )<define_variables>
_STRING_COLS_WITH_MISSING_VALUES = ['Cabin', 'Ticket', 'Sex', 'Name', 'Embarked']
for _col in _STRING_COLS_WITH_MISSING_VALUES:
__simple_imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
 __train_dataset[_col] = __simple_imputer.fit_transform(__train_dataset[_col].values.reshape(-1,1)) [:,0]
if _col in __test_dataset:
__test_dataset[_col] = __simple_imputer.transform(__test_dataset[_col].astype(__train_dataset[_col].dtypes ).values.reshape(-1,1)) [:,0]
data_size = len(train_dataset)
 indics = list(range(data_size))<count_duplicates>
_CATEGORICAL_COLS = ['Sex', 'Embarked']
_ohe = OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=-1)
 __train_dataset[_CATEGORICAL_COLS] = pd.DataFrame(_ohe.fit_transform(__train_dataset[_CATEGORICAL_COLS]), columns=_CATEGORICAL_COLS)
 __test_dataset[_CATEGORICAL_COLS] = pd.DataFrame(_ohe.transform(__test_dataset[_CATEGORICAL_COLS]), columns=_CATEGORICAL_COLS )
np.random.shuffle(indics )<define_variables>
_TEXT_COLUMNS = ['Name', 'Ticket', 'Cabin']
def process_text(__dataset):
for _col in _TEXT_COLUMNS:
process_text = [t.lower() for t in __dataset[_col]]
table = str.maketrans('', '', string.punctuation)
 process_text = [t.translate(table)for t in process_text]
process_text = [re.sub(r'\d+', 'num', t)for t in process_text]
__dataset[_col] = process_text
return __dataset
__train_dataset = process_text(__train_dataset)
 __test_dataset = process_text(__test_dataset )
validation_split = 0.1
split = int(np.round(validation_split*data_size))
train_indices,validation_indices = indics[split:],indics[:split]<randomize_order>
__feature_train = __train_dataset.drop(['Survived'], axis=1)
 __target_train =__train_dataset['Survived']
__feature_test = __test_dataset
train_sampler = SubsetRandomSampler(train_indices)
 valid_sampler = SubsetRandomSampler(validation_indices )<load_pretrained>
_TEXT_COLUMNS = ['Cabin', 'Ticket', 'Name']
__temp_train_data = __feature_train[_TEXT_COLUMNS]
__feature_train.drop(_TEXT_COLUMNS, axis=1, inplace=True)
 __feature_train_object_array = []
__temp_test_data = __feature_test[_TEXT_COLUMNS]
__feature_test.drop(_TEXT_COLUMNS, axis=1, inplace=True)
 __feature_test_object_array = []
for _col in _TEXT_COLUMNS:
__tfidfvectorizer = TfidfVectorizer(max_features=3000)
 vector_train = __tfidfvectorizer.fit_transform(__temp_train_data[_col])
 __feature_train_object_array.append(vector_train)
 vector_test = __tfidfvectorizer.transform(__temp_test_data[_col])
 __feature_test_object_array.append(vector_test)
 __feature_train = sparse.hstack([__feature_train] + __feature_train_object_array ).tocsr()
__feature_test = sparse.hstack([__feature_test] + __feature_test_object_array ).tocsr()
train_loader = DataLoader(train_dataset,batch_size=32,sampler=train_sampler)
 valid_loader = DataLoader(train_dataset,batch_size=32,sampler = valid_sampler )<choose_model_class>
__model = RandomForestClassifier()
__model.fit(__feature_train, __target_train)
 __y_pred = __model.predict(__feature_test )
model = resnet50(pretrained=False)
 model.load_state_dict(torch.load('.. /input/resnet50/resnet50.pth'))
for params in model.parameters() :
params.require_grad = False
model.fc = nn.Sequential(
 nn.Linear(2048,1024),
nn.ReLU() ,
nn.Linear(1024,5)
)
 fc_parameter = model.fc.parameters()
for params in fc_parameter:
params.require_grad = True
model = model.to(device )<choose_model_class>
submission = pd.DataFrame(columns=['PassengerId'], data=__test_dataset_submission_columns)
 submission = pd.concat([submission, pd.DataFrame(__y_pred, columns=['Survived'])], axis=1)
 submission.head()
<train_model><EOS> 
submission.to_csv("kaggle_submission.csv", index=False )