Kaggle competition: Titanic - Machine Learning from Disaster
sn_mse = sn_mse = SigmoidNeuron()
sn_mse.fit(X_scaled_train,y_sub_train,display_loss=True,epochs=4500,learning_rate=0.0008,loss_fn="mse" )<predict_on_test>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 train.head()
def print_accuracy(sn):
Y_pred_train = sn.predict(X_scaled_train)
 Y_pred_binarised_train =(Y_pred_train >= 0.5 ).astype("int" ).ravel()
accuracy_train = accuracy_score(Y_pred_binarised_train, y_sub_train)
 print("Train Accuracy : ", accuracy_train)
 print("-"*50 )<compute_test_metric>
test = pd.read_csv('/kaggle/input/titanic/test.csv')
 test.head()
print_accuracy(sn_mse )<predict_on_test>
Id = test["PassengerId"]
all_data = pd.concat([train,test],ignore_index = True )
print(accuracy_score(sn_mse.predict(X_scaled_test),y_sub_test))<normalization>
all_data['Name']
all_data["Title"] = all_data["Name"].apply(lambda x:x.split(",")[1].split(".")[0].strip())
 Title_Dict = {}
Title_Dict.update(dict.fromkeys(['Capt', 'Col', 'Major', 'Dr', 'Rev'],"Officer"))
Title_Dict.update(dict.fromkeys(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty'))
Title_Dict.update(dict.fromkeys(['Mme', 'Ms', 'Mrs'], 'Mrs'))
Title_Dict.update(dict.fromkeys(['Mlle', 'Miss'], 'Miss'))
Title_Dict.update(dict.fromkeys(['Mr'], 'Mr'))
Title_Dict.update(dict.fromkeys(['Master','Jonkheer'], 'Master'))
all_data["Title"] = all_data["Title"].map(Title_Dict )
mainScaler = StandardScaler()
X_train_scaled = mainScaler.fit_transform(X_train)
 X_test_scaled = mainScaler.transform(X_test )<train_model>
all_data['FamilySize']=all_data['SibSp']+all_data['Parch']+1
def SizeToLabel(s):
if(s>=2)&(s<=4):
return 2
elif(( s>=5)&(s<=7)) |(s == 1):
return 1
elif(s>7):
return 0
all_data["FamilyLabel"] = all_data["FamilySize"].apply(SizeToLabel )
sn_mse = SigmoidNeuron()
sn_mse.fit(X_train_scaled,Y_train,display_loss=True,epochs=4500,learning_rate=0.0008,loss_fn="mse" )<compute_test_metric>
all_data["Cabin"] = all_data["Cabin"].fillna("Unknown")
 all_data["Deck"] = all_data["Cabin"].str.get(0 )
print(accuracy_score(sn_mse.predict(X_train_scaled),Y_train))<save_to_csv>
Tick_Counts = dict(all_data['Ticket'].value_counts())
 all_data["TicketGroup"] = all_data["Ticket"].apply(lambda x:Tick_Counts[x] )
Y_pred_test = sn_mse.predict(X_test_scaled)
 Y_pred_binarised_test =(Y_pred_test >= 0.5 ).astype("int" ).ravel()
submission = {}
submission['ImageId'] = ID_test
submission['Class'] = Y_pred_binarised_test
submission = pd.DataFrame(submission)
 submission = submission[['ImageId', 'Class']]
submission = submission.sort_values(['ImageId'])
 submission.to_csv("submisision.csv", index=False )<define_variables>
def GroupToLabel(s):
if(s>=2)&(s<=4):
return 2
elif(( s>=5)&(s<=8)) |(s==1):
return 1
elif(s>8):
return 0
all_data["TicketLabel"] = all_data["TicketGroup"].apply(GroupToLabel )
np.random.seed(100)
 LEVEL = 'level_4a'<compute_test_metric>
def sex(s):
if s=="male":
return 1
else:
return 0
all_data["Sex"] = all_data["Sex"].apply(sex )
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(self, X, Y, epochs=1, learning_rate=1, initialise=True, loss_fn="mse", display_loss=False):
if initialise:
self.w = np.random.randn(1, X.shape[1])
 self.b = 0
if display_loss:
loss = {}
for i in tqdm_notebook(range(epochs), total=epochs, unit="epoch"):
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 = self.sigmoid(self.perceptron(x))
Y_pred.append(y_pred)
 return np.array(Y_pred )<load_pretrained>
def title(s):
if s=="Officer":
return 0
elif s=="Mr":
return 1
elif s=="Mrs":
return 2
elif s=="Miss":
return 3
elif s=="Master":
return 4
elif s=="Royalty":
return 5
all_data["Title"] = all_data["Title"].apply(title )
languages = ['ta', 'hi', 'en']
images_train = read_all(".. /input/level_4b_train/level_4b/background/", key_prefix='bgr_')
 for language in languages:
images_train.update(read_all(".. /input/level_4b_train/level_4b/"+language, key_prefix=language+"_"))
print(len(images_train))
images_test = read_all(".. /input/level_4b_test/kaggle_level_4b", key_prefix='')
 print(len(images_test))<normalization>
def embarked(s):
if s=="S":
return 0
elif s=="C":
return 1
elif s=="Q":
return 2
all_data["Embarked"] = all_data["Embarked"].apply(embarked )
scaler = MinMaxScaler()
X_scaled_train = scaler.fit_transform(X_train)
 X_scaled_test = scaler.transform(X_test )<train_model>
Y_missing = all_data.loc[:,"Age"]
nameList = ["Sex","Pclass","SibSp","Parch","Title"]
X_missing = all_data.loc[:,nameList]
Ytrain = Y_missing[Y_missing.notnull() ]
Ytest = Y_missing[Y_missing.isnull() ]
Xtrain = X_missing.iloc[Ytrain.index,:]
Xtest = X_missing.iloc[Ytest.index,:]
sn_ce = SigmoidNeuron()
sn_ce.fit(X_scaled_train, Y_train, epochs=100, learning_rate=0.015, loss_fn="ce", display_loss=True )<predict_on_test>
rfc = RandomForestRegressor(n_estimators=100)
 rfc = rfc.fit(Xtrain, Ytrain)
 Ypredict = rfc.predict(Xtest)
 all_data.loc[all_data.loc[:,"Age"].isnull() ,"Age"] = Ypredict
all_data["Age"] = all_data["Age"].apply(lambda x:round(x))
nameList = ["Sex","Pclass","Fare","Title","Age","FamilyLabel","TicketLabel","Embarked"]
df_data = all_data.loc[:,nameList]
df_data = SimpleImputer(missing_values=np.nan,strategy='constant',fill_value=0 ).fit_transform(df_data)
 df_data = pd.DataFrame(df_data)
 df_data.info()
def print_accuracy(sn):
Y_pred_train = sn.predict(X_scaled_train)
 Y_pred_binarised_train =(Y_pred_train >= 0.5 ).astype("int" ).ravel()
accuracy_train = accuracy_score(Y_pred_binarised_train, Y_train)
 print("Train Accuracy : ", accuracy_train)
 print("-"*50 )<compute_test_metric>
Y = all_data.loc[:,"Survived"]
Y_predict = Y[Y.isnull() ]
Y_train = Y[Y.notnull() ]
X_train = df_data.iloc[Y_train.index,:]
X_predict = df_data.iloc[Y_predict.index,:]
print_accuracy(sn_ce )<save_to_csv>
rfc = RandomForestClassifier(n_estimators=100,random_state=90)
 score_pre = cross_val_score(rfc,X_train,Y_train,cv=10 ).mean()
score_pre
Y_pred_test = sn_ce.predict(X_scaled_test)
 Y_pred_binarised_test =(Y_pred_test >= 0.5 ).astype("int" ).ravel()
submission = {}
submission['ImageId'] = ID_test
submission['Class'] = Y_pred_binarised_test
submission = pd.DataFrame(submission)
 submission = submission[['ImageId', 'Class']]
submission = submission.sort_values(['ImageId'])
 submission.to_csv("submisision.csv", index=False )<load_from_csv>
score_list=[]
for i in range(0,200,10):
rfc = RandomForestClassifier(n_estimators=i+1,random_state=90,n_jobs=-1)
 score = cross_val_score(rfc,X_train,Y_train,cv=10 ).mean()
score_list.append(score)
 print(max(score_list),(score_list.index(max(score_list)) *10)+1 )
assoc_df = pd.read_csv('.. /input/market-basket-id-ndsc-2020/association_order.csv')
 rules_df = pd.read_csv('.. /input/market-basket-id-ndsc-2020/rules.csv' )<merge>
score_list=[]
for i in range(25,40):
rfc = RandomForestClassifier(n_estimators=i, random_state=90, n_jobs=-1)
 score = cross_val_score(rfc,X_train,Y_train,cv=10 ).mean()
score_list.append(score)
 print(max(score_list),([*range(25,40)][score_list.index(max(score_list)) ]))
def get_assoc(*args):
orderid = [int(id)for id in args]
assoc_conds = [assoc_df.loc[(assoc_df['itemid'] == cond)]['orderid'] for cond in orderid]
assoc_conds = [set(assoc_cond.values.tolist())for assoc_cond in assoc_conds]
intersection_val = assoc_conds[0].intersection(*assoc_conds[1:])
 return len(intersection_val)
<string_transform>
param_grid = {"max_depth":np.arange(1,20,1)}
rfc = RandomForestClassifier(n_estimators=30, random_state=90)
 GS = GridSearchCV(rfc,param_grid,cv=10)
 GS.fit(X_train,Y_train)
 GS.best_params_
def split_rule(x):
return x.split(">")
 def calculate_rule(rules):

 condA, condB = split_rule(rules)
 condB += "&" + condA
condA = condA.split("&")
 condB = condB.split("&")
 totalA = get_assoc(*condA)
 totalB = get_assoc(*condB)
 return totalB / totalA<feature_engineering>
GS.best_score_
rules_df['confidence'] = [floor(confidence * 1000)for confidence in confidences]<define_variables>
param_grid={'min_samples_leaf':np.arange(1,1+10,1)}
rfc = RandomForestClassifier(
 n_estimators=30,
random_state=90,
max_depth=8 
)
 GS = GridSearchCV(rfc,param_grid,cv=10)
 GS.fit(X_train,Y_train)
 GS.best_params_
submission = rules_df[['rule', 'confidence']]
submission<save_to_csv>
GS.best_score_
submission.to_csv("submission.csv", index= False )<import_modules>
param_grid={'min_samples_split':np.arange(2, 2+20, 1)}
rfc = RandomForestClassifier(
 n_estimators=30,
random_state=90,
max_depth=8,
min_samples_leaf=2 
)
 GS = GridSearchCV(rfc,param_grid,cv=10)
 GS.fit(X_train,Y_train)
 GS.best_params_
from fastai.vision import *
from fastai.metrics import accuracy, fbeta<define_variables>
GS.best_score_
path = Path(".. /input" )<categorify>
rfc = RandomForestClassifier(
 n_estimators=30,
random_state=90,
max_depth=8,
min_samples_leaf=2,
min_samples_split=11 
)
 score = cross_val_score(rfc,X_train,Y_train,cv=10 ).mean()
score
src =(ImageList.from_folder(path/'train')
.split_by_rand_pct() 
.label_from_folder() 
.add_test(Path('.. /input/test/test' ).ls()))<normalization>
rfc.fit(X_train, Y_train)
 predictions = rfc.predict(X_predict ).astype(int )
data =(src.transform(get_transforms() , size=128)
.databunch(path=Path(".. /")) 
.normalize(imagenet_stats))<define_variables>
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('submission2.csv', index=False)
 print("Your submission was successfully saved!" )