Kaggle competition: Digit Recognizer
_=mk_model_xgb(xt,xv,yt,yv,func1=trans,func2=rev_trans,lr=0.2,min_child_weight =20,colsample_bytree = 0.8,md=20 )<train_model>
%matplotlib inline
model=XGBRegressor(colsample_bytree = 0.8, learning_rate = 0.2,min_child_weight =20, max_depth=20 ).fit(X,y.apply(trans)) 
<predict_on_test>
mnist_train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 mnist_test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
log_y_pred = model.predict(xtest )<concatenate>
mnist_train_data = mnist_train.loc[:, "pixel0":]
mnist_train_label = mnist_train.loc[:, "label"]
mnist_train_data = mnist_train_data/255.0
mnist_test = mnist_test/255.0
yp=np.apply_along_axis(rev_trans,arr=log_y_pred,axis=0)
 yp = np.round(yp )<prepare_output>
nclasses = mnist_train_label.max() - mnist_train_label.min() + 1
mnist_train_label = np_utils.to_categorical(mnist_train_label, num_classes = nclasses)
 print("Shape of y_train after encoding: ", mnist_train_label.shape )
submission = test[['datetime']].copy()
submission['count'] = yp
submission.head()<save_to_csv>
x_train=mnist_train_data
x_test=mnist_test_arr
y_train=mnist_train_label
x_train.shape
submission.to_csv('/kaggle/working/submission.csv', index=False )<import_modules>
x_val=x_train[33600:]
x=x_train[:33600]
y_val= y_train[33600:]
y=y_train[:33600]
y.shape
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.feature_selection import VarianceThreshold
from sklearn.linear_model import ElasticNet
import matplotlib.pyplot as plt
from sklearn.preprocessing import QuantileTransformer, quantile_transform
from sklearn.compose import TransformedTargetRegressor
from sklearn.linear_model import RidgeCV
from sklearn.compose import ColumnTransformer
import xgboost
from sklearn.metrics import mean_squared_log_error<feature_engineering>
from tensorflow.keras.preprocessing.image import ImageDataGenerator
def trans(x,l1=0.3,l2=0):
if l1!=0:
return(( x+l2)**l1-1)/l1
else:
return np.log(x+l2)
 def rev_trans(x,l1=0.3,l2=0):
return(x*l1+1)**(1/l1)-l2 
<load_from_csv>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x )
df = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv" )<feature_engineering>
from keras.optimizers import SGD
df['windspeed'] = np.where(df['windspeed']==0.0,8.9981,df['windspeed'] )<feature_engineering>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(5,5), activation='relu', input_shape=(x_train.shape[1:])) ,
tf.keras.layers.Conv2D(32,(5,5), activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)) ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(64,(5,5), activation='relu'),
tf.keras.layers.Conv2D(64,(5,5), activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)) ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(2048, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 learning_r= 0.01
weight_decay=1e-6
mometum=0.5
sgd= SGD(lr=learning_r,decay=weight_decay,momentum=mometum, nesterov=True)
 model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
 model.summary()
df['humidity'] = np.where(df['humidity']==0.0,8,df['humidity'] )<drop_column>
hist= model.fit(datagen.flow(x,y),epochs=50, validation_data=(x_val,y_val), verbose=1 )
df.drop(columns='temp',inplace = True )<feature_engineering>
p = model.predict(x_test )
df['windspeed'] = np.log(df['windspeed'] )<count_values>
p_t = []
for i in p:
p_t.append(np.argmax(i))
<feature_engineering><EOS> 
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": p_t
})
 submission.to_csv('my_submission.csv', index=False )