Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<import_modules>
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score,roc_curve, auc
from sklearn.ensemble import RandomForestRegressor
from lightgbm import LGBMClassifier<load_from_csv>
df_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 df_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 df_submission = pd.read_csv("/kaggle/input/digit-recognizer/sample_submission.csv" )
train=pd.read_csv('/kaggle/input/GiveMeSomeCredit/cs-training.csv')
 train.info() 
<feature_engineering>
print("Train File", df_train.isnull().any().sum())
 print("Test File", df_test.isnull().any().sum() )
train = train[train['age']>0]
train.loc[train['NumberOfTime30-59DaysPastDueNotWorse']>20, 'NumberOfTime30-59DaysPastDueNotWorse'] = 6
train.loc[train['NumberOfTimes90DaysLate']>20, 'NumberOfTimes90DaysLate'] = 2
train.loc[train['NumberOfTime60-89DaysPastDueNotWorse']>20, 'NumberOfTime60-89DaysPastDueNotWorse'] = 3<train_model>
y_train = df_train['label'].astype('float32')
 X_train = df_train.drop(['label'], axis=1 ).astype('int32')
 X_test = df_test.astype('float32')
 X_train.shape, y_train.shape, X_test.shape
train["NumberOfDependents"].fillna(train["NumberOfDependents"].mode() [0], inplace=True)
 mData = train.iloc[:,[6,2,3,4,5,7,8,9,10,11]]
train_known = mData[mData.MonthlyIncome.notnull() ].values
train_unknown = mData[mData.MonthlyIncome.isnull() ].values
train_X = train_known[:,1:]
train_y = train_known[:,0]
rfr = RandomForestRegressor(random_state=0,n_estimators=200,max_depth=3,n_jobs=-1)
 rfr.fit(train_X,train_y)
 predicted_y = rfr.predict(train_unknown[:,1:] ).round(0)
 train.loc[train.MonthlyIncome.isnull() ,'MonthlyIncome'] = predicted_y 
<split>
X_train = X_train/255
X_test = X_test/255
train_X = train_2[train_2.columns[2:]]
train_y = train_2[train_2.columns[1]]
train_X, test_X, train_y, test_y = train_test_split(train_X, train_y, test_size=0.1, random_state=20, stratify=train_y )<predict_on_test>
y_train = to_categorical(y_train, num_classes = 10)
 y_train.shape
lgbm = LGBMClassifier(max_depth=20,num_leaves=30,learning_rate=0.02,n_estimators=270,feature_fraction=0.7)
 lgbm.fit(train_X,train_y)
 pre_y = lgbm.predict_proba(test_X)[:,1]<feature_engineering>
x = tf.Variable(5.0)
 with tf.GradientTape() as tape:
y = x**3
test=pd.read_csv('/kaggle/input/GiveMeSomeCredit/cs-test.csv')
 test.info()
test.loc[test['NumberOfTime30-59DaysPastDueNotWorse']>20, 'NumberOfTime30-59DaysPastDueNotWorse'] = 6
test.loc[test['NumberOfTimes90DaysLate']>20, 'NumberOfTimes90DaysLate'] = 2
test.loc[test['NumberOfTime60-89DaysPastDueNotWorse']>20, 'NumberOfTime60-89DaysPastDueNotWorse'] = 3
test.describe()<find_best_model_class>
dy_dx = tape.gradient(y, x)
 dy_dx.numpy()
test["NumberOfDependents"].fillna(train["NumberOfDependents"].mode() [0], inplace=True)
 mData2 = test.iloc[:,[6,2,3,4,5,7,8,9,10,11]]
test_known = mData2[mData2.MonthlyIncome.notnull() ].values
test_unknown = mData2[mData2.MonthlyIncome.isnull() ].values
test_X2 = test_known[:,1:]
test_y2 = test_known[:,0]
rfr2 = RandomForestRegressor(random_state=0,n_estimators=200,max_depth=3,n_jobs=-1)
 rfr2.fit(test_X2,test_y2)
 predicted_y = rfr2.predict(test_unknown[:,1:] ).round(0)
 test.loc[test.MonthlyIncome.isnull() ,'MonthlyIncome'] = predicted_y<predict_on_test>
w = tf.Variable(tf.random.normal(( 3, 2)) , name='w')
 b = tf.Variable(tf.zeros(2, dtype=tf.float32), name='b')
 x = [[1., 2., 3.]]
with tf.GradientTape(persistent=True)as tape:
y = x @ w + b
loss = tf.reduce_mean(y**2)
test2 = test[test.columns[2:]]
pre_y2 = lgbm.predict_proba(test2)[:,1]<save_to_csv>
[dl_dw, dl_db] = tape.gradient(loss, [w, b] )
result=pd.read_csv('/kaggle/input/GiveMeSomeCredit/sampleEntry.csv')
 result['Probability'] = pre_y2
result.to_csv('./submit.csv',index=False)
 reload = pd.read_csv('./submit.csv')
 reload<import_modules>
x = tf.constant(3.0)
 with tf.GradientTape() as g:
g.watch(x)
 with tf.GradientTape() as gg:
gg.watch(x)
 y = x * x
dy_dx = gg.gradient(y, x)
 print(dy_dx.numpy())
 d2y_dx2 = g.gradient(dy_dx, x)
 print(d2y_dx2.numpy() )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score,roc_curve, auc
from sklearn.ensemble import RandomForestRegressor
from lightgbm import LGBMClassifier<load_from_csv>
def build_model(width, height, depth, classes):
inputShape =(height, width, depth)
 chanDim = -1
model = Sequential([
Conv2D(16,(3, 3), padding="same", input_shape=inputShape),
Activation("relu"),
BatchNormalization(axis=chanDim),
MaxPooling2D(pool_size=(2, 2)) ,
Conv2D(32,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
Conv2D(32,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
MaxPooling2D(pool_size=(2, 2)) ,
Conv2D(64,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
Conv2D(64,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
Conv2D(64,(3, 3), padding="same"),
Activation("relu"),
BatchNormalization(axis=chanDim),
MaxPooling2D(pool_size=(2, 2)) ,
Flatten() ,
Dense(256),
Activation("relu"),
BatchNormalization() ,
Dropout(0.5),
Dense(classes),
Activation("softmax")
 ])
 return model
train=pd.read_csv('/kaggle/input/GiveMeSomeCredit/cs-training.csv')
 train.info()
train = train[train['age']>0]

 train.loc[train['NumberOfTime30-59DaysPastDueNotWorse']>20, 'NumberOfTime30-59DaysPastDueNotWorse'] = 6
train.loc[train['NumberOfTimes90DaysLate']>20, 'NumberOfTimes90DaysLate'] = 2
train.loc[train['NumberOfTime60-89DaysPastDueNotWorse']>20, 'NumberOfTime60-89DaysPastDueNotWorse'] = 3
train.describe(percentiles=[.10,.20,.30,.40,.60,.70,.80,.90,.95,.99,.999] )<train_model>
def step(X, y):
with tf.GradientTape() as tape:
pred = model(X)
 loss = categorical_crossentropy(y, pred)
 grads = tape.gradient(loss, model.trainable_variables)
 opt.apply_gradients(zip(grads, model.trainable_variables))
train["NumberOfDependents"].fillna(train["NumberOfDependents"].mode() [0], inplace=True)
 mData = train.iloc[:,[6,2,3,4,5,7,8,9,10,11]]
train_known = mData[mData.MonthlyIncome.notnull() ].values
train_unknown = mData[mData.MonthlyIncome.isnull() ].values
train_X = train_known[:,1:]
train_y = train_known[:,0]
rfr = RandomForestRegressor(random_state=0,n_estimators=200,max_depth=3,n_jobs=-1)
 rfr.fit(train_X,train_y)
 predicted_y = rfr.predict(train_unknown[:,1:] ).round(0)
 train.loc[train.MonthlyIncome.isnull() ,'MonthlyIncome'] = predicted_y 
<split>
EPOCHS = 50
BS = 32
INIT_LR = 1e-3
model = build_model(28, 28, 1, 10)
 opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
 numUpdates = int(X_train.shape[0] / BS)
 for epoch in range(0, EPOCHS):
print("[INFO] starting epoch {}/{}...".format(epoch + 1, EPOCHS), end="")
 sys.stdout.flush()
epochStart = time.time()
for i in range(0, numUpdates):
start = i * BS
end = start + BS
step(X_train[start:end], y_train[start:end])
 epochEnd = time.time()
elapsed =(epochEnd - epochStart)/ 60.0
print("took {:.4} minutes".format(elapsed))
train_X = train_2[train_2.columns[2:]]
train_y = train_2[train_2.columns[1]]
train_X, test_X, train_y, test_y = train_test_split(train_X, train_y, test_size=0.1, random_state=20, stratify=train_y )<predict_on_test>
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image("model.png" )
lgbm = LGBMClassifier(max_depth=20,num_leaves=30,learning_rate=0.02,n_estimators=250,feature_fraction=0.7)
 lgbm.fit(train_X,train_y)
 pre_y = lgbm.predict_proba(test_X)[:,1]<feature_engineering>
model.compile(optimizer=opt, loss=categorical_crossentropy,metrics=["acc"])
test=pd.read_csv('/kaggle/input/GiveMeSomeCredit/cs-test.csv')
 test.info()
test.loc[test['NumberOfTime30-59DaysPastDueNotWorse']>20, 'NumberOfTime30-59DaysPastDueNotWorse'] = 6
test.loc[test['NumberOfTimes90DaysLate']>20, 'NumberOfTimes90DaysLate'] = 2
test.loc[test['NumberOfTime60-89DaysPastDueNotWorse']>20, 'NumberOfTime60-89DaysPastDueNotWorse'] = 3
test.describe()<find_best_model_class>
y_pred = model.predict(X_test)
 y_pred = np.argmax(y_pred,axis=1)
 my_submission = pd.DataFrame({'ImageId': list(range(1, len(y_pred)+1)) , 'Label': y_pred})
 my_submission.to_csv('submission.csv', index=False )