Kaggle competition: Titanic - Machine Learning from Disaster
import pandas as pd
import numpy as np
import torch
from torch import nn
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA<set_options>
train = pd.read_csv(".. /input/titanic/train.csv")
 train.head()
torch.cuda.is_available()<load_from_csv>
test = pd.read_csv(".. /input/titanic/test.csv")
 test.head()
train_data = pd.read_csv('/kaggle/input/sf-crime/train.csv.zip', parse_dates=['Dates'])
 test_data = pd.read_csv('/kaggle/input/sf-crime/test.csv.zip', parse_dates=['Dates'] )<feature_engineering>
all = pd.concat([train, test], sort = False)
 all.info()
all_features = pd.concat(( train_data.iloc[:, [0, 3, 4, 6, 7, 8]],
test_data.iloc[:, [1, 2, 3, 4, 5, 6]]),
sort=False)
 num_train = train_data.shape[0]
train_labels = pd.get_dummies(train_data['Category'] ).values
num_outputs = train_labels.shape[1]
all_features['year'] = all_features.Dates.dt.year
all_features['month'] = all_features.Dates.dt.month
all_features['new_year'] = all_features['month'].apply(
 lambda x: 1 if x == 1 or x == 2 else 0)
 all_features['day'] = all_features.Dates.dt.day
all_features['hour'] = all_features.Dates.dt.hour
all_features['evening'] = all_features['hour'].apply(lambda x: 1
if x >= 18 else 0)
 wkm = {
'Monday': 0,
'Tuesday': 1,
'Wednesday': 2,
'Thursday': 3,
'Friday': 4,
'Saturday': 5,
'Sunday': 6
}
all_features['DayOfWeek'] = all_features['DayOfWeek'].apply(lambda x: wkm[x])
 all_features['weekend'] = all_features['DayOfWeek'].apply(
 lambda x: 1 if x == 4 or x == 5 else 0)
 OneHot_features = pd.get_dummies(all_features['PdDistrict'])
 all_features['block'] = all_features['Address'].apply(
 lambda x: 1 if 'block' in x.lower() else 0)
 PCA_features = all_features[['X', 'Y']].values
Standard_features = all_features[['DayOfWeek', 'year', 'month', 'day',
'hour']].values
OneHot_features = pd.concat([
OneHot_features, all_features[['new_year', 'evening', 'weekend', 'block']]
],
axis=1 ).values
scaler = StandardScaler()
scaler.fit(Standard_features)
 Standard_features = scaler.transform(Standard_features)
 pca = PCA(n_components=2)
 pca.fit(PCA_features)
 PCA_features = pca.transform(PCA_features)
 all_features = np.concatenate(
(PCA_features, Standard_features, OneHot_features), axis=1)
 train_features = all_features[:num_train]
num_inputs = train_features.shape[1]
test_features = all_features[num_train:]<normalization>
all['Age'] = all['Age'].fillna(value=all['Age'].median())
 all['Fare'] = all['Fare'].fillna(value=all['Fare'].median() )
class Residual(nn.Module):
def __init__(self, num_inputs, num_outputs):
super(Residual, self ).__init__()
self.middle_L = nn.Linear(num_inputs, num_outputs)
 self.middle_R = nn.ReLU(num_outputs)
 if num_inputs != num_outputs:
self.right = nn.Linear(num_inputs, num_outputs)
 else:
self.right = None
self.middle_B = nn.BatchNorm1d(num_outputs)
 def forward(self, X):
Y = self.middle_B(self.middle_R(self.middle_L(X)))
 if self.right:
X = self.right(X)
 return Y + X<choose_model_class>
all.loc[ all['Age'] <= 16, 'Age'] = 0
all.loc[(all['Age'] > 16)&(all['Age'] <= 32), 'Age'] = 1
all.loc[(all['Age'] > 32)&(all['Age'] <= 48), 'Age'] = 2
all.loc[(all['Age'] > 48)&(all['Age'] <= 64), 'Age'] = 3
all.loc[ all['Age'] > 64, 'Age'] = 4
class build_model(nn.Module):
def __init__(self, num_inputs, num_outputs, dp=0.5):
super(build_model, self ).__init__()
self.net = nn.Sequential()
self.net.add_module('Residual1', Residual(num_inputs, 1024))
self.net.add_module('Residual2', Residual(1024, 512))
self.net.add_module('Residual3', Residual(512, 512))
self.net.add_module('Residual4', Residual(512, 256))
self.net.add_module('Dropout1', nn.Dropout(dp))
self.net.add_module('Residual5', Residual(256, 256))
self.net.add_module('Residual6', Residual(256, 128))
self.net.add_module('Residual7', Residual(128, 128))
self.net.add_module('Residual8', Residual(128, 64))
self.net.add_module('Dropout2', nn.Dropout(dp))
self.net.add_module('Residual9', Residual(64, 64))
self.net.add_module('Linear-out', nn.Linear(64, num_outputs))
self.net.add_module('Softmax', nn.Softmax(dim=-1))
def forward(self, x):
return self.net(x)
 net = build_model(num_inputs, num_outputs ).cuda()<compute_train_metric>
all['Title'] = all['Name'].apply(get_title)
 all['Title'].value_counts()
class MultiClassLogLoss(torch.nn.Module):
def __init__(self):
super(MultiClassLogLoss, self ).__init__()
def forward(self, y_pred, y_true):
return -(y_true *
torch.log(y_pred.float() + 1.00000000e-15)) / y_true.shape[0]
loss = MultiClassLogLoss().cuda()<find_best_params>
all['Title'] = all['Title'].replace(['Capt.', 'Dr.', 'Major.', 'Rev.'], 'Officer.')
 all['Title'] = all['Title'].replace(['Lady.', 'Countess.', 'Don.', 'Sir.', 'Jonkheer.', 'Dona.'], 'Royal.')
 all['Title'] = all['Title'].replace(['Mlle.', 'Ms.'], 'Miss.')
 all['Title'] = all['Title'].replace(['Mme.'], 'Mrs.')
 all['Title'].value_counts()
def make_iter(train_features, train_labels, batch_size):
train_features = torch.tensor(train_features, dtype=torch.float ).cuda()
train_labels = torch.tensor(train_labels ).cuda()
dataset = torch.utils.data.TensorDataset(train_features, train_labels)
 return torch.utils.data.DataLoader(dataset, batch_size, shuffle=True)
 def show_loss(net, loss, features, labels, team):
net.eval()
batch = make_iter(features, labels, 1024)
 loss_num = 0
n = 0
for x, y in batch:
loss_num += loss(net(x), y ).sum().item()
n += 1
print(team, end=' ')
 print('loss:', loss_num / n)
 def train(features, labels, batch_size):
net.train()
train_iter = make_iter(features, labels, batch_size)
 for X, y in train_iter:
y_hat = net(X)
 l = loss(y_hat, y ).sum()
optimizer.zero_grad()
l.backward()
optimizer.step()
show_loss(net, loss, features, labels, '训练集' )<init_hyperparams>
all['Cabin'] = all['Cabin'].fillna('Missing')
 all['Cabin'] = all['Cabin'].str[0]
all['Cabin'].value_counts()
num_epochs = 100
k_fold_num = 5
batch_size = 128
lr = 0.001
k_fold = False<choose_model_class>
all['Family_Size'] = all['SibSp'] + all['Parch'] + 1
all['IsAlone'] = 0
all.loc[all['Family_Size']==1, 'IsAlone'] = 1
all.head()
optimizer = torch.optim.Adam(net.parameters() , lr=lr )<split>
all_1 = all.drop(['Name', 'Ticket'], axis = 1)
 all_1.head()
if k_fold:
kf = KFold(n_splits=k_fold_num, shuffle=True)
 for epoch in range(num_epochs):
fold_num = 0
for train_index, test_index in kf.split(train_features):
X_train, X_test = train_features[train_index], train_features[
test_index]
y_train, y_test = train_labels[train_index], train_labels[
test_index]
print('第%d轮的第%d折：' %(epoch + 1, fold_num + 1))
fold_num += 1
train(X_train, y_train, batch_size)
 show_loss(net, loss, X_test, y_test, '测试集')
 else:
for epoch in range(num_epochs):
print('第%d轮：' %(epoch + 1))
train(train_features, train_labels, batch_size)
 net.eval()
test_iter = torch.utils.data.DataLoader(torch.tensor(test_features,
dtype=torch.float ).cuda() ,
1024,
shuffle=False)
 testResult = [line for x in test_iter for line in net(x ).cpu().detach().numpy() ]
sampleSubmission = pd.read_csv('/kaggle/input/sf-crime/sampleSubmission.csv.zip')
 Result_pd = pd.DataFrame(testResult,
index=sampleSubmission.index,
columns=sampleSubmission.columns[1:])
 Result_pd.to_csv('/kaggle/working/sampleSubmission(v0.5 ).csv', index_label='Id')
 torch.save(net,'/kaggle/working/net.pkl' )<import_modules>
