Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import networkx as nx
- import matplotlib.pyplot as plt
- # ------------------------------
- # 1. Define a Dummy Medical Dataset
- # ------------------------------
- entities = [
- 'Fever', 'Cough', 'Flu', 'Headache', 'Sore Throat',
- 'Body Ache', 'Cold', 'COVID', 'Pneumonia', 'Bronchitis'
- ]
- embeddings = {
- 'Fever': np.array([0.9, 0.3]),
- 'Cough': np.array([0.8, 0.4]),
- 'Flu': np.array([0.85, 0.35]),
- 'Headache': np.array([0.3, 0.9]),
- 'Sore Throat': np.array([0.6, 0.7]),
- 'Body Ache': np.array([0.75, 0.45]),
- 'Cold': np.array([0.8, 0.5]),
- 'COVID': np.array([0.82, 0.48]),
- 'Pneumonia': np.array([0.4, 0.6]),
- 'Bronchitis': np.array([0.42, 0.58])
- }
- # ------------------------------
- # 2. Define Helper Functions for Similarity and Fuzzy Membership
- # ------------------------------
- def cosine_similarity(vec1, vec2):
- return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
- def sigmoid(x):
- return 1 / (1 + np.exp(-x))
- def fuzzy_membership(similarity):
- return sigmoid(similarity)
- # ------------------------------
- # 3. Construct the Fuzzy Graph
- # ------------------------------
- G = nx.Graph()
- for entity in entities:
- G.add_node(entity)
- threshold = 0.55
- for i in range(len(entities)):
- for j in range(i+1, len(entities)):
- sim = cosine_similarity(embeddings[entities[i]], embeddings[entities[j]])
- mu_val = fuzzy_membership(sim)
- if mu_val > threshold:
- G.add_edge(entities[i], entities[j], weight=mu_val)
- # ------------------------------
- # 4. Augmenting With a Bayesian Network Slice
- # ------------------------------
- alpha = 0.9
- bayesian_network = {}
- for u, v, data in G.edges(data=True):
- mu_val = data['weight']
- bayesian_network[(u, v)] = alpha * mu_val
- bayesian_network[(v, u)] = alpha * mu_val
- # ------------------------------
- # 5. Visualization of the Fuzzy Graph
- # ------------------------------
- pos = nx.spring_layout(G, seed=42)
- node_color = 'skyblue'
- node_size = 800
- edges = G.edges(data=True)
- edge_weights = [data['weight'] for (u, v, data) in edges]
- cmap = plt.cm.plasma
- norm = plt.Normalize(min(edge_weights), max(edge_weights))
- edge_colors = [cmap(norm(weight)) for weight in edge_weights]
- plt.figure(figsize=(10, 7))
- nx.draw_networkx_nodes(G, pos, node_color=node_color, node_size=node_size)
- nx.draw_networkx_labels(G, pos, font_size=10, font_weight='bold')
- nx.draw_networkx_edges(G, pos, edge_color=edge_colors, width=2)
- sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
- sm.set_array([])
- plt.colorbar(sm, label='Fuzzy Membership Weight', ax=plt.gca())
- plt.title('Fuzzy Graph of Dummy Medical Entities')
- plt.axis('off')
- plt.show()
- # ------------------------------
- # 6. Display the Bayesian Network Probabilities
- # ------------------------------
- print("Bayesian Network Conditional Probabilities (P(child=True|parent=True)):")
- for edge, prob in bayesian_network.items():
- print(f"P({edge[1]}=True | {edge[0]}=True) = {prob:.3f}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement