Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import confusion_matrix
class SigmoidNeuron:
def __init__(self):
self.w = None
self.b = None
def perceptron(self, x):
return np.dot(x, self.w.T)+ self.b
def sigmoid(self, x):
return 1.0/(1.0 + np.exp(-x))
def grad_w_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)* x
def grad_b_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)
 def grad_w_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred * x
elif y == 1:
return -1 *(1 - y_pred)* x
else:
raise ValueError("y should be 0 or 1")
 def grad_b_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred
elif y == 1:
return -1 *(1 - y_pred)
 else:
raise ValueError("y should be 0 or 1")
 def fit_train_valid(self, X_train, Y_train,X_test,Y_test,epochs=1, learning_rate=1, initialise=True, loss_fn="mse", display_loss=False,lr_decay=False):
i = 0
if initialise:
np.random.seed(100)
 self.w = np.random.randn(1, X_train.shape[1])
 np.random.seed(100)
 self.b = np.random.randn()
if display_loss:
loss = {}
valid_loss = {}
while(i<epochs):
dw = 0
db = 0
for x, y in zip(X_train, Y_train):
if loss_fn == "mse":
dw += self.grad_w_mse(x, y)
 db += self.grad_b_mse(x, y)
 elif loss_fn == "ce":
dw += self.grad_w_ce(x, y)
 db += self.grad_b_ce(x, y)
 self.w -= learning_rate * dw
self.b -= learning_rate * db
if display_loss:
Y_pred = self.sigmoid(self.perceptron(X_train))
Y_test_pred = self.sigmoid(self.perceptron(X_test))
if loss_fn == "mse":
loss[i] = mean_squared_error(Y_train, Y_pred)
 valid_loss[i] = mean_squared_error(Y_test, Y_test_pred)
 elif loss_fn == "ce":
loss[i] = log_loss(Y_train, Y_pred)
 valid_loss[i] = log_loss(Y_test, Y_test_pred)
 if lr_decay:
if i>0 and valid_loss[i]-valid_loss[i-1]<0:
w = self.w
b = self.b
i+=1
elif i>0:
i = i-1
self.w = w
self.b = b
learning_rate/=2
else:
i+=1
else:
i+=1
if display_loss:
plt.plot(loss.values())
 plt.plot(valid_loss.values() ,color='orange')
 plt.xlabel('Epochs')
 if loss_fn == "mse":
plt.ylabel('Mean Squared Error')
 elif loss_fn == "ce":
plt.ylabel('Log Loss')
 plt.show()
def fit(self, X, Y, epochs=1, learning_rate=1, initialise=True, loss_fn="mse", display_loss=False):
if initialise:
np.random.seed(100)
 self.w = np.random.randn(1, X.shape[1])
 np.random.seed(100)
 self.b = np.random.randn()
if display_loss:
loss = {}
for i in range(epochs):
dw = 0
db = 0
for x, y in zip(X, Y):
if loss_fn == "mse":
dw += self.grad_w_mse(x, y)
 db += self.grad_b_mse(x, y)
 elif loss_fn == "ce":
dw += self.grad_w_ce(x, y)
 db += self.grad_b_ce(x, y)
 self.w -= learning_rate * dw
self.b -= learning_rate * db
if display_loss:
Y_pred = self.sigmoid(self.perceptron(X))
if loss_fn == "mse":
loss[i] = mean_squared_error(Y, Y_pred)
 elif loss_fn == "ce":
loss[i] = log_loss(Y, Y_pred)
 if display_loss:
plt.plot(loss.values())
 plt.xlabel('Epochs')
 if loss_fn == "mse":
plt.ylabel('Mean Squared Error')
 elif loss_fn == "ce":
plt.ylabel('Log Loss')
 plt.show()
def predict(self, X):
Y_pred = []
for x in X:
y_pred =1 if self.sigmoid(self.perceptron(x)) >=0.5 else 0
Y_pred.append(y_pred)
 return np.array(Y_pred )<train_model>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data.head()
class Perceptron:
def __init__(self):
self.w = None
self.b = None
def model(self, x):
return 1 if(np.dot(self.w, x)>= self.b)else 0
def predict(self, X):
Y = []
for x in X:
result = self.model(x)
 Y.append(result)
 return np.array(Y)
 def fit(self, X, Y, epochs = 1, lr = 1,initialize=True,display_loss=False):
if initialize:
np.random.seed(100)
 self.w = np.random.randn(1,X.shape[1])
 np.random.seed(100)
 self.b = np.random.randn()
accuracy = {}
max_accuracy = 0
wt_matrix = []
for i in range(epochs):
for x, y in zip(X, Y):
y_pred = self.model(x)
 if y == 1 and y_pred == 0:
self.w = self.w + lr * x
self.b = self.b - lr * 1
elif y == 0 and y_pred == 1:
self.w = self.w - lr * x
self.b = self.b + lr * 1
if display_loss:
accuracy[i] = accuracy_score(self.predict(X), Y)
 if(accuracy[i] > max_accuracy):
max_accuracy = accuracy[i]
if display_loss:
print(max_accuracy)
 plt.title("Accuracy")
 plt.plot(accuracy.values())
 plt.xlabel("Epochs")
 plt.ylabel("Accuracy")
 plt.ylim([0, 1])
 plt.show()<define_variables>
