{"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":"## WIKIMEDIA - Image/Caption Matching \n### We shall do a bit of EDA using various tools\n#### We shall use Autoviz , SweetViz and then do a bit of analysis using standard techniques","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-14T03:25:33.648795Z","iopub.execute_input":"2021-09-14T03:25:33.64972Z","iopub.status.idle":"2021-09-14T03:25:33.692444Z","shell.execute_reply.started":"2021-09-14T03:25:33.649614Z","shell.execute_reply":"2021-09-14T03:25:33.691813Z"}}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:06.998655Z","iopub.execute_input":"2021-09-15T05:23:06.999751Z","iopub.status.idle":"2021-09-15T05:23:07.051418Z","shell.execute_reply.started":"2021-09-15T05:23:06.999611Z","shell.execute_reply":"2021-09-15T05:23:07.050142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport requests\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport plotly.graph_objs as go\nimport plotly.express as px\nimport PIL.Image\nimport cv2\n\nfrom IPython.display import Image, display\n\nimport urllib\n\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-15T05:23:07.090809Z","iopub.execute_input":"2021-09-15T05:23:07.091128Z","iopub.status.idle":"2021-09-15T05:23:08.918629Z","shell.execute_reply.started":"2021-09-15T05:23:07.091097Z","shell.execute_reply":"2021-09-15T05:23:08.917370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### List the files","metadata":{}},{"cell_type":"code","source":"os.listdir('../input/wikipedia-image-caption/')","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-15T05:23:08.921236Z","iopub.execute_input":"2021-09-15T05:23:08.921680Z","iopub.status.idle":"2021-09-15T05:23:08.933557Z","shell.execute_reply.started":"2021-09-15T05:23:08.921633Z","shell.execute_reply":"2021-09-15T05:23:08.932331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the Main File","metadata":{}},{"cell_type":"code","source":"test_file = pd.read_csv('../input/wikipedia-image-caption/test.tsv', sep='\\t')\ntest_file","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:08.935511Z","iopub.execute_input":"2021-09-15T05:23:08.935810Z","iopub.status.idle":"2021-09-15T05:23:09.365849Z","shell.execute_reply.started":"2021-09-15T05:23:08.935781Z","shell.execute_reply":"2021-09-15T05:23:09.364660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wiki_df = pd.read_csv('../input/wikipedia-image-caption/image_data_test/image_pixels/test_image_pixels_part-00000.csv', \n                      sep='\\t', names=['image_url', 'b64_bytes', 'metadata_url'])\nprint(wiki_df)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:09.368814Z","iopub.execute_input":"2021-09-15T05:23:09.369154Z","iopub.status.idle":"2021-09-15T05:23:16.878842Z","shell.execute_reply.started":"2021-09-15T05:23:09.369114Z","shell.execute_reply":"2021-09-15T05:23:16.877691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the submission file","metadata":{}},{"cell_type":"code","source":"sub_file = pd.read_csv('../input/wikipedia-image-caption/sample_submission.csv')\nsub_file","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:16.880547Z","iopub.execute_input":"2021-09-15T05:23:16.881077Z","iopub.status.idle":"2021-09-15T05:23:17.399820Z","shell.execute_reply.started":"2021-09-15T05:23:16.881043Z","shell.execute_reply":"2021-09-15T05:23:17.398916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Check the size and shape","metadata":{}},{"cell_type":"code","source":"print(wiki_df.shape)\nprint(sub_file.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:17.401161Z","iopub.execute_input":"2021-09-15T05:23:17.401410Z","iopub.status.idle":"2021-09-15T05:23:17.406173Z","shell.execute_reply.started":"2021-09-15T05:23:17.401382Z","shell.execute_reply":"2021-09-15T05:23:17.405410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Install the libraries","metadata":{}},{"cell_type":"code","source":"!pip install autoviz xlrd","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-15T05:23:17.407655Z","iopub.execute_input":"2021-09-15T05:23:17.407871Z","iopub.status.idle":"2021-09-15T05:23:28.603699Z","shell.execute_reply.started":"2021-09-15T05:23:17.407847Z","shell.execute_reply":"2021-09-15T05:23:28.602503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the class","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom autoviz.AutoViz_Class import AutoViz_Class\nAV = AutoViz_Class()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-15T05:23:28.605033Z","iopub.execute_input":"2021-09-15T05:23:28.605768Z","iopub.status.idle":"2021-09-15T05:23:29.985033Z","shell.execute_reply.started":"2021-09-15T05:23:28.605736Z","shell.execute_reply":"2021-09-15T05:23:29.983815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"../input/wikipedia-image-caption/train-00001-of-00005.tsv\"\nsep = \"\\t\"\ndft = AV.AutoViz(\n    filename,\n    sep=sep,\n    depVar=\"\",\n    dfte=None,\n    header=0,\n    verbose=0,\n    lowess=False,\n    chart_format=\"svg\",\n    max_rows_analyzed=15000,\n    max_cols_analyzed=30,\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:29.986883Z","iopub.execute_input":"2021-09-15T05:23:29.987802Z","iopub.status.idle":"2021-09-15T05:23:29.996109Z","shell.execute_reply.started":"2021-09-15T05:23:29.987754Z","shell.execute_reply":"2021-09-15T05:23:29.994931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Try the EDA","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport squarify    # pip install squarify (algorithm for treemap)\n# plot it\nsquarify.plot(sizes=test_file['language'].value_counts().values, \n              label=test_file['language'].value_counts().index, \n              color=[\"green\",\"violet\",\"yellow\", \"blue\"],\n              alpha=.8 )\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:30.000157Z","iopub.execute_input":"2021-09-15T05:23:30.000905Z","iopub.status.idle":"2021-09-15T05:23:30.539577Z","shell.execute_reply.started":"2021-09-15T05:23:30.000854Z","shell.execute_reply":"2021-09-15T05:23:30.538801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Code inspired from by  Georgii Sirotenko  https://www.kaggle.com/georgiisirotenko/pytorch-fish-outliers-handling-test-100 & https://www.kaggle.com/mpwolke/wikimedia-urllib\n\nimport plotly.graph_objects as go    \n\nfig = go.Figure(\n    data=[ go.Bar(x=test_file['language'].value_counts().index, \n            y=test_file['language'].value_counts().values,\n            text=test_file['language'].value_counts().values,\n            textposition='auto',name='hist', marker_color='skyblue')],\n    layout_title_text=\"WikiMedia Image Dataset Language Distribution\"\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:30.541129Z","iopub.execute_input":"2021-09-15T05:23:30.542389Z","iopub.status.idle":"2021-09-15T05:23:30.679538Z","shell.execute_reply.started":"2021-09-15T05:23:30.542339Z","shell.execute_reply":"2021-09-15T05:23:30.678686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load one of the image files","metadata":{}},{"cell_type":"code","source":"image_file = pd.read_csv('../input/wikipedia-image-caption/image_data_test/image_pixels/test_image_pixels_part-00004.csv', \n                         sep='\\t', names=['image_url', 'b64_bytes', 'metadata_url'])\nimage_file","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:30.681089Z","iopub.execute_input":"2021-09-15T05:23:30.682266Z","iopub.status.idle":"2021-09-15T05:23:38.687033Z","shell.execute_reply.started":"2021-09-15T05:23:30.682218Z","shell.execute_reply":"2021-09-15T05:23:38.685877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define a function for loading images - 12 at a time","metadata":{}},{"cell_type":"code","source":"def showimages(imagelist):\n    f, ax = plt.subplots(4,3, figsize=(18,12))\n    image_flag=False\n    for i, image_id in enumerate(imagelist):\n        print(i, image_id)\n        with urllib.request.urlopen(image_id) as url:\n            if (image_id.lower().find('.svg') != -1):\n                print (\"Contains given SVG file \")\n                image_flag=True\n###         if (image_id.lower().find('.tiff') != -1):\n###                print (\"Contains given TIFF file \")\n###                image_flag=True \n###            if (image_id.lower().find('.tif') != -1):\n###                print (\"Contains given TIF file \")\n###                image_flag=True \n###\n            if (image_flag == False):\n                with open('./temp.jpg', 'wb') as f:\n                    f.write(url.read())\n        \n        if (image_flag == False):\n            imagetoshow=PIL.Image.open('./temp.jpg')\n            print(imagetoshow)\n            ax[i//3, i%3].imshow(imagetoshow) \n            ax[i//3, i%3].axis('off')\n    plt.show() ","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:38.688954Z","iopub.execute_input":"2021-09-15T05:23:38.689234Z","iopub.status.idle":"2021-09-15T05:23:38.699853Z","shell.execute_reply.started":"2021-09-15T05:23:38.689167Z","shell.execute_reply":"2021-09-15T05:23:38.699109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Select 12 files at a time","metadata":{}},{"cell_type":"code","source":"manualdisplay=image_file.image_url[90:102].values\nshowimages(manualdisplay)","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:38.701463Z","iopub.execute_input":"2021-09-15T05:23:38.702251Z","iopub.status.idle":"2021-09-15T05:23:55.846396Z","shell.execute_reply.started":"2021-09-15T05:23:38.702214Z","shell.execute_reply":"2021-09-15T05:23:55.845694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let us randomize","metadata":{}},{"cell_type":"code","source":"import random\n\nstart_num=random.randrange(0, len(image_file)-30)\nend_num = start_num + 12\nimagelist=image_file.image_url[start_num:end_num].values\nprint(imagelist.dtype)\nfor index, image in enumerate(imagelist):\n    if (image.find('.svg') != -1):\n        print (\"Contains given SVG file \")\n        imagelist[index] =  imagelist[index-1] #work to be done\n        \nshowimages(imagelist)","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:23:55.847505Z","iopub.execute_input":"2021-09-15T05:23:55.847839Z","iopub.status.idle":"2021-09-15T05:24:28.061046Z","shell.execute_reply.started":"2021-09-15T05:23:55.847813Z","shell.execute_reply":"2021-09-15T05:24:28.059799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Perhaps a word cloud?","metadata":{}},{"cell_type":"code","source":"file_name = pd.read_csv('../input/wikipedia-image-caption/train-00001-of-00005.tsv', \n                        sep='\\t',nrows=3000)\nfile_name.head(5)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-15T05:24:28.062775Z","iopub.execute_input":"2021-09-15T05:24:28.063065Z","iopub.status.idle":"2021-09-15T05:24:28.275417Z","shell.execute_reply.started":"2021-09-15T05:24:28.063031Z","shell.execute_reply":"2021-09-15T05:24:28.274335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator\nfrom PIL import Image\n\nkaggle_mask = np.array(Image.open('../input/kaggle/kaggle-logo.png'))\n#kaggle_mask = np.array(Image.open('../input/kaggle/kaggle-transparent.svg'))\nfig = plt.figure()\nfig.set_figwidth(10)\nfig.set_figheight(15)\nplt.imshow(kaggle_mask, cmap=plt.cm.gray, interpolation='bilinear') \nplt.axis('off')\n#plt.show()\n\nkaggle_wc= WordCloud(background_color='black',max_words = 3000,stopwords='site', mask = kaggle_mask)\nkaggle_wc.generate(\" \".join(file_name['page_title'].astype(str)))\nfig=plt.figure()\nfig.set_figwidth(20)\nfig.set_figheight(16)\nplt.axis('off')\nplt.imshow(kaggle_wc, interpolation='bilinear')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:27:57.198492Z","iopub.execute_input":"2021-09-15T05:27:57.198731Z","iopub.status.idle":"2021-09-15T05:30:17.139759Z","shell.execute_reply.started":"2021-09-15T05:27:57.198705Z","shell.execute_reply":"2021-09-15T05:30:17.138406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embed_file_sample_df=pd.read_csv('../input/wikipedia-image-caption/image_data_test/resnet_embeddings/test_resnet_embeddings_part-00001.csv')\npixel_file_sample_df=pd.read_csv('../input/wikipedia-image-caption/image_data_test/image_pixels/test_image_pixels_part-00002.csv')\n\nprint(embed_file_sample_df.head(2))\nprint(embed_file_sample_df.columns)\n\nprint(pixel_file_sample_df.head(2))\nprint(pixel_file_sample_df.columns)\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-15T05:30:17.141393Z","iopub.execute_input":"2021-09-15T05:30:17.141657Z","iopub.status.idle":"2021-09-15T05:30:30.428497Z","shell.execute_reply.started":"2021-09-15T05:30:17.141629Z","shell.execute_reply":"2021-09-15T05:30:30.427487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name.columns","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:35:58.771322Z","iopub.execute_input":"2021-09-15T05:35:58.771695Z","iopub.status.idle":"2021-09-15T05:35:58.779725Z","shell.execute_reply.started":"2021-09-15T05:35:58.771661Z","shell.execute_reply":"2021-09-15T05:35:58.778473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check_cols = ['language', 'mime_type', 'original_height', \n              'original_width', 'is_main_image','page_changed_recently']\nfor cols in check_cols:\n    print(file_name[cols].unique())","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:38:14.001874Z","iopub.execute_input":"2021-09-15T05:38:14.002207Z","iopub.status.idle":"2021-09-15T05:38:14.013518Z","shell.execute_reply.started":"2021-09-15T05:38:14.002162Z","shell.execute_reply":"2021-09-15T05:38:14.012364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#temp_df=file_name[[check_cols]]\ntemp_df1= file_name.iloc[:, 0]\ntemp_df2= file_name.iloc[:, 9:14]\ntemp_df3=pd.concat([temp_df1, temp_df2.reindex(temp_df2.index)], axis=1)\n#,file_name.iloc[:,9:12])\ntemp_df3.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-15T05:48:48.525358Z","iopub.execute_input":"2021-09-15T05:48:48.525675Z","iopub.status.idle":"2021-09-15T05:48:48.543345Z","shell.execute_reply.started":"2021-09-15T05:48:48.525643Z","shell.execute_reply":"2021-09-15T05:48:48.542325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n#temp_df3.boxplot(by='language')\ntemp_df3_group=temp_df3.groupby('language').agg('min')\n\n#temp_df3_group.columns\ntemp_df3_group['original_height'].plot(label = 'original_height', figsize = (20,16))\ntemp_df3_group['original_width'].plot(label = 'original_width', figsize = (20,16))\nplt.legend()\nplt.show()\ntemp_df3_group['mime_type'].value_counts().plot.bar(label='mime_type')\nplt.legend()\nplt.show()\ntemp_df3_group['is_main_image'].value_counts().plot.bar(label='is_main_image')\nplt.legend()\nplt.show()\ntemp_df3_group['attribution_passes_lang_id'].value_counts().plot.bar(label='attribution_passes_lang_id')\nplt.legend()\nplt.show()\n\ngraph_df = pd.concat([temp_df3_group['mime_type'].value_counts(), \n                temp_df3_group['is_main_image'].value_counts(),\n                temp_df3_group['attribution_passes_lang_id'].value_counts()], \n               axis=1, sort=True)\ngraph_df.columns = [\"Mime\", \"Main Image\", \"Attribution Passes\"]\ngraph_df.plot.bar(figsize = (20,16))\nplt.legend()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-15T06:15:42.285875Z","iopub.execute_input":"2021-09-15T06:15:42.286871Z","iopub.status.idle":"2021-09-15T06:15:44.717532Z","shell.execute_reply.started":"2021-09-15T06:15:42.286822Z","shell.execute_reply":"2021-09-15T06:15:44.716548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# More exploration to come\n\n### Thank you! ","metadata":{}}]}