mirror of
https://github.com/Estom/notes.git
synced 2026-02-03 10:33:35 +08:00
226 lines
4.8 KiB
Plaintext
226 lines
4.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import networkx as nx\n",
|
|
"G = nx.Graph()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"G.add_nodes_from([1,2,3])\n",
|
|
"G.add_edge(3,4)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[1, 2, 3, 4]\n",
|
|
"[(3, 4)]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(G.nodes())\n",
|
|
"print(G.edges())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<ipython-input-20-2623cf771fd5>:3: UserWarning: Matplotlib is currently using ps, which is a non-GUI backend, so cannot show the figure.\n",
|
|
" plt.show()\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import matplotlib.pyplot as plt\n",
|
|
"nx.draw(G)\n",
|
|
"plt.show()\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"(1, 2, 0.125)\n",
|
|
"(2, 1, 0.125)\n",
|
|
"(3, 4, 0.375)\n",
|
|
"(4, 3, 0.375)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"FG = nx.Graph()\n",
|
|
"FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)])\n",
|
|
"for n, nbrs in FG.adj.items():\n",
|
|
" for nbr, eattr in nbrs.items():\n",
|
|
" wt = eattr['weight']\n",
|
|
" if wt < 0.5: print(f\"({n}, {nbr}, {wt:.3})\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"(1, 2, 0.125)\n",
|
|
"(3, 4, 0.375)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for (u, v, wt) in FG.edges.data('weight'):\n",
|
|
" if wt < 0.5:\n",
|
|
" print(f\"({u}, {v}, {wt:.3})\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 33,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"ItemsView(AdjacencyView({1: {2: {'weight': 0.5}}, 2: {}, 3: {1: {'weight': 0.75}}}))\n",
|
|
"[3]\n",
|
|
"[2]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"DG = nx.DiGraph()\n",
|
|
"DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])\n",
|
|
"DG.out_degree(1, weight='weight')\n",
|
|
"print(DG.adj.items())\n",
|
|
"print(list(DG.predecessors(1)))\n",
|
|
"print(list(DG.successors(1)))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 35,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[[0 1 0 1 0 0 1 1 0 0]\n",
|
|
" [1 0 1 1 1 1 0 1 0 1]\n",
|
|
" [0 1 0 0 1 1 1 1 0 1]\n",
|
|
" [0 0 1 0 0 0 0 1 1 1]\n",
|
|
" [1 0 0 1 1 1 0 1 1 1]\n",
|
|
" [0 0 0 0 0 0 0 0 1 1]\n",
|
|
" [0 0 1 0 0 0 1 1 1 1]\n",
|
|
" [1 0 1 0 0 1 1 0 0 0]\n",
|
|
" [1 1 1 1 1 0 1 0 0 0]\n",
|
|
" [1 1 1 0 0 0 0 0 0 1]]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"a = np.random.randint(0, 2, size=(10, 10))\n",
|
|
"print(a)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 36,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"D = nx.DiGraph(a)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 40,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"array([[0., 1., 0., 1., 0., 0., 1., 1., 0., 0.],\n",
|
|
" [1., 0., 1., 1., 1., 1., 0., 1., 0., 1.],\n",
|
|
" [0., 1., 0., 0., 1., 1., 1., 1., 0., 1.],\n",
|
|
" [0., 0., 1., 0., 0., 0., 0., 1., 1., 1.],\n",
|
|
" [1., 0., 0., 1., 1., 1., 0., 1., 1., 1.],\n",
|
|
" [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],\n",
|
|
" [0., 0., 1., 0., 0., 0., 1., 1., 1., 1.],\n",
|
|
" [1., 0., 1., 0., 0., 1., 1., 0., 0., 0.],\n",
|
|
" [1., 1., 1., 1., 1., 0., 1., 0., 0., 0.],\n",
|
|
" [1., 1., 1., 0., 0., 0., 0., 0., 0., 1.]])"
|
|
]
|
|
},
|
|
"execution_count": 40,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"D.nodes()\n",
|
|
"D.edges()\n",
|
|
"nx.to_numpy_array(D)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"interpreter": {
|
|
"hash": "5ef0042cb263260037aa2928643ae94e240dd3afaec7872ebebe4f07619ddd0c"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3.8.8 ('ml')",
|
|
"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.8.8"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|