Kaggle competition: Bike Sharing Demand
generator = image.ImageDataGenerator(
 featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
vertical_flip=False,
horizontal_flip=False )<train_model>
train = pd.read_csv(".. /input/train.csv" )
num_train_sequences = len(X_train)
 num_val_sequences = len(X_val)
 print("
print("<train_model>
train = pd.read_csv(".. /input/train.csv", parse_dates = ["datetime"])
 test = pd.read_csv(".. /input/test.csv", parse_dates = ["datetime"] )
if(num_train_sequences % BATCH_SIZE)== 0:
steps_per_epoch = int(num_train_sequences / BATCH_SIZE)
 else:
steps_per_epoch = int(num_train_sequences / BATCH_SIZE)+ 1
if(num_val_sequences % BATCH_SIZE)== 0:
validation_steps = int(num_val_sequences / BATCH_SIZE)
 else:
validation_steps = int(num_val_sequences / BATCH_SIZE)+ 1
print("
print("<choose_model_class>
train["datetime"] = train["datetime"].astype("datetime64" )
LR = ReduceLROnPlateau(monitor="val_loss", factor=0.2, patience=2, min_lr=0.000001, verbose=1, cooldown=1 )<normalization>
train["datetime"] = pd.to_datetime(train["datetime"] )
def exponential_decay_fn(epoch):
return 0.01 * 0.1**(epoch / 20 )<normalization>
train["datetime"].dt.hour
def exponential_decay(lr, s):
def exponential_decay_fn(epoch):
return lr * 0.1**(epoch / s)
 return exponential_decay_fn
exponential_decay_fn = exponential_decay(lr=0.001, s=20 )<choose_model_class>
train["year"] = train["datetime"].dt.year
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
test["year"] = test["datetime"].dt.year
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
lr_scheduler = keras.callbacks.LearningRateScheduler(exponential_decay_fn )<define_variables>
train["datetime"].dt.day.value_counts()
callbacks = [
checkpoint,
LR,
lr_scheduler]<train_model>
VERBOSE = 1
history = model.fit_generator(generator.flow(X_train, y_train, batch_size=BATCH_SIZE),
validation_data=generator.flow(X_val, y_val, batch_size=BATCH_SIZE),
epochs=NP_EPOCHS,
verbose=VERBOSE,
steps_per_epoch=steps_per_epoch,
validation_steps=validation_steps,
class_weight=None,
initial_epoch=0,
callbacks=callbacks )<find_best_params>
y_train = train["count"]
values = {}
models = os.listdir(model_name)
 for model in models:
converted = model.replace(".h5", "")
 accuracy = float(converted.split("-")[-1])
 values.update({accuracy: model})
 key = max(values, key = values.get)
 best = values.get(key)
 print("Best model found: {}".format(best))<train_model>
train = train.drop(["datetime", "windspeed", "casual", "registered", "count"], 1)
 test.drop(["datetime", "windspeed"], 1, inplace=True )
model_path = model_name + best
print("Absolute path found: {}".format(model_path))<load_pretrained>
y_train = np.log1p(y_train )
model = load_model(model_path )<predict_on_test>
predictions = model.predict(test )<prepare_output>
from sklearn.ensemble import RandomForestRegressor
conversion = np.argmax(predictions, axis=1 )<train_model>
from sklearn.ensemble import RandomForestRegressor
print("No.f Id's available in the test dataset: {}".format(len(test_df['id'])))
 print("No.f predictions done by the model on test data: {}".format(len(result)) )<concatenate>
rf = RandomForestRegressor(n_estimators=100 )
submission = pd.concat([test_df['id'], result],axis = 1 )<save_to_csv>
rf.fit(train, y_train )
submission.to_csv("submission.csv", index=False )<load_from_csv>
preds = rf.predict(test )
sample_submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 x = train.iloc[:,1:].values
y = train.iloc[:,0].values
x_test = test.drop('id', axis=1 ).iloc[:,:].values
x = x.reshape(x.shape[0], 28, 28, 1)
 y = to_categorical(y, 10)
 x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
 x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.10, random_state=42)
 train_datagen = ImageDataGenerator(rescale=1./255.,
rotation_range=10,
width_shift_range=0.10,
height_shift_range=0.10,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(rescale=1./255.)<choose_model_class>
submission=pd.read_csv(".. /input/sampleSubmission.csv" )
optimizer = RMSprop(lr=0.001)
 model= Sequential()
model.add(Conv2D(24,kernel_size = 3, padding = "same", activation = "relu",input_shape =(28,28,1)))
 model.add(Conv2D(24,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(48 ,kernel_size = 5, padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.1))
model.add(Conv2D(48,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(48,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(96 ,kernel_size = 5, padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.1))
model.add(Conv2D(96,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(96,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(192 ,kernel_size = 5, padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256))
model.add(Dropout(0.2))
model.add(Dense(10, activation="softmax"))
model.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics = ["accuracy"] )<choose_model_class>
submission["count"] = preds
learning_rate_reduction= ReduceLROnPlateau(monitor='val_loss',
patience=1,
verbose=1,
factor=0.5,
min_lr=0.0001)
 early = EarlyStopping(monitor='val_loss', min_delta=0.0005, mode='min', verbose=1, patience=2)
 history = model.fit_generator(train_datagen.flow(x_train, y_train, batch_size=1024),
steps_per_epoch=100,
epochs=30,
validation_data=valid_datagen.flow(x_valid, y_valid),
validation_steps=50,
callbacks=[learning_rate_reduction] )<save_to_csv>
submission["count"] = np.expm1(preds )
predictions = model.predict_classes(x_test/255.)
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predictions
submission.to_csv("submission.csv", index=False )<import_modules>
submission.to_csv("allrf.csv", index=False )