test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data.head()
sys.path.insert(0,'.. /input/level_4b_train/' )<load_pretrained>
submission = pd.read_csv('.. /input/titanic/gender_submission.csv' )
languages = ['ta', 'hi', 'en']
images_train = read_all('.. /input/level_4b_train/'+LEVEL+"/"+"background", key_prefix='bgr_')
 for language in languages:
images_train.update(read_all('.. /input/level_4b_train/'+LEVEL+"/"+language, key_prefix=language+"_"))
print(len(images_train))
sys.path.insert(0,'.. /input/level_4b_test/')
 images_test = read_all('.. /input/level_4b_test/kaggle_'+LEVEL, key_prefix='')
 print(len(images_test))<normalization>
def highlight(value):
if value >= 0.75:
style = 'background-color: green'
elif value >= 0.5:
style = 'background-color: palegreen'
elif value >= 0.25:
style = 'background-color: pink'
else:
style = 'background-color: red'
return style
def pre_process_image(img):
img = img.reshape(64,64)
 ret,th1 = cv2.threshold(img,25,255,cv2.THRESH_BINARY)
 th1 = th1.ravel()
return th1<train_model>
women = train_data.loc[train_data.Sex == 'female']['Survived']
rate_women = sum(women)/ len(women)
 print('percent of women who survived :', rate_women )
X_train_processed = []
for i in range(X_train.shape[0]):
X_train_processed.append(pre_process_image(X_train[i]))
X_train = np.array(X_train_processed )<categorify>
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("percent of men who survived :", rate_men )
X_test_processed = []
for i in range(X_test.shape[0]):
X_test_processed.append(pre_process_image(X_test[i]))
X_test = np.array(X_test_processed )<split>
train_data['Family_size'] = train_data['SibSp'] + train_data['Parch'] + 1
X_sub_train, X_sub_test, y_sub_train, y_sub_test = train_test_split(X_train, Y_train, test_size=0.1, random_state=34,stratify=Y_train )<normalization>
missing_age_value = train_data['Age'].isnull().sum()
percent_of_missing_age_value = missing_age_value / 100
print('% of missing age value =', percent_of_missing_age_value )
sc = StandardScaler()
X_sc_sub_train = sc.fit_transform(X_sub_train)
 X_sc_sub_test = sc.transform(X_sub_test )<normalization>
train_data['Age_5'] = train_data['Age'] // 5
train_data['Age_5'] = train_data['Age_5'].fillna(mean_age // 5 ).astype('int' )
min_max = MinMaxScaler()
X_min_max_sub_train = min_max.fit_transform(X_sub_train)
 X_min_max_test = min_max.transform(X_sub_test )<statistical_test>
number_of_unique_age_values = len(train_data['Age_5'].unique())
 print(number_of_unique_age_values )
sn_mse = SigmoidNeuron()<train_model>
def df_transform(df):
df['Family_size'] = df['SibSp'] + df['Parch'] + 1
df['Age_5'] = df['Age'] // 5
mean_age = df['Age'].mean()
df['Age_5'] = df['Age_5'].fillna(mean_age // 5 ).astype('int')
 df['Sex'] = df['Sex'].replace({'male': 0, 'female': 1})
 df = df[['Family_size', 'Age_5', 'Sex', 'Pclass']]
return df
for learning_rate in [0.1,0.01,0.001,0.0001]:
sn_mse.fit_train_valid(X_sc_sub_train,y_sub_train,X_sc_sub_test,y_sub_test,display_loss=True,epochs=100,learning_rate=learning_rate,loss_fn="mse" )<train_model>
target = train_data.pop('Survived' )
for learning_rate in [0.0003,0.0006,0.0007,0.0008]:
sn_mse.fit_train_valid(X_sc_sub_train,y_sub_train,X_sc_sub_test,y_sub_test,display_loss=True,epochs=100,learning_rate=learning_rate,loss_fn="mse" )<train_model>
train_data = df_transform(train_data)
 train_data.head()
sn_mse.fit_train_valid(X_sc_sub_train,y_sub_train,X_sc_sub_test,y_sub_test,display_loss=True,epochs=4500,learning_rate=0.0008,loss_fn="mse" )<predict_on_test>
test_data = df_transform(test_data)
 test_data.head()
accuracy_score(sn_mse.predict(X_sc_sub_train),y_sub_train )<predict_on_test>
model = DecisionTreeClassifier(max_depth=5, random_state=42)
 model.fit(train_data, target )
accuracy_score(sn_mse.predict(X_sc_sub_test),y_sub_test )<split>
y_train = model.predict(train_data ).astype(int )
X_sub_train, X_sub_test, y_sub_train, y_sub_test = train_test_split(X_train, Y_train, test_size=0.1, random_state=10,stratify=Y_train )<compute_test_metric>
confusion_matrix(target, y_train )
print(np.mean(y_sub_train),np.mean(y_sub_test))<normalization>
y_pred = model.predict(test_data ).astype(int )
scaler = StandardScaler()
X_scaled_train = scaler.fit_transform(X_sub_train)
 X_scaled_test = scaler.transform(X_sub_test )<train_model>
submission["Survived"] = y_pred
submission.to_csv('submission.csv', index=False)
 submission['Survived'].hist()