{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "23ee4573-8bc4-f16f-82b5-c7f1793cf3e9"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import networkx as nx\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6508e302-ce36-a307-04f9-35f9380568f4"
      },
      "outputs": [],
      "source": [
        "df = pd.read_csv(\"../input/train.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6fc07e1a-9e5c-6ea2-e94b-1e985004550a"
      },
      "outputs": [],
      "source": [
        "df.head(3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4d5355ef-576d-4aa9-5123-470b5ca9c149"
      },
      "outputs": [],
      "source": [
        "df_duplicate = df[df.is_duplicate == 1]\n",
        "df_unduplicate = df[df.is_duplicate == 0]\n",
        "duplicated_edges = df_duplicate[[\"qid1\", \"qid2\"]].values\n",
        "unduplicated_edges = df_unduplicate[[\"qid1\", \"qid2\"]].values\n",
        "duplicated_graph = nx.Graph()\n",
        "duplicated_graph.add_edges_from(duplicated_edges)\n",
        "unduplicated_graph = nx.Graph()\n",
        "unduplicated_graph.add_edges_from(unduplicated_edges)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "724109ed-2bd8-852f-0f20-ef1a66125fc8"
      },
      "outputs": [],
      "source": [
        "def draw_degree_distribution(G):\n",
        "    hist = nx.degree_histogram(G)\n",
        "    print(hist)\n",
        "    plt.scatter(np.arange(len(hist)), hist)\n",
        "    plt.xlabel(\"Degree (d)\")\n",
        "    plt.ylabel(\"Frequency\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b48bdebd-1411-2761-d625-41e4da4d3173"
      },
      "outputs": [],
      "source": [
        "# is_duplicate == 1\n",
        "draw_degree_distribution(duplicated_graph)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "baaf87c2-5686-26c9-8ebf-3cd334f91298"
      },
      "outputs": [],
      "source": [
        "# is_duplicate == 0\n",
        "draw_degree_distribution(unduplicated_graph)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "76247ee9-879e-ea7c-283c-441b2ea47a34"
      },
      "source": [
        "Most questions are connected to another question, but some questions are connected to multiple questions."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "84d3d888-94ba-704c-d9bd-c7e99ec4c4cf"
      },
      "outputs": [],
      "source": [
        "duplicated_graph_degree = [(k, v) for k, v in duplicated_graph.degree().items()]\n",
        "duplicated_graph_degree.sort(key=lambda p: p[1], reverse=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b8700f0-b003-e956-d19e-443edd5a88da"
      },
      "outputs": [],
      "source": [
        "duplicated_graph_degree[:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ac895021-e7c2-fb17-ec1e-2ba95f7bfcda"
      },
      "outputs": [],
      "source": [
        "df_28133 = df_duplicate[(df_duplicate.qid1 == 28133) | (df_duplicate.qid2 == 28133)]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7744b1fd-5520-7675-3cd8-7654b5c25a00"
      },
      "outputs": [],
      "source": [
        "df_28133.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "a965819d-e0ad-8859-f0ea-36d9336e4046"
      },
      "source": [
        "Many people are interested in losing weight :)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "da32f826-4778-0a63-2212-e05dd55a1373"
      },
      "outputs": [],
      "source": [
        "qid = 3595\n",
        "df_tmp = df_duplicate[(df_duplicate.qid1 == qid) | (df_duplicate.qid2 == qid)]\n",
        "df_tmp.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ce11aefc-c9f4-89c5-e9f0-6d73598f6593"
      },
      "source": [
        "The topic of qid: 3595 is also losing weight..."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1e1fbf58-f81f-6e29-09b6-129b6834ff3f"
      },
      "outputs": [],
      "source": [
        "qid = 38\n",
        "df_tmp = df_duplicate[(df_duplicate.qid1 == qid) | (df_duplicate.qid2 == qid)]\n",
        "df_tmp.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1c022665-67e7-b6f3-98df-a9220efa898f"
      },
      "outputs": [],
      "source": [
        "df_tmp.head().question1.values"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "3940e4bc-20b4-fcd1-64a2-83a0eac1d1ec"
      },
      "source": [
        "The topic of qid: 38 is about the relationship between Quora and search engines. Should we start to search on Google?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "28a30124-872e-967a-9fc3-16dd04a5b45b"
      },
      "outputs": [],
      "source": [
        "qid = 4951\n",
        "df_tmp = df_duplicate[(df_duplicate.qid1 == qid) | (df_duplicate.qid2 == qid)]\n",
        "df_tmp.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "f0d3b0cf-8bf3-0da5-a348-4d138e6a6fee"
      },
      "source": [
        "The topic of qid: 4951 is like above."
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}