Kaggle competition: PadhAI: Text - Non Text Classification Level 4b
best_estimators<train_model>
class SigmoidNeuron:
def __init__(self):
self.w = None
self.b = None
self.best_epoch = 0
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
loss = {}
best_w = self.w.copy()
best_b = 0
best_loss = 999
best_epoch = 1
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
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 best_loss == 999:
best_loss = loss[i]
best_w = self.w.copy()
best_b = self.b
best_epoch = i
elif loss[i] < best_loss:
best_loss = loss[i]
best_w = self.w.copy()
best_b = self.b
best_epoch = i
self.w = best_w.copy()
self.b = best_b
self.best_epoch = best_epoch
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 )
tuned_vc = VotingClassifier(best_estimators)
 tuned_vc.fit(X_train,y_train )<predict_on_test>
a = np.array([1,2,3])
 c = list(a)
 print(a)
 print(c)
 b = [(lambda x: 0 if x < 2 else 255 )(x)for x in c]
print(b )
y_pred = tuned_vc.predict(X_test )<compute_test_metric>
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))
confusion_matrix(y_test,y_pred )<compute_test_metric>
a = np.array(( [1,2],[3,4]), dtype = int)
 print(a)
 b = a.flatten()
print(b)
 c = b.reshape(2,-1)
 print(c)
accuracy_score(y_test,y_pred )<compute_test_metric>
scaler = StandardScaler()
X_scaled_train = scaler.fit_transform(X_train)
 X_scaled_test = scaler.transform(X_test)
precision_score(y_test,y_pred )<compute_test_metric>
sn_mse = SigmoidNeuron()
sn_mse.fit(X_scaled_train, Y_train, epochs=100, learning_rate=0.015, loss_fn="mse", display_loss=True )
recall_score(y_test,y_pred )<compute_test_metric>
sn_ce = SigmoidNeuron()
sn_ce.fit(X_scaled_train, Y_train, epochs=10000, learning_rate=0.000005, loss_fn="ce", display_loss=True )
f1_score(y_test,y_pred )<import_modules>
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 )
import keras
from keras.utils import plot_model
from keras.models import Model,Sequential,load_model
from keras.layers import Input, Flatten, Dense, Dropout
from keras.layers.merge import concatenate
from keras import backend as K
from keras.callbacks import ModelCheckpoint,EarlyStopping,ReduceLROnPlateau<choose_model_class>
print_accuracy(sn_ce )
def nn_model(X,y,optimizer,kernels):
input_shape = X.shape[1]
if(len(np.unique(y)) == 2):
op_neurons = 1
op_activation = 'sigmoid'
loss = 'binary_crossentropy'
else:
op_neurons = len(np.unique(y))
op_activation = 'softmax'
loss = 'categorical_crossentropy'
classifier = Sequential()
classifier.add(Dense(units = input_shape,
kernel_initializer = kernels,
activation = 'relu',
input_dim = input_shape))
classifier.add(Dense(units = 8,
kernel_initializer = kernels,
activation = 'relu'))
classifier.add(Dense(units = 4,
kernel_initializer = kernels,
activation = 'relu'))
classifier.add(Dropout(rate = 0.25))
classifier.add(Dense(units = op_neurons,
kernel_initializer = kernels,
activation = op_activation))
classifier.compile(optimizer = optimizer,
loss = loss,
metrics = ['accuracy'])
 classifier.summary()
return classifier<train_model>
<create_dataframe><EOS> 
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 )