Kaggle competition: SYDE 522 (Winter 2020)
<SOS> metric: categorizationaccuracy Kaggle data source: syde-522-(winter-2020)<set_options>
import numpy as np
import pandas as pd
plt.rcParams['figure.figsize'] = 15,8
sns.set(rc={'figure.figsize':(15,8)})
 pd.options.display.float_format = '{:.2f}'.format
warnings.filterwarnings('ignore')
 gc.enable()<load_from_csv>
train_data = pd.read_csv('/kaggle/input/syde522/train.csv' )
train = pd.read_csv('.. /input/train_V2.csv')
 test = pd.read_csv('.. /input/test_V2.csv')
 train.head()<count_missing_values>
CNNmodel = VGG16(weights=None,input_shape=(150, 150, 3), include_top=False, pooling='max',)
 CNNmodel.load_weights('/kaggle/input/vgg16-weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')
 img_path = '/kaggle/input/syde522/train/train/0011.png'
img = image.load_img(img_path, target_size=(150,150))
def getFeatures(img):
x = image.img_to_array(img)
 x = np.expand_dims(x, axis=0)
 x = preprocess_input(x)
 featur = CNNmodel.predict(x)
 return featur.reshape(1, 512)
display(train[train.isnull().any(1)])
 display(test[test.isnull().any(1)] )<drop_column>
features=np.zeros(( 1,512))
labels=np.array('init')
 for idx,(_, entry)in enumerate(train_data.iterrows()):
img=imread(entry.train_file)
 feature = getFeatures(img)
 features=np.append(features,feature,axis=0)
 labels=np.append(labels,entry.Category)
 labels=labels.reshape(features[:,1].size)
 features=np.delete(features, 0, 0)
 labels=np.delete(labels, 0, 0)
 print(features.shape)
 print(labels.shape)
 features =(preprocessing.normalize(features))
neigh = KNeighborsClassifier(n_neighbors=7)
 neigh.fit(features, labels)
train.drop(2744604, inplace=True )<train_model>
def predictor(imgID):
img=imread('/kaggle/input/syde522/test/test/'+imgID)
 feature = getFeatures(img)
 feature = preprocessing.normalize(feature)
 prediction=neigh.predict(feature)
 return prediction[0]
test_files = glob.glob('/kaggle/input/syde522/test/test/*.png')
 test_file_id = [os.path.basename(test_file)for test_file in test_files]
test_submission = pd.DataFrame({'Id': test_file_id, 'Category': [predictor(test_img_id)for test_img_id in test_file_id]})
 test_submission.to_csv('submission.csv', index=False )