Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
%matplotlib inline
%matplotlib inline
pd.set_option("display.max_columns", 100)
 def fxn() :
warnings.warn("deprecated", DeprecationWarning)
 with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 fxn() 
<load_from_csv>
X = pd.read_csv('.. /input/digit-recognizer/train.csv')
 X_test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 X.shape, X_test.shape
data=pd.read_csv('.. /input/bike-sharing-demand/train.csv')
<rename_columns>
X_train = X.drop(['label'],1)
 Y_train = X['label']
X_train.shape
data=data.rename(columns={col_names[5]: "Temperature(C)",col_names[6]:"Dew Temperature(C)",col_names[7]: "humidity(%)",
col_names[8]:"Wind Speed(m/s)",col_names[11]:"Number of rented bikes"
} )<count_missing_values>
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train = X_train/255
X_test = X_test/255
X_train = X_train.reshape(-1, 28, 28, 1)
 X_test = X_test.reshape(-1,28 ,28, 1)
 X_train.shape, X_test.shape
data.isna().sum()<feature_engineering>
Y_train= tf.keras.utils.to_categorical(Y_train, 10)
 Y_train.shape
def convert_date(df):
df['datetime'] = pd.to_datetime(df['datetime'])
 df['month'] = df['datetime'].dt.month
df['hour'] = df['datetime'].dt.hour
df['weekday'] = df['datetime'].dt.dayofweek
df["day"]=df["datetime"].dt.day
df["year"]=df["datetime"].dt.year
df['dayofweek'] = df['datetime'].dt.dayofweek
df['month_start'] = df['datetime'].dt.is_month_start
df['woy'] = df['datetime'].dt.isocalendar().week.astype(int)
 return df<data_type_conversions>
x_train, val_x, y_train, val_y = train_test_split(X_train, Y_train, test_size=0.20 )
data=convert_date(data )<create_dataframe>
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 15 )
df1VizSeasons=pd.DataFrame(data.groupby('season' ).sum() ['Number of rented bikes'].sort_values(ascending=False))
df1VizSeasons.style.background_gradient(cmap=sns.light_palette("red", as_cmap=True))<create_dataframe>
es = EarlyStopping(monitor='loss', patience=12)
 filepath="/kaggle/working/bestmodel.h5"
md = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min' )
df1VizDays=pd.DataFrame(data.groupby('dayofweek' ).sum() ['Number of rented bikes'].sort_values(ascending=False))
df1VizDays.style.background_gradient(cmap=sns.light_palette("red", as_cmap=True))<drop_column>
epochs = 30
num_classes = 10
batch_size = 128
input_shape =(28, 28, 1)
 adam = tf.keras.optimizers.Adam(0.001 )
def drop_cols(df):
df=df.drop(['datetime','casual','registered'],axis=1)
 return df<drop_column>
i = Input(shape=input_shape)
 x = Conv2D(32,(3, 3), strides=2, activation='relu' )(i)
 x = Conv2D(64,(3, 3), strides=2, activation='relu' )(x)
 x = Flatten()(x)
 x = Dropout(0.2 )(x)
 x = Dense(1024, activation='relu' )(x)
 x = Dropout(0.2 )(x)
 x = Dense(K, activation='softmax' )(x)
 model = Model(i, x)
 model.summary()
data=drop_cols(data )<define_variables>
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'] )
features_plot=['Number of rented bikes', 'Temperature(C)', 'humidity(%)',
'Wind Speed(m/s)', 'Dew Temperature(C)',
]<define_variables>
History = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
epochs = epochs,
validation_data =(val_x, val_y),
callbacks = [es,md],
shuffle = True
)
def find_skewed_boundaries(df, variable, distance):
IQR = df[variable].quantile(0.75)- df[variable].quantile(0.25)
 lower_boundary = df[variable].quantile(0.25)-(IQR * distance)
 upper_boundary = df[variable].quantile(0.75)+(IQR * distance)
 return upper_boundary, lower_boundary<feature_engineering>
pred = model1.predict(X_test)
 pred_class = np.argmax(pred,axis=1)
def additional(df):
df['ideal'] = df[['Temperature(C)', 'Wind Speed(m/s)']].apply(lambda x:(0, 1)[x['Temperature(C)'] > 27 and x['Wind Speed(m/s)'] < 30], axis = 1)
 df['sticky'] = df[['humidity(%)', 'workingday']].apply(lambda x:(0, 1)[x['workingday'] == 1 and x['humidity(%)'] >= 60], axis = 1)
 df["windchill"]=13.12+0.6215*df['Temperature(C)']-11.37*(df['Wind Speed(m/s)']*3.6)**0.16+0.3965*df['Temperature(C)'] \
*(df['Wind Speed(m/s)']*3.6)**0.16
df['newweather']=17.3+df['Temperature(C)']-0.11*df['humidity(%)']+0.34*df['Wind Speed(m/s)']
df['hour_sin'] = np.sin(2 * np.pi * df['hour']/23.0)
 df['hour_cos'] = np.cos(2 * np.pi * df['hour']/23.0)
 df['month_sin'] = np.sin(2 * np.pi * df['month']/12)
 df['month_cos'] = np.cos(2 * np.pi * df['month']/12)
 df['log_wind']=np.log1p(df['Wind Speed(m/s)'])
 df['log_hum']=np.log1p(df['humidity(%)'])
 return df<feature_engineering>
submissions=pd.DataFrame({"ImageId": list(range(1,len(pred_class)+1)) ,
"Label": pred_class})
 submissions.to_csv("submissions.csv", index=False, header=True)
 submissions