Advertisement
Danila_lipatov

graphs_plot_examp

Jan 12th, 2023 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #https://www.cian.ru/sale/flat/281946132/
  2. #https://www.cian.ru/sale/flat/281271977/
  3. #https://www.cian.ru/sale/flat/281748776/
  4.  
  5.  
  6. import pyvis
  7. from uuid import uuid4
  8.  
  9. from pyvis.network import Network
  10.  
  11. net = Network() # создаём объект графа
  12. import pandas as pd
  13.  
  14. df = pd.DataFrame({'ID':[1,2,3,4,5,6],
  15. 'First Name':['Felix', 'Jean', 'James', 'Daphne', 'James', 'Peter'],
  16. 'Family Name': ['Revert', 'Durand', 'Wright', 'Hull', 'Conrad', 'Donovan'],
  17. 'Phone number': ['+33 6 12 34 56 78', '+33 7 00 00 00 00', '+33 6 12 34 56 78', '+33 6 99 99 99 99', '+852 0123 4567', '+852 0123 4567'],
  18. 'Email': ['felix.revert@gmail.com', 'jean.durand@gmail.com', 'j.custom@gmail.com', pd.np.nan, 'j.custom@gmail.com', pd.np.nan]})
  19.  
  20.  
  21. column_edge = 'Phone number'
  22. column_ID = 'ID'
  23.  
  24. data_to_merge = df[[column_ID, column_edge]].dropna(subset=[column_edge]).drop_duplicates() # select columns, remove NaN
  25.  
  26. # To create connections between people who have the same number,
  27. # join data with itself on the 'ID' column.
  28. data_to_merge = data_to_merge.merge(
  29. data_to_merge[[column_ID, column_edge]].rename(columns={column_ID:column_ID+"_2"}),
  30. on=column_edge
  31. )
  32.  
  33. # By joining the data with itself, people will have a connection with themselves.
  34. # Remove self connections, to keep only connected people who are different.
  35. d = data_to_merge[~(data_to_merge[column_ID] == data_to_merge[column_ID + "_2"])] \
  36. .dropna()[[column_ID, column_ID + "_2", column_edge]]
  37.  
  38. # To avoid counting twice the connections (person 1 connected to person 2 and person 2 connected to person 1)
  39. # we force the first ID to be "lower" then ID_2
  40. d.drop(d.loc[d[column_ID + "_2"] < d[column_ID]].index.tolist(), inplace=True)
  41.  
  42.  
  43. # добавление узлов
  44. net.add_nodes(
  45. df, # node ids
  46. label=['Table1', 'Table2', 'Table3', 'Table4', 'TAble5'], # node labels
  47. # node titles (display on mouse hover)
  48. title=['Main node', 'Just node', 'Just node', 'Just node', 'Node with self-loop'],
  49. color=['#d47415', '#22b512', '#42adf5', '#4a21b0', '#e627a3'] # node colors (HEX)
  50. )
  51. # добавляем тот же список узлов, что и в предыдущем примере
  52. net.add_edges([])
  53.  
  54. net.show('graph.html')
  55.  
  56. """import plotly.express as px
  57. import datapane as dp
  58.  
  59. location_df = dp.Blob.get(name='location_df', owner='khuyentran1401').download_df()
  60.  
  61. m = px.scatter_geo(location_df, lat='latitude', lon='longitude',
  62. color='total_stars', size='forks',
  63. hover_data=['user_name','followers'],
  64. title='Locations of Top Users')
  65.  
  66. m.show()"""
  67. """import datapane as dp
  68. import pandas as pd
  69. import numpy as np
  70.  
  71. df = pd.DataFrame(
  72. {
  73. "A": np.random.normal(-1, 1, 5),
  74. "B": np.random.normal(1, 2, 5),
  75. }
  76. )
  77.  
  78. table = dp.DataTable(df,)
  79. app = dp.App(table)
  80. app.save(path="simple-table.html")"""
  81.  
  82.  
  83. import networkx as nx
  84.  
  85. G = nx.from_pandas_edgelist(df=d, source=column_ID, target=column_ID+'_2', edge_attr=column_edge)
  86.  
  87. G.add_nodes_from(nodes_for_adding=df.ID.tolist())
  88. nx.draw(G)
  89.  
  90.  
  91.  
  92. import matplotlib.pyplot as plt
  93. import pyvis
  94. from uuid import uuid4
  95. import networkx as nx
  96. from pyvis.network import Network
  97.  
  98. #net = Network() # создаём объект графа
  99. df = pd.read_excel("example_uuid.xlsx")
  100. G = nx.Graph() # создаём объект графа
  101. fig = plt.figure()
  102.  
  103. # определяем список узлов (ID узлов)
  104. nodes = []
  105. for col in df:
  106. for i in df[col].unique():
  107. nodes.append(i)
  108. """for val in col[1]:
  109. print(val)"""
  110. #nodes.append(val)
  111.  
  112. stop = 0
  113.  
  114. # определяем список рёбер
  115. # список кортежей, каждый из которых представляет ребро
  116. # кортеж (id_1, id_2) означает, что узлы id_1 и id_2 соединены ребром
  117. #edges = [(1, 2), (1, 3), (2, 3), (2, 4), (3, 5), (5, 5)]
  118. edges = []
  119. for i in range(len(nodes) - 1):
  120. edges.append((nodes[i], nodes[i + 1]))
  121. # добавляем информацию в объект графа
  122. G.add_nodes_from(nodes)
  123. G.add_edges_from(edges)
  124.  
  125. # рисуем граф и отображаем его
  126. nx.draw(G, with_labels=True, font_weight='bold')
  127.  
  128. plt.show()
  129. stop = 0
  130.  
  131. """***HELPFUL LINKS***
  132.  
  133. https://habr.com/ru/company/vk/blog/522094/
  134. https://habr.com/ru/company/ruvds/blog/705368/
  135. https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html#sphx-glr-auto-examples-tree-plot-unveil-tree-structure-py
  136. https://machinelearningmastery.ru/getting-started-with-graph-analysis-in-python-with-pandas-and-networkx-5e2d2f82f18e/
  137. https://github.com/FelixChop/MediumArticles/blob/master/Graph_analysis_Python.ipynb"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement