Kaggle competition: Leaf Classification
submission.to_csv("submission.csv", index=False )<save_to_csv>
df_train=pd.read_csv(".. /input/leaf-classification/train.csv.zip",index_col='id')
 df_train.shape
submission.to_csv("submission.csv", index=False )<import_modules>
df_train.isna().sum()
from glob import glob
import numpy as np
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras import layers<load_pretrained>
y_train=df_train.species
X_train=df_train.drop(columns=['species'],axis=1 )
with ZipFile('.. /input/aerial-cactus-identification/test.zip')as test_obj :
test_obj.extractall()
with ZipFile('.. /input/aerial-cactus-identification/train.zip')as train_obj :
train_obj.extractall()<load_from_csv>
object_cols = [cname for cname in X_train.columns if X_train[cname].nunique() < 10 and X_train[cname].dtype == "object"]
object_cols
df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 print(df.head())
 file_list = df['id']
has_cactus = df['has_cactus']
print(len(file_list), len(has_cactus))<load_from_csv>
len(y_train.value_counts() )
test_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 print(test_df.head())
 test_fnames = test_df['id']
test_labels = test_df['has_cactus']
print(len(test_fnames), len(test_labels))<define_variables>
encoder = LabelEncoder()
y_fit = encoder.fit(y_train)
 y_train = y_fit.transform(y_train)
 classes = list(y_fit.classes_ )
data_paths = glob('train/*.jpg')
 test_paths = glob('test/*.jpg')
 print(len(data_paths), len(test_paths))<train_model>
quantile_transformer = QuantileTransformer(random_state=0)
 scaler = quantile_transformer.fit(X_train)
 X_train= quantile_transformer.transform(X_train )
pa = glob('train/*.jpg')[0]
pa
g = tf.io.read_file(pa)
 im = tf.io.decode_image(g)
 print(im.shape)
 plt.imshow(im)
 plt.show()<define_variables>
parameters = {
'tol':[0.001, 0.009],
'C':list(range(100, 1000,100)) ,
'max_iter': list(range(10, 100, 10)) ,
"solver":("newton-cg", "lbfgs", "liblinear"),
"penalty":("l1", "l2")
 }
parameters
input_shape =(32, 32, 3)
 batch_size = 32<define_variables>
my_randome_state=500
data_dir = 'train'
data_paths = []
for fname, label in zip(file_list, has_cactus):
data_paths.append(( os.path.join(data_dir, fname), label))<categorify>
log_reg =LogisticRegression(multi_class='multinomial',
random_state=my_randome_state)
 gsearch = GridSearchCV(estimator=log_reg,
param_grid = parameters,
scoring="neg_log_loss",
n_jobs=4,cv=5, verbose=7 )
def tmp_func(path_name):
return path_name<categorify>
gsearch.fit(X_train, y_train )
def read_data(path_name):
img_path = path_name[0]
label = tf.strings.to_number(path_name[1], out_type=tf.int64)
 gfile = tf.io.read_file(img_path)
 image = tf.io.decode_image(gfile)
 return image, label<create_dataframe>
best_max_iter = gsearch.best_params_.get('max_iter')
 best_tol = gsearch.best_params_.get('tol')
 best_C = gsearch.best_params_.get('C')
 best_solver = gsearch.best_params_.get('solver')
 best_penalty = gsearch.best_params_.get('penalty')
 best_max_iter,best_tol,best_C,best_penalty,best_solver
a = tf.data.Dataset.from_tensor_slices(np.array(data_paths[:2]))
a = a.map(tmp_func)
 p = next(iter(a))
p[0], p[1]<split>
final_model = LogisticRegression(max_iter=best_max_iter,
random_state=my_randome_state,
tol=best_tol,
C=best_C,
solver=best_solver,
penalty=best_penalty )
train_ratio = 0.8
train_paths = data_paths[:int(train_ratio*len(data_paths)) ]
test_paths = data_paths[int(train_ratio*len(data_paths)) :]<define_variables>
final_model.fit(X_train, y_train )
train_ds = tf.data.Dataset.from_tensor_slices(np.array(train_paths))
train_ds = train_ds.map(read_data)
 train_ds = train_ds.shuffle(len(train_paths))
train_ds = train_ds.batch(batch_size)
 train_ds = train_ds.repeat()<define_variables>
X_test=pd.read_csv(".. /input/leaf-classification/test.csv.zip" )
valid_ds = tf.data.Dataset.from_tensor_slices(np.array(test_paths))
valid_ds = valid_ds.map(read_data)
 valid_ds = valid_ds.batch(batch_size)
 valid_ds = valid_ds.repeat()<choose_model_class>
test_ids = X_test.id
X_test = X_test.drop(['id'], axis =1 )
inputs = layers.Input(input_shape)
 net = layers.Conv2D(32, 3, 1, 'SAME' )(inputs)
 net = layers.Activation('relu' )(net)
 net = layers.Conv2D(32, 3, 1, 'SAME' )(net)
 net = layers.Activation('relu' )(net)
 net = layers.MaxPooling2D(( 2, 2))(net)
 net = layers.Dropout(0.5 )(net)
 net = layers.Conv2D(64, 3, 1, 'SAME' )(net)
 net = layers.Activation('relu' )(net)
 net = layers.Conv2D(64, 3, 1, 'SAME' )(net)
 net = layers.Activation('relu' )(net)
 net = layers.MaxPooling2D(( 2, 2))(net)
 net = layers.Dropout(0.5 )(net)
 net = layers.Flatten()(net)
 net = layers.Dense(512 )(net)
 net = layers.Activation('relu' )(net)
 net = layers.Dropout(0.5 )(net)
 net = layers.Dense(1 )(net)
 net = layers.Activation('sigmoid' )(net)
 model = tf.keras.Model(inputs=inputs, outputs=net, name='cactus_cnn' )<choose_model_class>
X_test = scaler.transform(X_test )
model.compile(loss = tf.keras.losses.binary_crossentropy,
optimizer = tf.keras.optimizers.Adam() ,
metrics=['accuracy'] )<define_variables>
final_predictions = final_model.predict_proba(X_test )
steps_per_epoch = len(train_paths)// batch_size
validation_steps = len(test_paths)// batch_size<train_model>
submission = pd.DataFrame(final_predictions, columns=classes)
 submission.insert(0, 'id', test_ids)
 submission
<define_variables><EOS> 
submission.to_csv('submission_log_reg.csv', index = False)
 print("done" )