0% found this document useful (0 votes)
3 views

graph_analysis2_code

Uploaded by

Bhavan Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

graph_analysis2_code

Uploaded by

Bhavan Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

graph_analysis2

1 import numpy as np
2 import pandas as pd
3 import networkx as nx
4 from sklearn.ensemble import RandomForestRegresso­
r
5 from sklearn.model_selection import train_test_split
6 from sklearn.metrics import mean_squared_error, r2_score
7 from sklearn.model_selection import GridSearchCV
8 from scipy.stats import ttest_ind
9 import matplotlib.pyplot as plt
10 from mpl_toolkits.mplot3d import Axes3D
11
12 # Step 1: Generate Enhanced Canonical Graph Models
13 def generate_graphs(num_nodes=10):
14 graphs = {
15 "Erdos-Renyi": nx.erdos_renyi_graph(num_nodes, 0.5),
16 "Preferential Attachment": nx.barabasi_albert_grap­
h(num_nodes, 3),
17 "Watts-Strogatz": nx.watts_strogatz_graph­
(num_nodes, 4, 0.2),
18 "1D Lattice": nx.grid_graph(dim=[num_nodes]),
19 }
20 # Add edge weights to simulate latency or signal strength
21 for graph in graphs.values():
22 for u, v in graph.edges():
23 graph[u][v]['weight'] = np.random.uniform(0.1, 1.0) # Random weights
24 return graphs
25
26 # Step 2: Improved Feature Engineering
27 def create_dataset(graph):
28 # Features: degree, clustering, betweenness centrality, average shortest path length
29 degree = np.array([graph.degree[node] for node in graph.nodes()])
30 clustering = np.array([nx.clustering(graph, node) for node in graph.nodes()])
31 betweenness = np.array(list(nx.betweenness_centrali­
ty(graph).values()))
32 shortest_path = np.array([nx.average_shortest_pat­
h_length(graph) for _ in graph.nodes()])
33
34 # Combine features into a DataFrame
35 features = np.stack((degree, clustering, betweenness, shortest_path), axis=1)
36
37 # Target: Efficiency metric (e.g., inverse of latency)
38 targets = 1 / (1 + np.mean(shortest_path)) # Higher efficiency for shorter paths
39 target_values = np.full(len(graph.nodes()), targets)
40
41 return pd.DataFrame(features, columns=["Degree", "Clustering", "Betweenness", "ShortestPath"]), target_values
42
43 # Step 3: Visualize Graphs in 3D
44 def visualize_and_save(graph, name):
45 pos = nx.spring_layout(graph, dim=3, seed=42)
46 fig = plt.figure()
47 ax = fig.add_subplot(111, projection='3d')
48 for node, (x, y, z) in pos.items():
49 ax.scatter(x, y, z, label=node, s=50)
50 for neighbor in graph.neighbors(node):
51 nx_, ny_, nz_ = pos[neighbor]
52 ax.plot([x, nx_], [y, ny_], [z, nz_], color='gray', alpha=0.5)
53 ax.set_title(name)
54 plt.show()
55 adj_matrix = nx.adjacency_matrix(graph).todense()
56 return adj_matrix
57
58 # Step 4: Train Model with Hyperparameter Tuning
59 def train_and_evaluate(graphs, real_data=None):
60 results = {}
61 for name, graph in graphs.items():
62 print(f"\nProcessing {name} graph...")
63
64 # Visualize and save graph
65 adj_matrix = visualize_and_save(graph, name)
66
67 # Create dataset
68 X, y = create_dataset(graph)
69
70 # Split data
71 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
72
73 # Hyperparameter tuning for Random Forest
74 param_grid = {'n_estimators': [100, 200], 'max_depth': [None, 5, 10]}
75 model = GridSearchCV(RandomForestRegresso­
r(random_state=42), param_grid, cv=3)
76 model.fit(X_train, y_train)
77 best_model = model.best_estimator_
78
79 # Evaluate model
80 y_pred = best_model.predict(X_test)
81 mse = mean_squared_error(y_test, y_pred)
82 r2 = r2_score(y_test, y_pred)
83
84 # Compare with real data if provided
85 if real_data is not None:
86 _, p_value = ttest_ind(real_data, y_pred)
87 else:
88 p_value = 1.0 # Placeholder if no real data
89
90 results[name] = {
91 "MSE": mse,
92 "R2": r2,
93 "P-value": p_value,
94 "Adjacency Matrix": adj_matrix
95 }
96
97 print(f"Results for {name}: MSE={mse:.4f}, R2={r2:.4f}, P-value={p_value:.4f}")
98
99 return results
100
101 # Main Workflow
102 if __name__ == "__main__":
103 # Generate Graphs
104 graphs = generate_graphs(num_nodes=15)
105
106 # Simulated real data for comparison (e.g., NASA metrics)
107 real_data = np.random.uniform(0.1, 0.9, 20) # Simulated real-world values
108
109 # Train, Evaluate, and Compare
110 results = train_and_evaluate(graphs, real_data=real_data)
111
112 # Summary
113 print("\nFinal Results:")
114 for graph, metrics in results.items():
115 print(f"{graph}: {metrics}")
116

You might also like