Kaggle competition: PadhAI: Text - Non Text Classification Level 4b
<SOS> metric: categorizationaccuracy Kaggle data source: padhai:-text-non-text-classification-level-4b<define_search_space>
warnings.simplefilter("ignore")
 np.random.seed(100)
 LEVEL = 'level_4b'
model_param_grid['DecisionTreeClassifier'] = {'criterion' : ["gini","entropy"],
'max_features': ['auto', 'sqrt', 'log2'],
'min_samples_split': [10,11,12,13,14,15],
'min_samples_leaf':[1,2,3,4,5,6,7]}<define_search_space>
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 = X.mean(axis=0)
 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)
 self.w -= learning_rate * dw
self.b -= learning_rate * db
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)
 flag = loss[i]
elif loss_fn == "ce":
loss[i] = log_loss(Y, Y_pred)
 flag = loss[i]
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()
min_key = min(loss, key=loss.get)
 print(min_key)
 print(loss.get(min_key))
print(i)
 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 )
model_param_grid['RandomForestClassifier'] = {'n_estimators' : [50,100,150,200],
'criterion' : ["gini","entropy"],
'max_features': ['auto', 'sqrt', 'log2'],
'class_weight' : ["balanced", "balanced_subsample"]}<define_search_space>
languages = ['en','ta', 'hi']
images_train = read_all(".. /input/"+LEVEL+"_train/"+LEVEL+"/"+"background", key_prefix='bgr_')
 for language in languages:
images_train.update(read_all(".. /input/"+LEVEL+"_train/"+LEVEL+"/"+language, key_prefix=language+"_"))
print(len(images_train))
images_test = read_all(".. /input/"+LEVEL+"_test/kaggle_"+LEVEL, key_prefix='')
 print(len(images_test))
model_param_grid['AdaBoostClassifier'] = {'n_estimators' : [25,50,75,100],
'learning_rate' : [0.001,0.01,0.05,0.1,1,10],
'algorithm' : ['SAMME', 'SAMME.R']}<train_on_grid>
X_train = []
Y_train = []
ID_train = []
for key, value in images_train.items() :
X_train.append(value)
 ID_train.append(key)
 if key[:4] == "bgr_":
Y_train.append(0)
 else:
Y_train.append(1)
 ID_test = []
X_test = []
for key, value in images_test.items() :
ID_test.append(int(key))
X_test.append(value)
 X_train = np.array(X_train)
 Y_train = np.array(Y_train)
 X_test = np.array(X_test)
 print(X_train.shape, Y_train.shape)
 print(X_test.shape )
def tune_parameters(model_name,model,params,cv,scorer,X,y):
best_model = GridSearchCV(estimator = model,
param_grid = params,
scoring = scorer,
cv = cv,
n_jobs = -1 ).fit(X, y)
 print("Tuning Results for ", model_name)
 print("Best Score Achieved: ",best_model.best_score_)
 print("Best Parameters Used: ",best_model.best_params_)
 return best_model.best_estimator_<compute_test_metric>
scaler = StandardScaler()
X_scaled_train = scaler.fit_transform(X_train)
 X_scaled_test = scaler.transform(X_test )
def roc_metric(y_test, y_pred):
score = roc_auc_score(y_test, y_pred)
 return score<normalization>
sn_ce = SigmoidNeuron()
sn_ce.fit(X_scaled_train, Y_train, epochs=100, learning_rate=1,initialise=True,loss_fn="ce",display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=200, learning_rate=0.1,initialise=False,loss_fn="ce",display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=200, learning_rate=0.01,initialise=False,loss_fn="ce" ,display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=300, learning_rate=0.001,initialise=False,loss_fn="ce",display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=500, learning_rate=0.0001,initialise=False,loss_fn="ce",display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=800, learning_rate=0.00001,initialise=False,loss_fn="ce",display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=1000, learning_rate=0.000005,initialise=False,loss_fn="ce",display_loss=True )
roc_scorer = make_scorer(roc_metric,greater_is_better=True )<define_variables>
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)
 mismatch_name = []
for x,y,z in zip(Y_pred_binarised_train, Y_train, ID_train):
if(x!=y):
mismatch_name.append(z)
 print(mismatch_name )
best_estimators = []<train_on_grid>
print_accuracy(sn_ce )
for m_name, m_obj in estimators:
best_estimators.append(( m_name,tune_parameters(m_name,
m_obj,
model_param_grid[m_name],
10,
roc_scorer,
X_train,
y_train)) )<find_best_params>
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 )