{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "PXZouNKCnTLM" }, "source": [ "# SNT - TP Python 1 : Modélisation d'un réseau social" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "P7klja9m0LqK" }, "source": [ "\"Kitten\"" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Za4SNnD02VP3" }, "source": [ "Pour commencer, exécutez le script suivant pour importer deux modules qui nous seront nécessaires pendant tout le TP." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "nwDAFtmp2DAG" }, "outputs": [], "source": [ "# importation du module \"matplotlib\" pour créer des graphiques\n", "import matplotlib.pyplot as plt\n", "# importation du module \"networkx\" pour simuler un réseau social\n", "import networkx as nx" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3nPzAzNinfKM" }, "source": [ "## 1 Introduction" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "J67N7uX9nwxw" }, "source": [ "Exécuter plusieurs fois le script Python ci-dessous (sans chercher pour le moment à en comprendre le code) :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "fkztyiGsl9hh" }, "outputs": [], "source": [ "#import warnings\n", "G = nx.random_geometric_graph(200, 0.125)\n", "pos = nx.get_node_attributes(G, 'pos')\n", "dmin = 1\n", "ncenter = 0\n", "for n in pos:\n", " x, y = pos[n]\n", " d = (x - 0.5)**2 + (y - 0.5)**2\n", " if d < dmin:\n", " ncenter = n\n", " dmin = d\n", "\n", "plt.clf()\n", "#with warnings.catch_warnings(record=True):\n", "p = dict(nx.single_source_shortest_path_length(G, ncenter))\n", "plt.figure(figsize=(8, 8))\n", "nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)\n", "nx.draw_networkx_nodes(G, pos, nodelist=list(p.keys()),\n", " node_size=80,\n", " node_color=list(p.values()),\n", " cmap=plt.cm.Reds_r)\n", "\n", "plt.xlim(-0.05, 1.05)\n", "plt.ylim(-0.05, 1.05)\n", "plt.axis('off')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JrDg5CSYn7Qk" }, "source": [ "**Exercice 1** : Expliquer pourquoi la figure générée nous permet de modéliser un réseau social." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Dv3CQXuaoLjx" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "dkxZQkV4qEKZ" }, "source": [ "## 2 Découverte du module networkx" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "CdbhEd5nwTfC" }, "source": [ "Simulons un réseau social avec le module Python `networkx`.\n", "\n", "Lire puis exécuter plusieurs fois le code suivant :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "DCosSw2DqWQQ" }, "outputs": [], "source": [ "# création d'un graphe vide non orienté appelé \"monReseau\"\n", "monReseau = nx.Graph()\n", "\n", "monReseau.add_node(\"Audrey\")\n", "monReseau.add_node(\"Nicolas\")\n", "monReseau.add_node(\"Alexis\")\n", "monReseau.add_node(\"Hélène\")\n", "\n", "plt.clf()\n", "# dessine (draw) et affiche (show) le graphe \"monReseau\"\n", "nx.draw(monReseau, with_labels=True)\n", "plt.show()\n", "\n", "print(\"Nombre de personnes =\", monReseau.number_of_nodes())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pQi47nXvxG31" }, "source": [ "**Exercice 2** : Que fait le code de la ligne 6 : `monReseau.add_node(\"Alexis\")` ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "0l_L3Ij4wwAm" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "nbP5UDKaypo5" }, "source": [ "Lire puis exécuter plusieurs fois le code suivant :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "ImUlXBDzw1EH" }, "outputs": [], "source": [ "plt.clf()\n", "\n", "monReseau = nx.Graph()\n", "\n", "monReseau.add_node(\"Audrey\")\n", "monReseau.add_node(\"Nicolas\")\n", "monReseau.add_node(\"Alexis\")\n", "monReseau.add_node(\"Hélène\")\n", "\n", "monReseau.add_edge(\"Audrey\", \"Nicolas\")\n", "monReseau.add_edge(\"Audrey\", \"Alexis\")\n", "monReseau.add_edge(\"Nicolas\", \"Alexis\")\n", "monReseau.add_edge(\"Hélène\", \"Audrey\")\n", "monReseau.add_edge(\"Hélène\", \"Nicolas\")\n", "\n", "nx.draw(monReseau, with_labels=True)\n", "plt.show()\n", "\n", "print(\"Nombre de personnes =\", monReseau.number_of_nodes())\n", "print(\"Nombre de liens =\", monReseau.number_of_edges())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uxOAGCxNx98x" }, "source": [ "**Exercice 3** : Que fait le code de la ligne 8 : `monReseau.add_edge(\"Audrey\",\"Nicolas\")` ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4oHzS1-9wvF2" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cPo7VAkdEY4r" }, "source": [ "**Exercice 4** : Quelles sont les deux personnes qui ne sont pas directement en relation ? Que faudrait-il faire pour les mettre en relation ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Jh1p2r64EpXs" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "MwuOxgaCFm6F" }, "source": [ "## 3 Un graphe orienté" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "RN0DPh8f2yyD" }, "source": [ "Lire puis exécuter plusieurs fois le code suivant, qui est presque le même que le code précédent :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "IaPjWjY42Rwg" }, "outputs": [], "source": [ "plt.clf()\n", "\n", "monReseau = nx.DiGraph() # création d'un graphe orienté\n", "\n", "monReseau.add_node(\"Audrey\")\n", "monReseau.add_node(\"Nicolas\")\n", "monReseau.add_node(\"Alexis\")\n", "monReseau.add_node(\"Hélène\")\n", "\n", "monReseau.add_edge(\"Audrey\", \"Nicolas\") # lien de Audrey vers Nicolas\n", "monReseau.add_edge(\"Audrey\", \"Alexis\")\n", "monReseau.add_edge(\"Nicolas\", \"Alexis\")\n", "monReseau.add_edge(\"Hélène\", \"Audrey\")\n", "monReseau.add_edge(\"Hélène\", \"Nicolas\")\n", "monReseau.add_edge(\"Alexis\", \"Audrey\")\n", "\n", "nx.draw(monReseau, with_labels=True)\n", "plt.show()\n", "\n", "print(\"Nombre de personnes =\", monReseau.number_of_nodes())\n", "print(\"Nombre de liens =\", monReseau.number_of_edges())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "LBbIhfK03kea" }, "source": [ "**Exercice 5** : Qu'est-ce qui a changé par rapport au graphe précédent ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Qd1Wyv4K34Jn" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "afK4phwCGCF-" }, "source": [ "**Exercice 6** : Pourquoi la relation entre Audrey et Alexis est-elle différente des autres ? Pourquoi voit-on 5 segments mais 6 liens ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "9v7atjS3GK5Q" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kfR8oJj-Bmcz" }, "source": [ "**Exercice 7** : Ce dernier graphe est dit **orienté**, par opposition au graphe du paragraphe précédent qui est dit **non orienté**. Parmi Facebook, Twitter, Instagram, WhatsApp, Snapchat, citer un réseau social orienté et un réseau social non orienté." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JN3ciF_ZC0Zv" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hmYYAcYqyx6R" }, "source": [ "## 4 Mon petit réseau social" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Hg5RxXk8zS6o" }, "source": [ "**Exercice 8** : Choisir six prénoms de votre choix et simuler graphiquement un réseau social **non orienté** entre ces personnes, à l'aide du code appris précédemment." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "q98gY_5qr0oN" }, "outputs": [], "source": [ "# votre code ici\n", "...\n", "pass" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "zqPbPBz_5Bt2" }, "source": [ "## 5 Un réseau social où tout le monde est amis" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kuyFwz1rGb68" }, "source": [ "Lire puis exécuter plusieurs fois le programme suivant." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "JLMauw_55H-s" }, "outputs": [], "source": [ "plt.clf()\n", "\n", "monRezo = nx.Graph()\n", "\n", "Liste = [\"Ronaldo\", \"Messi\", \"Totti\", \"Salah\", \"Mbappé\", \"Neymar\"]\n", "\n", "for a in Liste:\n", " monRezo.add_node(a)\n", "\n", "for b in Liste:\n", " for c in Liste:\n", " monRezo.add_edge(b, c)\n", "\n", "nx.draw(monRezo, with_labels = True)\n", "plt.show()\n", "\n", "print(\"Nombre de personnes =\", monRezo.number_of_nodes())\n", "print(\"Nombre de liens =\", monRezo.number_of_edges())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "P5huorFeIGxU" }, "source": [ "**Exercice 9** : Expliquer ce que font les lignes 8, 9 et 10 du programme." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "CLNCYe4WIYJ6" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ujqLQSVfKPxz" }, "source": [ "## 6 Un réseau social plus ou moins social" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Z5Rag5B61yDX" }, "source": [ "La fonction `random()` renvoie un nombre réel au hasard entre 0 et 1.\n", "\n", "Lire puis exécutez plusieurs fois de suite le programme suivant :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "gVBk9JA95mEH" }, "outputs": [], "source": [ "from random import random\n", "\n", "plt.clf()\n", "\n", "monRezo = nx.Graph()\n", "\n", "Liste = [\"Ronaldo\", \"Messi\", \"Totti\", \"Salah\", \"Pogba\", \"Neymar\"]\n", "p = 0.3\n", "\n", "for a in Liste:\n", " monRezo.add_node(a)\n", "\n", "for b in Liste:\n", " for c in Liste:\n", " if random() < p:\n", " monRezo.add_edge(b, c)\n", "\n", "nx.draw(monRezo, with_labels = True)\n", "plt.show()\n", "\n", "print(\"Nombre de personnes =\", monRezo.number_of_nodes())\n", "print(\"Nombre de liens =\", monRezo.number_of_edges())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "HVzfqSdTJLrx" }, "source": [ "**Exercice 10** : Par défaut `p = 0.3` (ligne 6 du programme). Donnez à `p` la valeur `0.1` puis exécutez plusieurs fois le programme. Donnez à `p` la valeur `0.9` puis exécutez plusieurs fois le programme. Que constatez-vous ? Pourquoi ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Tf0jr-H6o1Xj" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uUruzBzSo14E" }, "source": [ "## 7 D'autres réseaux ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Yan5CMbk5hA8" }, "source": [ "**Bonus** : Nous venons de simuler un réseau social avec le module Python `networkx`. Quels autres types de réseaux pourrait-on modéliser avec `networkx` ?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "x5sF25-U561q" }, "source": [ "*---> votre réponse dans cette cellule...*" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_QHg-GV7DCIZ" }, "source": [ "\n", "\n", "---\n", "\n" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "SNT - TP Python - Réseau Social.ipynb", "provenance": [] }, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }