Advertisement
coderchesser

Code#1

Apr 12th, 2025
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | Source Code | 0 0
  1. import numpy as np
  2. import networkx as nx
  3. import matplotlib.pyplot as plt
  4.  
  5. # ------------------------------
  6. # 1. Define a Dummy Medical Dataset
  7. # ------------------------------
  8.  
  9. entities = [
  10.     'Fever', 'Cough', 'Flu', 'Headache', 'Sore Throat',
  11.     'Body Ache', 'Cold', 'COVID', 'Pneumonia', 'Bronchitis'
  12. ]
  13.  
  14. embeddings = {
  15.     'Fever':       np.array([0.9, 0.3]),
  16.     'Cough':       np.array([0.8, 0.4]),
  17.     'Flu':         np.array([0.85, 0.35]),
  18.     'Headache':    np.array([0.3, 0.9]),
  19.     'Sore Throat': np.array([0.6, 0.7]),
  20.     'Body Ache':   np.array([0.75, 0.45]),
  21.     'Cold':        np.array([0.8, 0.5]),
  22.     'COVID':       np.array([0.82, 0.48]),
  23.     'Pneumonia':   np.array([0.4, 0.6]),
  24.     'Bronchitis':  np.array([0.42, 0.58])
  25. }
  26.  
  27. # ------------------------------
  28. # 2. Define Helper Functions for Similarity and Fuzzy Membership
  29. # ------------------------------
  30.  
  31. def cosine_similarity(vec1, vec2):
  32.     return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
  33.  
  34. def sigmoid(x):
  35.     return 1 / (1 + np.exp(-x))
  36.  
  37. def fuzzy_membership(similarity):
  38.     return sigmoid(similarity)
  39.  
  40. # ------------------------------
  41. # 3. Construct the Fuzzy Graph
  42. # ------------------------------
  43.  
  44. G = nx.Graph()
  45.  
  46. for entity in entities:
  47.     G.add_node(entity)
  48.  
  49. threshold = 0.55
  50.  
  51. for i in range(len(entities)):
  52.     for j in range(i+1, len(entities)):
  53.         sim = cosine_similarity(embeddings[entities[i]], embeddings[entities[j]])
  54.         mu_val = fuzzy_membership(sim)
  55.         if mu_val > threshold:
  56.             G.add_edge(entities[i], entities[j], weight=mu_val)
  57.  
  58. # ------------------------------
  59. # 4. Augmenting With a Bayesian Network Slice
  60. # ------------------------------
  61.  
  62. alpha = 0.9
  63. bayesian_network = {}
  64. for u, v, data in G.edges(data=True):
  65.     mu_val = data['weight']
  66.     bayesian_network[(u, v)] = alpha * mu_val
  67.     bayesian_network[(v, u)] = alpha * mu_val
  68.  
  69. # ------------------------------
  70. # 5. Visualization of the Fuzzy Graph
  71. # ------------------------------
  72.  
  73. pos = nx.spring_layout(G, seed=42)
  74.  
  75. node_color = 'skyblue'
  76. node_size = 800
  77.  
  78. edges = G.edges(data=True)
  79. edge_weights = [data['weight'] for (u, v, data) in edges]
  80.  
  81. cmap = plt.cm.plasma
  82. norm = plt.Normalize(min(edge_weights), max(edge_weights))
  83. edge_colors = [cmap(norm(weight)) for weight in edge_weights]
  84.  
  85. plt.figure(figsize=(10, 7))
  86. nx.draw_networkx_nodes(G, pos, node_color=node_color, node_size=node_size)
  87. nx.draw_networkx_labels(G, pos, font_size=10, font_weight='bold')
  88. nx.draw_networkx_edges(G, pos, edge_color=edge_colors, width=2)
  89.  
  90. sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
  91. sm.set_array([])
  92.  
  93. plt.colorbar(sm, label='Fuzzy Membership Weight', ax=plt.gca())
  94.  
  95. plt.title('Fuzzy Graph of Dummy Medical Entities')
  96. plt.axis('off')
  97. plt.show()
  98.  
  99. # ------------------------------
  100. # 6. Display the Bayesian Network Probabilities
  101. # ------------------------------
  102.  
  103. print("Bayesian Network Conditional Probabilities (P(child=True|parent=True)):")
  104. for edge, prob in bayesian_network.items():
  105.     print(f"P({edge[1]}=True | {edge[0]}=True) = {prob:.3f}")
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement