Kaggle competition: Aerial Cactus Identification
sample_sub['SalePrice']=final_pred<save_to_csv>
TRAIN_IMAGES_PATH = '.. /input/train/train'
TEST_IMAGES_PATH = '.. /input/test/test'
sample_sub.to_csv('SampleSubmissionForHousePrice.csv',index=False )<import_modules>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/sample_submission.csv')
 train_image_ids = train_df['id']
training_labels = train_df['has_cactus']
test_image_ids = test_df['id']
test_labels = test_df['has_cactus']
warnings.filterwarnings("ignore")
 pd.set_option('display.max_columns', 5000)
 pd.set_option('display.max_rows', 5000)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
all_train_images, normalized_images = get_images(TRAIN_IMAGES_PATH, train_image_ids)
 test_images, normalized_test_images = get_images(TEST_IMAGES_PATH, test_image_ids )
train=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 sample_sub=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv' )<filter>
augs = [np.fliplr, np.flipud, np.rot90]
def augment(images, labels, augs):

 all_images = list()
all_labels = list()
for i,image in tqdm_notebook(enumerate(images)) :
all_images.append(image)
 cur_label = labels[i]
all_labels.append(cur_label)
 if cur_label == 1:
all_images.append(augs[random.randint(0,2)](image))
all_labels.append(cur_label)
 else:
for aug in augs:
all_labels.append(cur_label)
 all_images.append(aug(image))
return np.stack(all_images), np.array(all_labels)
train[train.SalePrice>300000]<sort_values>
normalized_train_images, final_training_labels = augment(normalized_images, np.array(training_labels), augs)
 NUM_TRAIN_IMAGES = int(0.75 * normalized_train_images.shape[0])
 indices = np.random.permutation(normalized_train_images.shape[0])
 training_idx, val_idx = indices[:NUM_TRAIN_IMAGES], indices[NUM_TRAIN_IMAGES:]
train_data = normalized_train_images[training_idx,:]
train_labels = np.array(final_training_labels)[training_idx]
val_data = normalized_train_images[val_idx,:]
val_labels = final_training_labels[val_idx]
corr_dict=correlation_train['SalePrice'].sort_values(ascending=False ).to_dict()
important_columns=[]
for key,value in corr_dict.items() :
if(( value>0.1)&(value<0.8)) |(value<=-0.1):
important_columns.append(key)
 important_columns<concatenate>
def get_weights(shape):

 initializer = tf.contrib.layers.xavier_initializer()
return tf.Variable(initializer(shape=shape))
def get_biases(length):

 return tf.Variable(tf.constant(0.0005, shape=[length]))
train_test=pd.concat([train,test],axis=0,sort=False)
 train_test.head()<create_dataframe>
def conv_layer(input, in_channels, filter_size, num_filters):

 shape = [filter_size, filter_size, in_channels, num_filters]
weights = get_weights(shape)
 bias = get_biases(num_filters)
 layer = tf.nn.convolution(input,
weights,
strides=[1,1], dilation_rate=[1,1],
padding="SAME")
 layer= tf.nn.bias_add(layer, bias)
 new_layer = tf.nn.relu(layer)
 print(new_layer)
 return new_layer, weights
def flatten(input_tensor):

 layer_shape = input_tensor.get_shape()
total_elements = layer_shape[1:4].num_elements()
layer = tf.reshape(input_tensor, [-1, total_elements])
 return layer, total_elements
def fc_layer(input, in_features, out_features):

 weight = get_weights([in_features, out_features])
 bias = get_biases(out_features)
 layer = tf.matmul(input, weight)+ bias
return layer
pd.set_option('display.max_rows', 5000)
 train_test_null_info=pd.DataFrame(train_test.isnull().sum() ,columns=['Count of NaN'])
 train_test_dtype_info=pd.DataFrame(train_test.dtypes,columns=['DataTypes'])
 train_tes_info=pd.concat([train_test_null_info,train_test_dtype_info],axis=1)
 train_tes_info<data_type_conversions>
input_image = tf.placeholder(name="input", shape=(None, 32, 32, 3), dtype=tf.float32)
 labels = tf.placeholder(name="labels", shape=(None), dtype=tf.int64)
 with tf.variable_scope("block1_conv1"):
layer_conv1, weights_1 = conv_layer(input_image, 3, 3, 32)
 with tf.variable_scope("block1_conv2"):
layer_conv2, weights_2 = conv_layer(layer_conv1, 32, 3, 32)
 with tf.variable_scope("block2_conv1"):
layer_conv3, weights_3 = conv_layer(layer_conv2, 32, 3, 64)
 with tf.variable_scope("block2_conv2"):
layer_conv4, weights_4 = conv_layer(layer_conv3, 64, 3, 64)
 layer_output_pool = tf.nn.max_pool(layer_conv4, ksize = [1,4,4,1], strides = [1,2,2,1], padding = "VALID")
 with tf.variable_scope("block3_conv1"):
layer_conv4, weights_4 = conv_layer(layer_output_pool, 64, 3, 128)
 with tf.variable_scope("block3_conv2"):
layer_conv5, weights_5 = conv_layer(layer_conv4, 128, 3, 128)
 with tf.variable_scope("block3_conv3"):
layer_conv6, weights_6 = conv_layer(layer_conv5, 128, 3, 128)
 layer_output_pool = tf.nn.max_pool(layer_conv6, ksize = [1,4,4,1], strides = [1,2,2,1], padding = "VALID")
 print(layer_output_pool, layer_conv4)
 flattened_layer, in_features = flatten(layer_output_pool)
 fclyr = fc_layer(flattened_layer, in_features, 128)
 fc_layer2 = tf.nn.relu(fclyr)
 final_layer = fc_layer(fc_layer2, 128, 2)
 y_pred = tf.nn.softmax(final_layer)
 cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=final_layer,
