Kaggle competition: Galaxy Zoo - The Galaxy Challenge
<SOS> metric: RMSE Kaggle data source: galaxy-zoo-the-galaxy-challenge<data_type_conversions>
df = pd.read_csv('.. /input/44352/training_solutions_rev1.csv')
 df_train, df_test = train_test_split(df, test_size=.2)
 df_train.shape, df_test.shape
train["Date"] = train["Date"].apply(lambda x: datetime.strptime(x,'%Y-%m-%d'))
test["Date"] = test["Date"].apply(lambda x: datetime.strptime(x,'%Y-%m-%d')) 
<feature_engineering>
%matplotlib inline
ORIG_SHAPE =(424,424)
 CROP_SIZE =(256,256)
 IMG_SHAPE =(64,64)
 def get_image(path, x1,y1, shape, crop_size):
x = plt.imread(path)
 x = x[x1:x1+crop_size[0], y1:y1+crop_size[1]]
x = resize(x, shape)
 x = x/255.
 return x
def get_all_images(dataframe, shape=IMG_SHAPE, crop_size=CROP_SIZE):
x1 =(ORIG_SHAPE[0]-CROP_SIZE[0])//2
y1 =(ORIG_SHAPE[1]-CROP_SIZE[1])//2
sel = dataframe.values
ids = sel[:,0].astype(int ).astype(str)
 y_batch = sel[:,1:]
x_batch = []
for i in tqdm(ids):
x = get_image('.. /input/44352/images_training_rev1/'+i+'.jpg', x1,y1, shape=shape, crop_size=crop_size)
 x_batch.append(x)
 x_batch = np.array(x_batch)
 return x_batch, y_batch
X_train, y_train = get_all_images(df_train)
 X_test, y_test = get_all_images(df_test )
dataset = [train,test]
for data in dataset:
data['Year'] = data['Date'].dt.year
data['Month'] = data['Date'].dt.month
data['Day'] = data['Date'].dt.day
data['Quarter'] = data['Date'].dt.quarter<data_type_conversions>
def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true)))
 model = Sequential()
model.add(Conv2D(512,(3, 3), input_shape=(IMG_SHAPE[0], IMG_SHAPE[1], 3)))
 model.add(Conv2D(256,(3, 3)))
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(256,(3, 3)))
 model.add(Conv2D(128,(3, 3)))
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(3, 3)))
 model.add(Conv2D(128,(3, 3)))
 model.add(Activation('relu'))
model.add(GlobalMaxPooling2D())
 model.add(Dropout(0.25))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(37))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adamax', metrics=[root_mean_squared_error])
 model.summary()
train.insert(10,"TS",0)
 test.insert(8,"TS",0)
 train["TS"] = train["Date"].apply(lambda x: x.timestamp())
 print(train['Date'])
 train["TS"] = train["TS"].astype(int)
 print(train['TS'])
<data_type_conversions>
batch_size = 128
model.fit(X_train, y_train, epochs=30, validation_data=(X_test, y_test))
test["TS"] = test["Date"].apply(lambda x: x.timestamp())
 print(type(test['TS'][1]))
test["TS"] = test["TS"].astype(int)
 print(type(test['TS'][1]))<groupby>
def test_image_generator(ids, shape=IMG_SHAPE):
x1 =(ORIG_SHAPE[0]-CROP_SIZE[0])//2
y1 =(ORIG_SHAPE[1]-CROP_SIZE[1])//2
x_batch = []
for i in ids:
x = get_image('.. /input/44352/images_test_rev1/'+i, x1, y1, shape=IMG_SHAPE, crop_size=CROP_SIZE)
 x_batch.append(x)
 x_batch = np.array(x_batch)
 return x_batch
val_files = os.listdir('.. /input/44352/images_test_rev1/')
 val_predictions = []
N_val = len(val_files)
 for i in tqdm(np.arange(0, N_val, batch_size)) :
if i+batch_size > N_val:
upper = N_val
else:
upper = i+batch_size
X = test_image_generator(val_files[i:upper])
 y_pred = model.predict(X)
 val_predictions.append(y_pred)
 val_predictions = np.array(val_predictions)
 Y_pred = np.vstack(val_predictions)
 ids = np.array([v.split('.')[0] for v in val_files] ).reshape(len(val_files),1)
 submission_df = pd.DataFrame(np.hstack(( ids, Y_pred)) , columns=df.columns)
 submission_df = submission_df.sort_values(by=['GalaxyID'])
 submission_df.to_csv('sample_submission.csv', index=False )