Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<save_to_csv>
%matplotlib inline
submission['count'] = np.exp(randomforest_preds)
 submission.to_csv('submission.csv', index=False )<import_modules>
base_set = pd.read_csv('.. /input/digit-recognizer/train.csv' )
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_log_error
from sklearn.model_selection import GridSearchCV
from sklearn.experimental import enable_hist_gradient_boosting
from sklearn.ensemble import HistGradientBoostingRegressor 
<load_from_csv>
label_column = 'label'
X = base_set.drop(columns=[label_column] ).values
Y = base_set[label_column]
df=pd.read_csv('.. /input/bike-sharing-demand/train.csv')
 test=pd.read_csv('.. /input/bike-sharing-demand/test.csv')
<count_missing_values>
X = X / 255
def reshape_data(np_array):
return np_array.reshape(len(np_array), 28, 28, 1)
 X = reshape_data(X )
df.isnull().sum()<feature_engineering>
Y = to_categorical(Y, 10 )
df['Date_time'] = df['datetime'].astype('datetime64[ns]')
 df['Year']=df.Date_time.dt.year
df['Month']=df.Date_time.dt.month
df['Day']=df.Date_time.dt.day
df['Hour']=df.Date_time.dt.hour
test['Date_time'] = test['datetime'].astype('datetime64[ns]')
 test['Year']=test.Date_time.dt.year
test['Month']=test.Date_time.dt.month
test['Day']=test.Date_time.dt.day
test['Hour']=test.Date_time.dt.hour<count_values>
train_to_valtest_ratio =.32
validate_to_test_ratio =.5 
(X_train,
X_validation_and_test,
Y_train,
Y_validation_and_test)= train_test_split(X, Y, test_size=train_to_valtest_ratio)
(X_validation,
X_test,
Y_validation,
Y_test)= train_test_split(X_validation_and_test, Y_validation_and_test, test_size=validate_to_test_ratio )
for i in df.columns:
print(i)
 print(df[i].value_counts().sum)
 print("***********************************************" )<feature_engineering>
model = Sequential([
Conv2D(64, kernel_size=3, padding='same', activation='relu', input_shape=X.shape[1:]),
Conv2D(64, kernel_size=3, padding='same', activation='relu'),
Conv2D(128, kernel_size=3, padding='same', activation='relu'),
MaxPooling2D(pool_size=2),
Conv2D(128, kernel_size=3, padding='same', activation='relu'),
Conv2D(192, kernel_size=3, padding='same', activation='relu'),
MaxPooling2D(pool_size=2),
Conv2D(192, kernel_size=5, padding='same', activation='relu'),
MaxPooling2D(pool_size=2, padding='same'),
Dropout(0.25),
Flatten() ,
Dense(256, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax')
 ])
 model.summary()
df = df.drop(columns=['holiday'])
 test = test.drop(columns=['holiday'])
 df['windspeed']=df['windspeed'].replace(0,df['windspeed'].median())
 test['windspeed']=test['windspeed'].replace(0,test['windspeed'].median())
 df['weekday'] = 0
for ind in df.index:
df['weekday'][ind]= calendar.weekday(df['Year'][ind], df['Month'][ind] , df['Day'][ind])
 test['weekday'] = 0
for ind in test.index:
test['weekday'][ind]= calendar.weekday(test['Year'][ind], test['Month'][ind] , test['Day'][ind])
 df['is_night'] = 0
df.loc[(df['Hour'] < 6)|(df['Hour'] > 20), 'is_night'] = 1
test['is_night'] = 0
test.loc[(test['Hour'] < 6)|(test['Hour'] > 20), 'is_night'] = 1
df["weekend"] = df["weekday"].apply(lambda x: 1 if x == 5 or x == 6 else 0)
 test["weekend"] = test["weekday"].apply(lambda x: 1 if x == 5 or x == 6 else 0)
 df["day_cos"] = np.cos(( df['Hour'])*(2 * np.pi / 24))
df["day_sin"] = np.sin(( df['Hour'])*(2 * np.pi / 24))
df["month_cos"] = np.cos(( df['Month'])*(2 * np.pi / 12))
df["month_sin"] = np.sin(( df['Month'])*(2 * np.pi / 12))
df["weekday_sin"] = np.sin(( df['weekday'])*(2 * np.pi / 7))
df["weekday_cos"] = np.cos(( df['weekday'])*(2 * np.pi / 7))
test["day_cos"] = np.cos(( test['Hour'])*(2 * np.pi / 24))
test["day_sin"] = np.sin(( test['Hour'])*(2 * np.pi / 24))
test["month_cos"] = np.cos(( test['Month'])*(2 * np.pi / 12))
test["month_sin"] = np.sin(( test['Month'])*(2 * np.pi / 12))
test["weekday_sin"] = np.sin(( test['weekday'])*(2 * np.pi / 7))
test["weekday_cos"] = np.cos(( test['weekday'])*(2 * np.pi / 7))
Q1_windspeed = df['windspeed'].quantile(0.25)
 Q3_windspeed = df['windspeed'].quantile(0.75)
 IQR1 = Q3_windspeed - Q1_windspeed
Q1_causal = df['casual'].quantile(0.25)
 Q3_causal = df['casual'].quantile(0.75)
 IQR2 = Q3_causal - Q1_causal
Q1_registered = df['registered'].quantile(0.25)
 Q3_registered = df['registered'].quantile(0.75)
 IQR3 = Q3_registered - Q1_registered
df.loc[df.windspeed > 16.997900, 'windspeed'] = np.nan
df['windspeed'].fillna(1.5*IQR1,inplace=True)
 df.loc[df.casual > 49.000000, 'casual'] = np.nan
df['casual'].fillna(1.5*IQR2,inplace=True)
 df.loc[df.registered >222.000000, 'registered'] = np.nan
df['registered'].fillna(1.5*IQR3,inplace=True )<prepare_x_and_y>
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] )
train_df, val_df = train_test_split(df, test_size=0.2, random_state=42,shuffle=True)
 X_train = train_df.drop(columns=['count'])
 y_train = train_df['count']
