{"cells":[{"metadata":{"trusted":true,"_uuid":"027a11aaf6634ae5bc7100b913644f993dc500a7","collapsed":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport seaborn as sns\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\ndf = pd.read_csv(\n    '../input/tuning_labels.csv', header=None, names=['img_id', 'labels'])\ndescription = pd.read_csv('../input/class-descriptions.csv')\n\nlabel_lookup = {label: descr for label, descr in zip(\n    description.label_code.values, description.description.values)}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eec3a665f19f6ebdff8e7a20d2b2d14d096a1135","collapsed":true},"cell_type":"code","source":"import csv\n\nlabel_count = {}\nwith open('../input/tuning_labels.csv') as f:\n    csv_rdr = csv.reader(f)\n    for row in csv_rdr:\n        inst_labels = row[1].split()\n        label_count = {**label_count, **{l: label_count.get(l, 0) + 1 for l in inst_labels}}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"541e079c90e4c129c4131237bd86258e730c3b33"},"cell_type":"code","source":"top_label_name, top_label_count = zip(*[\n    l for l in sorted(label_count.items(), reverse=True, key=lambda l: l[1])[:100]])\nlabel_count_df = pd.DataFrame(data={'label': list(map(label_lookup.get, top_label_name)), 'count': top_label_count})\nsns.set(rc={'figure.figsize':(12, 18)})\nax = sns.barplot(data=label_count_df, y='label', x='count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84fe44e5aa9327a8b723233de92d60c4d9b75df2","collapsed":true},"cell_type":"code","source":"label_idx = {l: i for i, l in enumerate(label_count)}\ncooccurrence = np.zeros((len(label_idx), len(label_idx)))\nwith open('../input/tuning_labels.csv') as f:\n    csv_rdr = csv.reader(f)\n    for row in csv_rdr:\n        inst_labels = row[1].split()\n        cooccurrence[list(zip(*[(label_idx[l1], label_idx[l2]) for l1 in inst_labels for l2 in inst_labels]))] += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ab5514a9a80eaac2229bab228f6bc90bae0ce83"},"cell_type":"code","source":"labels, _ = zip(*[\n    l for l in sorted(label_count.items(), reverse=True, key=lambda l: l[1])])\n\nmost_freq_labels = labels[:30]\nmost_freq_label_name = [label_lookup[l] for l in most_freq_labels]\nmost_freq_label_idx = [label_idx[l] for l in most_freq_labels]\nmost_freq_label_cooccurrence = cooccurrence[:, most_freq_label_idx]\n\nmost_freq_label_corr = pd.DataFrame(data=most_freq_label_cooccurrence, columns=most_freq_label_name).corr()\n\nless_freq_labels = labels[-30:]\nless_freq_label_name = [label_lookup[l] for l in less_freq_labels]\nless_freq_label_idx = [label_idx[l] for l in less_freq_labels]\nless_freq_label_cooccurrence = cooccurrence[:, less_freq_label_idx]\n\nless_freq_label_corr = pd.DataFrame(data=less_freq_label_cooccurrence, columns=less_freq_label_name).corr()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d693c427f11d53aa46710edcc15f1a10fa70761d"},"cell_type":"code","source":"def plot_corr(corr):\n    sns.set(style=\"white\")\n    mask = np.zeros_like(corr, dtype=np.bool)\n    mask[np.triu_indices_from(mask)] = True\n    cmap = sns.diverging_palette(220, 10, as_cmap=True)\n    plt.figure(figsize = (12, 12))\n    return sns.heatmap(\n        corr, mask=mask, cmap=cmap, vmax=.3, center=0, \n        square=True, linewidths=.5, cbar_kws={\"shrink\": .5})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5335b7cf495afdb0c5801100ed9392fad84a12b6"},"cell_type":"code","source":"plot_corr(most_freq_label_corr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07f6b4521c8b5c660616f18a64494a848224f937"},"cell_type":"code","source":"plot_corr(less_freq_label_corr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"28e7d7ebd615cfc04901006d86b54c6928cda5ba"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8cbb7cf8f3dfaf5cbe2e7f8b794e61bda571ec0d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d1415d8f9cbdb62c42394b7a919f03290b6b15f2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"58c354c73337b400bc443782671a3050a6ee5593"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"bb82bd32350c4570d23c448f30e5cda1e7109fb2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}