all_dummies = pd.get_dummies(all_1)
 all_dummies.info()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
all_test = all_dummies[all_dummies['Survived'].isna() ]
all_test.info()
train_data = pd.read_csv("/kaggle/input/sf-crime/train.csv")
 test_data = pd.read_csv("/kaggle/input/sf-crime/test.csv" )<count_missing_values>
X_train, X_test, y_train, y_test = train_test_split(all_train.drop(['PassengerId','Survived'],axis=1),
all_train['Survived'], test_size=0.30,
random_state=101 )
print(train_data.isnull().sum())
 print(test_data.isnull().sum() )<drop_column>
from sklearn.ensemble import RandomForestClassifier
train_data = train_data.drop(["Descript", "Resolution"], axis = 1 )<feature_engineering>
RF_Model = RandomForestClassifier()
def transformDataset(dataset):
dataset['Dates'] = pd.to_datetime(dataset['Dates'])
 dataset['Date'] = dataset['Dates'].dt.date
dataset['n_days'] =(dataset['Date'] - dataset['Date'].min() ).apply(lambda x: x.days)
 dataset['Year'] = dataset['Dates'].dt.year
dataset['DayOfWeek'] = dataset['Dates'].dt.dayofweek
dataset['WeekOfYear'] = dataset['Dates'].dt.weekofyear
dataset['Month'] = dataset['Dates'].dt.month
dataset['Hour'] = dataset['Dates'].dt.hour
dataset['Block'] = dataset['Address'].str.contains('block', case=False)
 dataset['Block'] = dataset['Block'].map(lambda x: 1 if x == True else 0)
 dataset = dataset.drop('Dates', 1)
 dataset = dataset.drop('Date', 1)
 dataset = dataset.drop('Address', 1)
 dataset = pd.get_dummies(data=dataset, columns=[ 'PdDistrict'], drop_first = True)
 return dataset 
<create_dataframe>
from sklearn.model_selection import GridSearchCV
train_data = transformDataset(train_data )<create_dataframe>
from sklearn.model_selection import GridSearchCV
test_data = transformDataset(test_data )<categorify>
parameters = {'n_estimators' :(10,30,50,70,90,100)
 , 'criterion' :('gini', 'entropy')
 , 'max_depth' :(3,5,7,9,10)
 , 'max_features' :('auto', 'sqrt')
 , 'min_samples_split' :(2,4,6)
 }
le = LabelEncoder()
train_data["Category"] = le.fit_transform(train_data["Category"])
<prepare_x_and_y>
RF_grid = GridSearchCV(RandomForestClassifier(n_jobs = -1, oob_score= False), param_grid = parameters, cv = 3, verbose = True )
X = train_data.drop("Category",axis=1 ).values
y = train_data["Category"].values<split>
RF_grid_model = RF_grid.fit(X_train, y_train )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10 )<train_model>
RF_grid_model.best_estimator_
dtree = DecisionTreeClassifier()
dtree.fit(X_train,y_train )<predict_on_test>
RF_Model = RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
criterion='gini', max_depth=7, max_features='sqrt',
max_leaf_nodes=None, max_samples=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=6,
min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=-1,
oob_score=False, random_state=None, verbose=0,
warm_start=False )
predictions = dtree.predict(X_test)
<compute_test_metric>
RF_Model.fit(X_train, y_train )
print(classification_report(y_test,predictions))<train_model>
predictions = RF_Model.predict(X_test)
 predictions
rfc = RandomForestClassifier(n_estimators=40,min_samples_split=100)
 rfc.fit(X_train, y_train )<predict_on_test>
print(f'Test : {RF_Model.score(X_test, y_test):.3f}')
 print(f'Train : {RF_Model.score(X_train, y_train):.3f}' )
rfc_pred = rfc.predict(X_test)
 print(classification_report(y_test,rfc_pred))<categorify>
TestForPred = all_test.drop(['PassengerId', 'Survived'], axis = 1 )
keys = le.classes_
values = le.transform(le.classes_)
 keys<define_variables>
t_pred = RF_Model.predict(TestForPred ).astype(int )
dictionary = dict(zip(keys, values))
print(dictionary )<drop_column>
PassengerId = all_test['PassengerId']
test_data = test_data.drop('Id', 1 )<predict_on_test>
RF_Sub = pd.DataFrame({'PassengerId': PassengerId, 'Survived':t_pred })
 RF_Sub.head()
y_pred_proba = rfc.predict_proba(test_data)
 y_pred_proba<prepare_output>
RF_Sub.to_csv("RF_Class_Submission.csv", index = False )