X_val = val_df.drop(columns=['count'])
 y_val = val_df['count']
print(list(train_df.columns))<drop_column>
reduce_learning_reducer = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.3, min_lr=0.00001)
 early_stopper = EarlyStopping(monitor = 'val_loss', min_delta = 1e-10, patience = 10, verbose = 1, restore_best_weights = True)
 training_result = model.fit(X_train, Y_train,
batch_size=96,
epochs=150,
validation_data=(X_validation, Y_validation),
callbacks=[reduce_learning_reducer, early_stopper] )
X_train = X_train [['weekend','is_night','weekday','temp','season', 'workingday', 'weather','humidity', 'windspeed','Year', 'Month', 'Day', 'Hour']]
X_val = X_val [list(X_train.columns)]
X_test = test[list(X_train.columns)]<compute_train_metric>
test_result = model.test_on_batch(X_test, Y_test)
 test_result
RegModel=XGBRegressor(learning_rate = 0.07,max_depth = 10, alpha = 6, n_estimators = 350)
 y_train = np.log(train_df['count']+1)
 RegModel.fit(X_train,y_train)
 train_predict=RegModel.predict(X_train)
 train_predict=np.exp(train_predict)
 Error1= np.sqrt(mean_squared_log_error(np.exp(y_train), train_predict))
print('RMSLE Training:' + str(Error1))
predict= RegModel.predict(X_val)
 predict=np.exp(predict)
 Error= np.sqrt(mean_squared_log_error(y_val , predict))
print('RMSLE Validation:' + str(Error))<save_to_csv>
score = model.evaluate(X_test, Y_test, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )
y_test_predicted = RegModel.predict(X_test)
 y_test_predicted=np.exp(y_test_predicted)
 test['count'] = y_test_predicted.astype(int)
 test[['datetime', 'count']].to_csv('/kaggle/working/submission.csv', index=False)
<set_options>
benchmark = pd.read_csv('.. /input/digit-recognizer/test.csv')
 benchmark = benchmark / 255
benchmark = reshape_data(benchmark.values )
%matplotlib inline<load_from_csv>
prediction = model.predict(benchmark )
train = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv' )<drop_column>
submission = pd.DataFrame({
'ImageId': [i + 1 for i in range(len(benchmark)) ],
'Label': [values.argmax() for values in prediction]
})
 submission.head()
train.drop(['casual', 'registered'], axis=1, inplace=True )<data_type_conversions>
submission.to_csv('submission.csv', index=False )
train['datetime'] = pd.to_datetime(train['datetime'], errors='coerce')
 test['datetime'] = pd.to_datetime(test['datetime'], errors='coerce')
<define_variables>
submission.to_csv('submission.csv', index=False )