labels=tf.one_hot(labels,2))
cost = tf.reduce_mean(cross_entropy)
 optimizer = tf.train.AdamOptimizer(learning_rate=0.001 ).minimize(cost)
 gpu_options = tf.GPUOptions(allow_growth=True)
 config = tf.ConfigProto(gpu_options=gpu_options)
 init=tf.global_variables_initializer()
train_test.loc[train_test['Fireplaces']==0,'FireplaceQu']='Nothing'
train_test['LotFrontage'] = train_test['LotFrontage'].fillna(train_test.groupby('1stFlrSF')['LotFrontage'].transform('mean'))
train_test['LotFrontage'].interpolate(method='linear',inplace=True)
 train_test['LotFrontage']=train_test['LotFrontage'].astype(int)
 train_test['MasVnrArea'] = train_test['MasVnrArea'].fillna(train_test.groupby('MasVnrType')['MasVnrArea'].transform('mean'))
train_test['MasVnrArea'].interpolate(method='linear',inplace=True)
 train_test['MasVnrArea']=train_test['MasVnrArea'].astype(int)
 train_test["Fence"] = train_test["Fence"].fillna("None")
 train_test["FireplaceQu"] = train_test["FireplaceQu"].fillna("None")
 train_test["Alley"] = train_test["Alley"].fillna("None")
 train_test["PoolQC"] = train_test["PoolQC"].fillna("None")
 train_test["MiscFeature"] = train_test["MiscFeature"].fillna("None")
 train_test.loc[train_test['BsmtFinSF1']==0,'BsmtFinType1']='Unf'
train_test.loc[train_test['BsmtFinSF2']==0,'BsmtQual']='TA'
train_test['YrBltRmd']=train_test['YearBuilt']+train_test['YearRemodAdd']
train_test['Total_Square_Feet'] =(train_test['BsmtFinSF1'] + train_test['BsmtFinSF2'] + train_test['1stFlrSF'] + train_test['2ndFlrSF'] + train_test['TotalBsmtSF'])
 train_test['Total_Bath'] =(train_test['FullBath'] +(0.5 * train_test['HalfBath'])+ train_test['BsmtFullBath'] +(0.5 * train_test['BsmtHalfBath']))
train_test['Total_Porch_Area'] =(train_test['OpenPorchSF'] + train_test['3SsnPorch'] + train_test['EnclosedPorch'] + train_test['ScreenPorch'] + train_test['WoodDeckSF'])
 train_test['exists_pool'] = train_test['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 train_test['exists_garage'] = train_test['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 train_test['exists_fireplace'] = train_test['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 train_test['exists_bsmt'] = train_test['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 train_test['old_house'] = train_test['YearBuilt'].apply(lambda x: 1 if x <1990 else 0)
 for i in train_test.columns:
if 'SalePrice' not in i:
if 'object' in str(train_test[str(i)].dtype):
train_test[str(i)]=train_test[str(i)].fillna(method='ffill')
<categorify>
sess = tf.Session(config = config)
 NUM_ITERATIONS = 20
BATCH = 32
sess.run(init)
 for i in tqdm_notebook(range(NUM_ITERATIONS)) :
num_batches = int(train_data.shape[0] / BATCH)+ 1
losses = list()
epoch_predictions = list()
for j in tqdm_notebook(range(num_batches)) :
batch_labels = train_labels[BATCH*j: BATCH*j + BATCH].astype(np.int64)
 batch_data = train_data[BATCH*j: BATCH*j + BATCH]
loss, _, probabilities = sess.run([cost, optimizer, y_pred], feed_dict={input_image:batch_data, labels:batch_labels})
 predictions = np.argmax(probabilities, axis=1)
 epoch_predictions.extend(predictions)
 losses.append(loss)
 print("EPOCH " + str(i))
epoch_predictions = np.array(epoch_predictions)
 train_loss = np.mean(np.array(losses))
train_accuracy =(np.sum(epoch_predictions==train_labels)/ train_labels.shape[0])* 100
num_batches = int(val_data.shape[0] / BATCH)+ 1
val_predictions = list()
for j in range(num_batches):
batch_data = val_data[BATCH*j: BATCH*j + BATCH]
batch_labels = val_labels[BATCH*j: BATCH*j + BATCH].astype(np.int64)
 loss, _, probabilities = sess.run([cost, optimizer, y_pred], feed_dict={input_image:batch_data, labels:batch_labels})
 predictions = np.argmax(probabilities, axis=1)
 val_predictions.extend(predictions)
 losses.append(loss)
 val_accuracy =(np.sum(val_predictions==val_labels)/ val_labels.shape[0])* 100
print("Loss after Epoch - %f, Train Accuracy - %f, Val Accuracy - %f" %(train_loss, train_accuracy, val_accuracy))
columns =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond', 'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1', 'YrSold', 'MoSold', 'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope', 'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond')
 for col in columns:
lbl_enc = LabelEncoder()
lbl_enc.fit(list(train_test[col].values))
train_test[col] = lbl_enc.transform(list(train_test[col].values))<sort_values>
test_preds = list()
num_batches = int(normalized_test_images.shape[0] / BATCH)+ 1
for j in range(num_batches):
batch_data = normalized_test_images[BATCH*j: BATCH*j + BATCH]
batch_labels = test_labels[BATCH*j: BATCH*j + BATCH].astype(np.int64)
 probabilities = sess.run(y_pred, feed_dict={input_image:batch_data, labels:batch_labels})
 predictions = np.argmax(probabilities, axis=1)
 test_preds.extend(predictions)
<filter><EOS> 
submission_df = test_df
submission_df['has_cactus'] = np.array(test_preds)
 submission_df.to_csv("submissions.csv",index=False )