Kaggle competition: Aerial Cactus Identification
X = traindf.drop(columns=['PassengerId', 'Survived'], axis=1)
 y = traindf['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30 )<create_dataframe>
print(tf.__version__ )
results = pd.DataFrame(columns=['Validation'], index=['Logistic Regression', 'Support Vector Machine', 'KNN', 'Random Forest'] )<compute_train_metric>
sns.set(font_scale=1.5 )
def log_reg(X_train, X_test, y_train, y_test):
logmodel = LogisticRegression(C=.01)
 logmodel.fit(X_train, y_train)
 predictions = logmodel.predict(X_test)
 print(accuracy_score(y_test, predictions))
return accuracy_score(y_test, predictions )<compute_train_metric>
MAIN_DIR = '.. /input/'
TRAIN_DIR = os.path.join(MAIN_DIR,"train/train/")
 TEST_DIR = os.path.join(MAIN_DIR, "test/test/")
 IMAGE_ROWS = 32
IMAGE_COLS = 32
CHANNELS = 3
BATCH_SIZE = 256
EPOCHS = 300
IMG_SHAPE =(IMAGE_ROWS, IMAGE_COLS,CHANNELS )
LR_preds = log_reg(X_train, X_test, y_train, y_test)
 results.loc['Logistic Regression', 'Validation'] = LR_preds<train_on_grid>
df_train = pd.read_csv(MAIN_DIR + "train.csv" )
def svm(X_train, X_test, y_train, y_test):
c_vals = list(range(1, 100))
accuracy = [0 for i in range(99)]
for i, c in enumerate(c_vals):
svc_model = SVC(C=c)
 svc_model.fit(X_train, y_train)
 predictions = svc_model.predict(X_test)
 accuracy[i] = accuracy_score(y_test, predictions)
 print("Best C Value:", c_vals[accuracy.index(max(accuracy)) ])
 print(accuracy)
 print("Prediction Accuracy: ", max(accuracy))
return max(accuracy)
<compute_test_metric>
df_train['has_cactus'].value_counts()
svm_preds = svm(X_train, X_test, y_train, y_test)
 results.loc['Support Vector Machine', 'Validation'] = svm_preds
results.head() 
<normalization>
df_train.isnull().any()
def knn(X_train, X_test, y_train, y_test):
scaler = StandardScaler()
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test)
 ks = [i + 1 for i in range(20)]
accuracy = [0 for i in range(20)]
for i, k in enumerate(ks):
knn = KNeighborsClassifier(n_neighbors = k)
 knn.fit(X_train, y_train)
 predictions = knn.predict(X_test)
 accuracy[i] = accuracy_score(y_test, predictions)
 print(ks)
 print(accuracy)
 print("Best k Value:", ks[accuracy.index(max(accuracy)) ])
 print("Prediction Accuracy: ", max(accuracy))
