{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## PET FINDER / EDA + Regression on CatVsDog augmented dataset","metadata":{"_uuid":"40733c39647bac5056ba0dada8409d6184fefb68"}},{"cell_type":"markdown","source":"The aim of this notebook is to explore data and exploit metadata. We will in particular try to add to the meta-data an information concerning the type of animal (Cat or dog) in order to see if this proves to be valuable in the context of a regression.\n\n","metadata":{"_uuid":"0182259264d23ccfb3c27d530ed10fb4ba7a35da"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n%matplotlib inline \n\nimport cv2\n\nimport os\n\nTRAIN_DIR = '../input/petfinder-pawpularity-score/train'\nTEST_DIR = '../input/petfinder-pawpularity-score/test'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-10T05:51:01.421253Z","iopub.execute_input":"2021-11-10T05:51:01.421577Z","iopub.status.idle":"2021-11-10T05:51:01.646153Z","shell.execute_reply.started":"2021-11-10T05:51:01.421519Z","shell.execute_reply":"2021-11-10T05:51:01.645354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **1. First approach to data**","metadata":{}},{"cell_type":"markdown","source":"First, let's load the Ids as well as the associated metadata.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/petfinder-pawpularity-score/train.csv\")\ntest = pd.read_csv(\"../input/petfinder-pawpularity-score//test.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:05.209882Z","iopub.execute_input":"2021-11-10T05:51:05.210152Z","iopub.status.idle":"2021-11-10T05:51:05.296625Z","shell.execute_reply.started":"2021-11-10T05:51:05.210102Z","shell.execute_reply":"2021-11-10T05:51:05.295995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Then let's edit some photos :","metadata":{}},{"cell_type":"code","source":"def displaySample(attribute):\n    \n    f, ax = plt.subplots(1, 2, figsize = (15, 5))\n\n    dfSample0 = train[train[attribute]==0].sample()\n    dfSample1 = train[train[attribute]==1].sample()\n\n    imgBGR = cv2.imread(TRAIN_DIR + '/' + dfSample0.iloc[0]['Id'] + '.jpg')\n    imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB)\n    ax[0].imshow(imgRGB)\n    ax[0].axis('off')\n    ax[0].set_title(attribute+\" 0\")   \n\n    imgBGR = cv2.imread(TRAIN_DIR + '/' + dfSample1.iloc[0]['Id'] + '.jpg')\n    imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB)\n    ax[1].imshow(imgRGB)\n    ax[1].axis('off')\n    ax[1].set_title(attribute+\" 1\")  \n        \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:08.400195Z","iopub.execute_input":"2021-11-10T05:51:08.400818Z","iopub.status.idle":"2021-11-10T05:51:08.412632Z","shell.execute_reply.started":"2021-11-10T05:51:08.400759Z","shell.execute_reply":"2021-11-10T05:51:08.411628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Subject Focus\" values\n#-------------------------------------------------\ndisplaySample('Subject Focus')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:11.399808Z","iopub.execute_input":"2021-11-10T05:51:11.400091Z","iopub.status.idle":"2021-11-10T05:51:11.771990Z","shell.execute_reply.started":"2021-11-10T05:51:11.400040Z","shell.execute_reply":"2021-11-10T05:51:11.771269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Eyes\" values\n#-------------------------------------------------\ndisplaySample('Eyes')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:14.910308Z","iopub.execute_input":"2021-11-10T05:51:14.910583Z","iopub.status.idle":"2021-11-10T05:51:15.266643Z","shell.execute_reply.started":"2021-11-10T05:51:14.910534Z","shell.execute_reply":"2021-11-10T05:51:15.264935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Face\" values\n#-------------------------------------------------\ndisplaySample('Face')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:18.279968Z","iopub.execute_input":"2021-11-10T05:51:18.280282Z","iopub.status.idle":"2021-11-10T05:51:18.701581Z","shell.execute_reply.started":"2021-11-10T05:51:18.280217Z","shell.execute_reply":"2021-11-10T05:51:18.700193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Near\" values\n#-------------------------------------------------\ndisplaySample('Near')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:21.985630Z","iopub.execute_input":"2021-11-10T05:51:21.985912Z","iopub.status.idle":"2021-11-10T05:51:22.427790Z","shell.execute_reply.started":"2021-11-10T05:51:21.985862Z","shell.execute_reply":"2021-11-10T05:51:22.427154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"action\" values\n#-------------------------------------------------\ndisplaySample('Action')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:25.575248Z","iopub.execute_input":"2021-11-10T05:51:25.575637Z","iopub.status.idle":"2021-11-10T05:51:25.925056Z","shell.execute_reply.started":"2021-11-10T05:51:25.575559Z","shell.execute_reply":"2021-11-10T05:51:25.924381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Accessory\" values\n#-------------------------------------------------\ndisplaySample('Accessory')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:28.980122Z","iopub.execute_input":"2021-11-10T05:51:28.980428Z","iopub.status.idle":"2021-11-10T05:51:29.385725Z","shell.execute_reply.started":"2021-11-10T05:51:28.980375Z","shell.execute_reply":"2021-11-10T05:51:29.385034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Group\" values\n#-------------------------------------------------\ndisplaySample('Group')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:33.419838Z","iopub.execute_input":"2021-11-10T05:51:33.420116Z","iopub.status.idle":"2021-11-10T05:51:33.777270Z","shell.execute_reply.started":"2021-11-10T05:51:33.420065Z","shell.execute_reply":"2021-11-10T05:51:33.776559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"collage\" values\n#-------------------------------------------------\ndisplaySample('Collage')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:36.855312Z","iopub.execute_input":"2021-11-10T05:51:36.855595Z","iopub.status.idle":"2021-11-10T05:51:37.204071Z","shell.execute_reply.started":"2021-11-10T05:51:36.855545Z","shell.execute_reply":"2021-11-10T05:51:37.203338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Human\" values\n#-------------------------------------------------\ndisplaySample('Human')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:44:08.766677Z","iopub.execute_input":"2021-11-10T04:44:08.767039Z","iopub.status.idle":"2021-11-10T04:44:09.176081Z","shell.execute_reply.started":"2021-11-10T04:44:08.766986Z","shell.execute_reply":"2021-11-10T04:44:09.174748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Occlusion\" values\n#-------------------------------------------------\ndisplaySample('Occlusion')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:51.010878Z","iopub.execute_input":"2021-11-10T05:51:51.011159Z","iopub.status.idle":"2021-11-10T05:51:51.362970Z","shell.execute_reply.started":"2021-11-10T05:51:51.011105Z","shell.execute_reply":"2021-11-10T05:51:51.362316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Info\" values\n#-------------------------------------------------\ndisplaySample('Info')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:54.230153Z","iopub.execute_input":"2021-11-10T05:51:54.230466Z","iopub.status.idle":"2021-11-10T05:51:54.615164Z","shell.execute_reply.started":"2021-11-10T05:51:54.230415Z","shell.execute_reply":"2021-11-10T05:51:54.614484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comparison of differents \"Blur\" values\n#-------------------------------------------------\ndisplaySample('Blur')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:51:57.659944Z","iopub.execute_input":"2021-11-10T05:51:57.660257Z","iopub.status.idle":"2021-11-10T05:51:57.958095Z","shell.execute_reply.started":"2021-11-10T05:51:57.660193Z","shell.execute_reply":"2021-11-10T05:51:57.957084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have cats and dogs here. In addition the meta data are quite complete, however we notice that it lacks the presence of the most discriminating factor which is the type of animal (cat or dog). A neural network will undoubtedly be able to accommodate, however it may be interesting to add this information as part of our analysis.","metadata":{}},{"cell_type":"markdown","source":"## **2. Quick labeling: cat or dog**","metadata":{}},{"cell_type":"markdown","source":"We will quickly add the cat or dog information to the dataset. This is a classic, so we will not go into the details of CNN learning process, we will go directly in this notebook to the prediction phase. Nevertheless we can specify that we will use a Resnet50 architecture whose precision has been evaluated at 0.98","metadata":{}},{"cell_type":"markdown","source":"#### Definition of some constants","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"}},{"cell_type":"code","source":"# Fixed for our Cats & Dogs classes\nNUM_CLASSES = 2\n\n# Fixed for Cats & Dogs color images\nCHANNELS = 3\n\nIMAGE_RESIZE = 224\nRESNET50_POOLING_AVERAGE = 'avg'\nDENSE_LAYER_ACTIVATION = 'softmax'\nOBJECTIVE_FUNCTION = 'categorical_crossentropy'\n\n# Common accuracy metric for all outputs, but can use different metrics for different output\nLOSS_METRICS = ['accuracy']\n\n# Using 1 to easily manage mapping between test_generator & prediction for submission preparation\nBATCH_SIZE_TESTING = 1","metadata":{"_uuid":"d157dc1311928d1ada13ed2ef4a34c4ea5c0b538","execution":{"iopub.status.busy":"2021-11-10T05:52:21.261887Z","iopub.execute_input":"2021-11-10T05:52:21.262201Z","iopub.status.idle":"2021-11-10T05:52:21.267155Z","shell.execute_reply.started":"2021-11-10T05:52:21.262129Z","shell.execute_reply":"2021-11-10T05:52:21.266346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Import of libraries and definition of paths","metadata":{}},{"cell_type":"code","source":"from tensorflow.python.keras.applications import ResNet50\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense\nfrom tensorflow.python.keras import optimizers\n\nresnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\nresnet_bestwgt_path = '../input/catvsdogweights/best.hdf5'","metadata":{"_uuid":"ee0da10690e2848537102cb0c74f9fe19a50431b","execution":{"iopub.status.busy":"2021-11-10T05:52:24.210328Z","iopub.execute_input":"2021-11-10T05:52:24.210608Z","iopub.status.idle":"2021-11-10T05:52:24.215638Z","shell.execute_reply.started":"2021-11-10T05:52:24.210554Z","shell.execute_reply":"2021-11-10T05:52:24.214491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Definition de l'architecture du modele, compilation et chargement des poids","metadata":{"_uuid":"4ebad39fff164624f5616a83cd8831308431da7d"}},{"cell_type":"code","source":"model = Sequential()\n\n# 1st layer as the lumpsum weights from resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\n# NOTE that this layer will be set below as NOT TRAINABLE, i.e., use it as is\nmodel.add(ResNet50(include_top = False, pooling = RESNET50_POOLING_AVERAGE, weights = resnet_weights_path))\n\n# 2nd layer as Dense for 2-class classification, i.e., dog or cat using SoftMax activation\nmodel.add(Dense(NUM_CLASSES, activation = DENSE_LAYER_ACTIVATION))\n\n# Say not to train first layer (ResNet) model as it is already trained\nmodel.layers[0].trainable = False\n\n# Compilation\nsgd = optimizers.SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)\nmodel.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)\n\n# Weights loading\nmodel.load_weights(resnet_bestwgt_path)\nmodel.summary()","metadata":{"_uuid":"0bb4a8eccd70874ef21f0809f478f993fa127fb2","execution":{"iopub.status.busy":"2021-11-10T05:52:26.650192Z","iopub.execute_input":"2021-11-10T05:52:26.650472Z","iopub.status.idle":"2021-11-10T05:52:42.445011Z","shell.execute_reply.started":"2021-11-10T05:52:26.650421Z","shell.execute_reply":"2021-11-10T05:52:42.441911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Generator preparation","metadata":{"_uuid":"8d003a99fe4ff58b8905c7f6b5286d97a852cb7a"}},{"cell_type":"code","source":"from keras.applications.resnet50 import preprocess_input\nfrom keras.preprocessing.image import ImageDataGenerator\n\nimage_size = IMAGE_RESIZE\n\n# preprocessing_function is applied on each image but only after re-sizing & augmentation (resize => augment => pre-process)\n# Each of the keras.application.resnet* preprocess_input MOSTLY mean BATCH NORMALIZATION (applied on each batch) stabilize the inputs to nonlinear activation functions\n# Batch Normalization helps in faster convergence\ndata_generator = ImageDataGenerator(preprocessing_function=preprocess_input)\n\ntest_generator = data_generator.flow_from_directory(\n    directory = '../input/petfinder-pawpularity-score',\n    target_size = (image_size, image_size),\n    batch_size = BATCH_SIZE_TESTING,\n    class_mode = None,\n    shuffle = False,\n    seed = 123\n)\n","metadata":{"_uuid":"ac9ebd909fee81c8c8d9b9fccb6590944d8106eb","execution":{"iopub.status.busy":"2021-11-10T05:52:47.949977Z","iopub.execute_input":"2021-11-10T05:52:47.950446Z","iopub.status.idle":"2021-11-10T05:52:53.685117Z","shell.execute_reply.started":"2021-11-10T05:52:47.950352Z","shell.execute_reply":"2021-11-10T05:52:53.684406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Predictions","metadata":{"_uuid":"8c6e7f35552f87b522aa32397ffb71ea732ce80b","trusted":true}},{"cell_type":"code","source":"# Reset before each call to predict\ntest_generator.reset()\npred = model.predict_generator(test_generator, steps = len(test_generator), verbose = 1)\npredicted_class_indices = np.argmax(pred, axis = 1)","metadata":{"_uuid":"9911ff38ec4b82f5282f078aae63b2a510a9a6cc","execution":{"iopub.status.busy":"2021-11-10T05:53:01.535984Z","iopub.execute_input":"2021-11-10T05:53:01.536288Z","iopub.status.idle":"2021-11-10T05:56:35.268199Z","shell.execute_reply.started":"2021-11-10T05:53:01.536216Z","shell.execute_reply":"2021-11-10T05:56:35.267541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Formatting labels","metadata":{}},{"cell_type":"code","source":"results_df = pd.DataFrame(\n    {\n        'id': pd.Series(test_generator.filenames), \n        'Dog': pd.Series(predicted_class_indices)\n    })\n\nresults_df[['set', 'Id']] = results_df['id'].str.split('/', expand=True)\nresults_df = results_df.drop(['id'], axis=1)\nresults_df['Id'] = results_df['Id'].map(lambda x: x.rstrip('.jpg'))\nresults_df.head()","metadata":{"_uuid":"39bac850385ba7129c97c1c0d389b36e7f2f0dfe","execution":{"iopub.status.busy":"2021-11-10T05:56:54.750517Z","iopub.execute_input":"2021-11-10T05:56:54.750792Z","iopub.status.idle":"2021-11-10T05:56:54.810901Z","shell.execute_reply.started":"2021-11-10T05:56:54.750741Z","shell.execute_reply":"2021-11-10T05:56:54.810145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Let's visualize some predicted labels","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(3, 3, figsize = (10, 10))\n\ni = 0\nfor index, row in results_df[results_df['set']=='train'].sample(n=9).iterrows():\n    imgBGR = cv2.imread(TRAIN_DIR + '/' + row['Id'] + '.jpg')\n    imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB)\n    \n    # a if condition else b\n    predicted_class = \"Dog\" if row['Dog'] else \"Cat\"\n\n    ax[i//3, i%3].imshow(imgRGB)\n    ax[i//3, i%3].axis('off')\n    ax[i//3, i%3].set_title(\"Predicted:{}\".format(predicted_class))    \n    i += 1\n    \nplt.show()","metadata":{"_uuid":"e54bc22bd1a2af7d704f9164b624b79b8cbd4f56","execution":{"iopub.status.busy":"2021-11-10T05:58:37.020861Z","iopub.execute_input":"2021-11-10T05:58:37.021374Z","iopub.status.idle":"2021-11-10T05:58:37.891586Z","shell.execute_reply.started":"2021-11-10T05:58:37.021140Z","shell.execute_reply":"2021-11-10T05:58:37.890680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We now have to merge obtained labels with original metadata.","metadata":{}},{"cell_type":"code","source":"merged_train = pd.merge(train, results_df, on=\"Id\")\nmerged_test = pd.merge(test, results_df, on=\"Id\")\n\nmerged_train = merged_train.drop(['set'], axis=1)\nmerged_test = merged_test.drop(['set'], axis=1)\n\nmerged_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:58:46.349653Z","iopub.execute_input":"2021-11-10T05:58:46.349938Z","iopub.status.idle":"2021-11-10T05:58:46.397910Z","shell.execute_reply.started":"2021-11-10T05:58:46.349886Z","shell.execute_reply":"2021-11-10T05:58:46.397154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3. Exploratory Data Analysis**","metadata":{}},{"cell_type":"markdown","source":"### 3.1 Distributions","metadata":{}},{"cell_type":"code","source":"statDf = pd.concat([merged_train[['Pawpularity']].describe(), \n           merged_train[merged_train['Dog']==1]['Pawpularity'].describe(),\n           merged_train[merged_train['Dog']==0]['Pawpularity'].describe()], \n           axis=1)\nstatDf.columns = ['All', 'Dog', 'Cat']\nstatDf","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:13.390892Z","iopub.execute_input":"2021-11-10T05:59:13.391208Z","iopub.status.idle":"2021-11-10T05:59:13.427025Z","shell.execute_reply.started":"2021-11-10T05:59:13.391133Z","shell.execute_reply":"2021-11-10T05:59:13.426230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We notice that cats score lower on average than dogs. The standard deviation is also lower for cats, which confirms our diagnosis.","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nax = sns.boxplot(x=\"Dog\", y=\"Pawpularity\", data=merged_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:17.140638Z","iopub.execute_input":"2021-11-10T05:59:17.141162Z","iopub.status.idle":"2021-11-10T05:59:18.239648Z","shell.execute_reply.started":"2021-11-10T05:59:17.140929Z","shell.execute_reply":"2021-11-10T05:59:18.238772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The analysis of dispersion compared to the median confirms different profiles between cats (left) and dogs (right). Although cats also achieve high scores, these constitute a longer outliers' tail than dogs. The cats boxplot is also tighter around the median, showing some homogeneity in the notations.","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 2, figsize = (15, 5))\nax[0].hist(merged_train[merged_train['Dog']==0]['Pawpularity'], color='blue')\nax[0].set_title('Cat scores distribution')\nax[1].hist(merged_train[merged_train['Dog']==1]['Pawpularity'], color='orange')\nax[1].set_title('Dog scores distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:20.989973Z","iopub.execute_input":"2021-11-10T05:59:20.990324Z","iopub.status.idle":"2021-11-10T05:59:21.328909Z","shell.execute_reply.started":"2021-11-10T05:59:20.990256Z","shell.execute_reply":"2021-11-10T05:59:21.328016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The distributions confirm a slightly more spread out notation for the dogs. Nevertheless the two profiles are similar with a concentration of the scores in the mean values.","metadata":{}},{"cell_type":"markdown","source":"### 3.2 Correlation between features","metadata":{}},{"cell_type":"code","source":"corrDF = merged_train.drop(['Id'], axis=1)\nsns.heatmap(corrDF.corr());","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:24.709792Z","iopub.execute_input":"2021-11-10T05:59:24.710094Z","iopub.status.idle":"2021-11-10T05:59:25.136072Z","shell.execute_reply.started":"2021-11-10T05:59:24.710037Z","shell.execute_reply":"2021-11-10T05:59:25.135089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There seems to be little lesson to be learned from this correlation matrix.\nNevertheless we can note several points:\n- an animal whose face is visible also has its eyes visible (which seems logical)\n- a collage is often accompanied by text\n- the occlusion and the presence of a human in the image are correlated (which is also logical)\n- finally, no variable seems to have an obvious linear correlation link with the target popularity score","metadata":{}},{"cell_type":"markdown","source":"## **4. Regression**","metadata":{}},{"cell_type":"markdown","source":"We are now going to try a regression on the metadata with elastic Net.","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import ElasticNet\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:31.640514Z","iopub.execute_input":"2021-11-10T05:59:31.640801Z","iopub.status.idle":"2021-11-10T05:59:31.897732Z","shell.execute_reply.started":"2021-11-10T05:59:31.640742Z","shell.execute_reply":"2021-11-10T05:59:31.897033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's standardize the target\nmerged_train['Pawpularity'] = (merged_train['Pawpularity']-min(merged_train['Pawpularity']))/(max(merged_train['Pawpularity'])-min(merged_train['Pawpularity']))\nmerged_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:35.950817Z","iopub.execute_input":"2021-11-10T05:59:35.951091Z","iopub.status.idle":"2021-11-10T05:59:35.988825Z","shell.execute_reply.started":"2021-11-10T05:59:35.951041Z","shell.execute_reply":"2021-11-10T05:59:35.988233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = merged_train.drop(['Id', 'Pawpularity'], axis=1)\ny = merged_train['Pawpularity']","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:39.575299Z","iopub.execute_input":"2021-11-10T05:59:39.575600Z","iopub.status.idle":"2021-11-10T05:59:39.584199Z","shell.execute_reply.started":"2021-11-10T05:59:39.575549Z","shell.execute_reply":"2021-11-10T05:59:39.580637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.001)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:42.400630Z","iopub.execute_input":"2021-11-10T05:59:42.400919Z","iopub.status.idle":"2021-11-10T05:59:42.409341Z","shell.execute_reply.started":"2021-11-10T05:59:42.400866Z","shell.execute_reply":"2021-11-10T05:59:42.408437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reg1 = ElasticNet(alpha=0.7, l1_ratio=0.2)\nreg1.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:48.689812Z","iopub.execute_input":"2021-11-10T05:59:48.690088Z","iopub.status.idle":"2021-11-10T05:59:48.740735Z","shell.execute_reply.started":"2021-11-10T05:59:48.690037Z","shell.execute_reply":"2021-11-10T05:59:48.739963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RMSE evaluation","metadata":{}},{"cell_type":"code","source":"y_pre = reg1.predict(x_test)\ntest_pre = reg1.predict(merged_test.iloc[:,1:])\nmse = mean_squared_error(y_test,y_pre)\nrmse = np.sqrt(mse)\nrmse","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:52.260061Z","iopub.execute_input":"2021-11-10T05:59:52.260389Z","iopub.status.idle":"2021-11-10T05:59:52.271731Z","shell.execute_reply.started":"2021-11-10T05:59:52.260328Z","shell.execute_reply":"2021-11-10T05:59:52.271129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We finally return the score to its original scale and then create the submission file.","metadata":{}},{"cell_type":"code","source":"merged_test['Pawpularity'] = (test_pre*(100-1))+1\nsubmission = merged_test.drop(columns=['Subject Focus',\n                                       'Eyes','Face','Action','Accessory','Group','Collage','Human',\n                                       'Occlusion','Info','Blur','Near', 'Dog'],axis=0)\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:59:58.060638Z","iopub.execute_input":"2021-11-10T05:59:58.060942Z","iopub.status.idle":"2021-11-10T05:59:58.250989Z","shell.execute_reply.started":"2021-11-10T05:59:58.060891Z","shell.execute_reply":"2021-11-10T05:59:58.250310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5. Conclusion**","metadata":{}},{"cell_type":"markdown","source":"It is of course always possible to do better, but the metadata does not seem to be sufficiently discriminating to be used on its own in the prediction of the popularity score.","metadata":{}}]}