Kaggle competition: Digit Recognizer
test['label'] = np.argmax(preds, axis=1)
 test.head()
test.to_csv('submission.csv', index=False )<find_best_params>
dataset=pd.read_csv('.. /input/train.csv' )
del model
torch.cuda.empty_cache()<load_from_csv>
y=dataset['label']
train=pd.read_csv(r'.. /input/bike-sharing-demand/train.csv')
 test=pd.read_csv(r'.. /input/bike-sharing-demand/test.csv')
 df=train.copy()
test_df=test.copy()
df.head()<feature_engineering>
X_train=dataset.iloc[:,1:]
df['datetime'] = pd.to_datetime(df['datetime'])
 test_df['datetime'] = pd.to_datetime(test_df['datetime'])
 df['year'] = df['datetime'].apply(lambda x: x.year)
 df['month'] = df['datetime'].apply(lambda x: x.month)
 df['day'] = df['datetime'].apply(lambda x: x.day)
 df['hour'] = df['datetime'].apply(lambda x: x.hour)
 test_df['year'] = test_df['datetime'].apply(lambda x: x.year)
 test_df['month'] = test_df['datetime'].apply(lambda x: x.month)
 test_df['day'] = test_df['datetime'].apply(lambda x: x.day)
 test_df['hour'] = test_df['datetime'].apply(lambda x: x.hour)
 df.drop(['datetime', 'casual', 'registered'], axis=1, inplace=True)
 test_df.drop(['datetime'], axis=1, inplace=True)
 df<categorify>
X_test=pd.read_csv('.. /input/test.csv' )
df = pd.get_dummies(df, columns=['year', 'month', 'day', 'hour', 'holiday', 'workingday', 'season', 'weather'])
 test_df = pd.get_dummies(test_df, columns=['year', 'month', 'day', 'hour', 'holiday', 'workingday', 'season', 'weather'])
 df, test_df = df.align(test_df, join='left', axis=1)
 test_df = test_df.drop(['count'], axis=1)
 print(df.shape)
 print(test_df.shape )<compute_test_metric>
X_train=X_train/255.0
X_test=X_test/255.0
def rmsle(y, pred):
log_y = np.log1p(y)
 log_pred = np.log1p(pred)
 squared_error =(log_y - log_pred)**2
rmsle = np.sqrt(np.mean(squared_error))
return rmsle<train_on_grid>
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split, cross_val_score
import itertools
df_train_target = df['count']
df_train_features = df.drop('count',axis=1)
 def print_best_params(model, params):
grid_model = GridSearchCV(
 model,
param_grid = params,
cv=5,
scoring='neg_mean_squared_error')
 grid_model.fit(df_train_features, df_train_target)
 rmse = np.sqrt(-1*grid_model.best_score_)
 print(
 '{0} 5 CV 시 최적 평균 RMSE 값 {1}, 최적 alpha:{2}'.format(model.__class__.__name__, np.round(rmse, 4), grid_model.best_params_))
return grid_model.best_estimator_<feature_engineering>
target=to_categorical(y,10 )
df['count'] = np.log1p(df['count'] )<train_model>
X_t, X_v, Y_t, Y_v = train_test_split(X_train, target, test_size = 0.1 )
x_train,x_test,y_train,y_test=train_test_split(df.drop('count',axis=1),df['count'],test_size=0.3,random_state=42)
 lr_reg = LinearRegression()
lr_reg.fit(x_train, y_train)
 lr_pred = lr_reg.predict(x_test)
 y_test_exp = np.expm1(y_test)
 lr_pred_exp = np.expm1(lr_pred)
 print('LinearRegression RMSLE:', rmsle(y_test_exp, lr_pred_exp)) 
<compute_train_metric>
model=Sequential()
model.add(Conv2D(filters=64,kernel_size=(7,7),padding = 'Same',
activation ='relu', input_shape =(28,28,1)) )
rf_model = RandomForestRegressor()
rf_model.fit(x_train, y_train)
 rf_pred = rf_model.predict(x_test)
 y_test_exp = np.expm1(y_test)
 rf_pred_exp = np.expm1(rf_pred)
 print('RandomForest RMSLE:', rmsle(y_test_exp, rf_pred_exp))<compute_test_metric>
model.add(Conv2D(filters=64,kernel_size=(7,7),padding = 'Same',
activation ='relu'))
xgb_model = XGBRegressor(learning_rate=0.2)
 xgb_model.fit(x_train, y_train)
 xgb_pred = xgb_model.predict(x_test)
 y_test_exp = np.expm1(y_test)
 xgb_pred_exp = np.expm1(xgb_pred)
 print('xgboost RMSLE:', rmsle(y_test_exp, xgb_pred_exp))<import_modules>
model.add(MaxPool2D(pool_size=(2,2)) )
lgb_params = {
'learning_rate' : [0.05],
'n_estimators':[500],
'max_bin' : [80],
}
lgb_model = LGBMRegressor()
lgb_model.fit(x_train, y_train)
 lgb_pred = lgb_model.predict(x_test)
 y_test_exp = np.expm1(y_test)
 lgb_pred_exp = np.expm1(lgb_pred)
 print('LGBMRegressor RMSLE:', rmsle(y_test_exp,lgb_pred_exp))
lgb_estimator = print_best_params(lgb_model, lgb_params)
<prepare_x_and_y>
model.add(Dropout(0.3))
X_train = df.drop(['count'], axis=1)
 y_train = df['count']
X_test = test_df
X_test<load_from_csv>
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)) )
lgb_model = LGBMRegressor()
lgb_model.fit(X_train, y_train)
 pred = lgb_model.predict(X_test)
 pred_exp = np.expm1(pred)
 submission = pd.read_csv('.. /input/bike-sharing-demand/sampleSubmission.csv')
 submission.loc[:, 'count'] = pred_exp
submission<save_to_csv>
model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(512, activation = "relu", use_bias= True))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
submission.to_csv('submission.csv', index=False )<save_to_csv>
model.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )
submission.to_csv('submission.csv', index=False )<set_options>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.2,
width_shift_range=0.2,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_t )
warnings.filterwarnings('always')
 warnings.filterwarnings('ignore')
 style.use('fivethirtyeight')
 sns.set(style='whitegrid',color_codes=True)
<load_from_csv>
model.fit_generator(datagen.flow(X_t,Y_t, batch_size= 82),
epochs = 60, validation_data =(X_v,Y_v),
verbose = 2, steps_per_epoch=X_t.shape[0] // 82)
train=pd.read_csv(r'.. /input/bike-sharing-demand/train.csv')
 test=pd.read_csv(r'.. /input/bike-sharing-demand/test.csv')
 df=train.copy()
test_df=test.copy()
df.head()<count_values>
ewsult=model.predict(X_test )
df.season.value_counts()<count_values>
ewsult=np.argmax(ewsult,axis=1 )
df.holiday.value_counts() 
<count_values>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),ewsult],axis = 1)
 submission.to_csv("cnnmodel.csv",index=False )