return max(accuracy )<compute_test_metric>
def cnn_graph() :
main_input = tf.keras.Input(shape=(IMAGE_ROWS,IMAGE_COLS,CHANNELS), name="main_input")
 x = tf.keras.layers.Conv2D(16,(3,3), padding='same', use_bias=False, name="conv_1" )(main_input)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(32,(3,3), padding='same', use_bias=False, name='conv_2' )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(64,(3,3), padding='same', use_bias=False, name="conv_3" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(64,(3,3), padding='same', use_bias=False, name="conv_4" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(128,(3,3), padding='same', use_bias=False, name="conv_5" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(128,(3,3), padding='same', use_bias=False, name="conv_6" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(256,(3,3), padding='same', use_bias=False, name="conv_7" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(256,(3,3), padding='same', use_bias=False, name="conv_8" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Conv2D(512,(3,3), padding='same', use_bias=False, name="conv_9" )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Conv2D(512,(3,3), padding='same', use_bias=False, name='conv_10' )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Activation('relu' )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 x = tf.keras.layers.Flatten()(x)
 x = tf.keras.layers.Dense(256, activation='relu', use_bias=True )(x)
 x = tf.keras.layers.BatchNormalization(axis=-1 )(x)
 x = tf.keras.layers.Dropout(0.25 )(x)
 outputs = tf.keras.layers.Dense(1, activation='sigmoid', name="output_sigmoid" )(x)
 model = tf.keras.Model(inputs=main_input, outputs=outputs)
 return model
knn_preds = knn(X_train, X_test, y_train, y_test)
 results.loc['KNN', 'Validation'] = knn_preds
results.head()<import_modules>
df = pd.read_csv(MAIN_DIR+'train.csv')
 df['has_cactus'] = df['has_cactus'].apply(str )
from sklearn.decomposition import PCA<save_to_csv>
model = cnn_graph()
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = 'binary_crossentropy',
metrics=['accuracy'] )
scaler = StandardScaler()
scaler.fit(X)
 test_feats = testdf.drop('PassengerId', axis=1)
 X = scaler.transform(X)
 test_feats = scaler.transform(test_feats)
 pca = PCA(n_components = 4)
 pca.fit(X)
 x_train_pca = pca.transform(X)
 x_test_pca = pca.transform(test_feats)
 svc_model = SVC(C = 1)
 svc_model.fit(x_train_pca, y)
 svm_predictions = svc_model.predict(x_test_pca)
 output = pd.DataFrame({ 'PassengerId' : testdf['PassengerId'], 'Survived': svm_predictions })
 output.to_csv('titanic-predictions-svm-pca.csv', index=False)
 output 
<set_options>
def load_data(dataframe=None, batch_size=16, class_mode='binary', target_size=(IMAGE_ROWS, IMAGE_COLS)) :
k_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255,
zoom_range=0.1,
validation_split=0.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 data_train = k_gen.flow_from_dataframe(dataframe,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=target_size,
class_mode=class_mode,
batch_size=batch_size,
shuffle=True,
subset="training")
 data_test = k_gen.flow_from_dataframe(dataframe,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=(target_size),
class_mode=class_mode,
batch_size=batch_size,
shuffle=True,
subset='validation')
 return data_train, data_test
warnings.simplefilter(action='ignore', category=FutureWarning )<load_from_csv>
train_k_gen, val_k_gen = load_data(dataframe=df,batch_size=BATCH_SIZE, target_size=(IMAGE_ROWS, IMAGE_COLS))
DF = 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" )<categorify>
history = model.fit_generator(train_k_gen,
steps_per_epoch=(train_k_gen.n//train_k_gen.batch_size),
epochs=EPOCHS,
validation_data=val_k_gen,
validation_steps=len(val_k_gen),
shuffle=True )
DF['LotFrontage']=DF.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
DF['GarageYrBlt']=DF.groupby('Neighborhood')['GarageYrBlt'].transform(lambda x: x.fillna(x.median()))
DF['MasVnrArea']=DF.groupby('Neighborhood')['MasVnrArea'].transform(lambda x: x.fillna(x.median()))<categorify>
import tqdm
from tqdm import tqdm, tqdm_notebook
Test['LotFrontage']=Test.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
Test['GarageYrBlt']=Test.groupby('Neighborhood')['GarageYrBlt'].transform(lambda x: x.fillna(x.median()))
Test['MasVnrArea']=Test.groupby('Neighborhood')['MasVnrArea'].transform(lambda x: x.fillna(x.median()))<feature_engineering>
def predict(model, sample_submission):
pred = np.empty(( sample_submission.shape[0],))
for n in tqdm(range(sample_submission.shape[0])) :
data = np.array(Image.open(TEST_DIR + sample_submission.id[n]))
pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)) /255)[0]
sample_submission['has_cactus'] = pred
return sample_submission
DF['cond*qual'] =(DF['OverallCond'] * DF['OverallQual'])/ 100.0
DF['home_age_when_sold'] = DF['YrSold'] - DF['YearBuilt']
DF['garage_age_when_sold'] = DF['YrSold'] - DF['GarageYrBlt']
DF['TotalSF'] = DF['TotalBsmtSF'] + DF['1stFlrSF'] + DF['2ndFlrSF']
DF['total_porch_area'] = DF['WoodDeckSF'] + DF['OpenPorchSF'] + DF['EnclosedPorch'] + DF['3SsnPorch'] + DF['ScreenPorch']
DF['Totalsqrfootage'] =(DF['BsmtFinSF1'] + DF['BsmtFinSF2'] + DF['1stFlrSF'] + DF['2ndFlrSF'])
 DF['Total_Bathrooms'] =(DF['FullBath'] +(0.5 * DF['HalfBath'])+ DF['BsmtFullBath'] +(0.5 * DF['BsmtHalfBath']))<feature_engineering>
sample_submission = pd.read_csv(MAIN_DIR + 'sample_submission.csv')
 df_prediction = predict(model, sample_submission)
 df_prediction.to_csv('submission_1.csv', index=False )