Networkx example¶
This Example will show you step by step, how to use networkx graphs with a PHOTONAI Graph pipeline.
First: Necessary imports
In [1]:
Copied!
from networkx.generators import barabasi_albert_graph, erdos_renyi_graph
from photonai.base import Hyperpipe, PipelineElement
from sklearn.model_selection import KFold
import networkx as nx
import numpy as np
from networkx.generators import barabasi_albert_graph, erdos_renyi_graph
from photonai.base import Hyperpipe, PipelineElement
from sklearn.model_selection import KFold
import networkx as nx
import numpy as np
Next: Generate graphs and labels
In [2]:
Copied!
# create dataset of 1000 graphs
rr_graphs = [erdos_renyi_graph(20, .25) for _ in range(500)]
er_graphs = [erdos_renyi_graph(20, .2) for _ in range(500)]
graphs = rr_graphs + er_graphs
# we have to transform the networkx graphs into numpy graphs before passing them to photon
graphs = np.array([nx.to_numpy_array(g) for g in graphs])
graphs = np.expand_dims(graphs, axis=-1)
# create labels
rr_labels = [0] * 500
er_labels = [1] * 500
labels = rr_labels + er_labels
# create dataset of 1000 graphs
rr_graphs = [erdos_renyi_graph(20, .25) for _ in range(500)]
er_graphs = [erdos_renyi_graph(20, .2) for _ in range(500)]
graphs = rr_graphs + er_graphs
# we have to transform the networkx graphs into numpy graphs before passing them to photon
graphs = np.array([nx.to_numpy_array(g) for g in graphs])
graphs = np.expand_dims(graphs, axis=-1)
# create labels
rr_labels = [0] * 500
er_labels = [1] * 500
labels = rr_labels + er_labels
Setup Hyperpipe
In [3]:
Copied!
# Design your Pipeline
my_pipe = Hyperpipe('networkx_example_pipe',
inner_cv=KFold(n_splits=2),
optimizer='sk_opt',
optimizer_params={'n_configurations': 25},
metrics=['accuracy', 'balanced_accuracy', 'recall', 'precision'],
best_config_metric='accuracy')
my_pipe.add(PipelineElement('GCNClassifier', feature_axis=0))
# Design your Pipeline
my_pipe = Hyperpipe('networkx_example_pipe',
inner_cv=KFold(n_splits=2),
optimizer='sk_opt',
optimizer_params={'n_configurations': 25},
metrics=['accuracy', 'balanced_accuracy', 'recall', 'precision'],
best_config_metric='accuracy')
my_pipe.add(PipelineElement('GCNClassifier', feature_axis=0))
Final step: Train your Hyperpipe
In [4]:
Copied!
my_pipe.fit(graphs, labels)
my_pipe.fit(graphs, labels)
===================================================================================================== PHOTONAI ANALYSIS: networkx_example_pipe ===================================================================================================== Found 2 target classes: [0 1] ***************************************************************************************************** Outer Cross validation Fold 1 ***************************************************************************************************** Did not find any hyperparameter to convert into skopt space.
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/optimization/scikit_optimize/sk_opt.py:169: UserWarning: Did not find any hyperparameter to convert into skopt space. warnings.warn(msg) 100%|██████████| 200/200 [00:22<00:00, 8.90it/s] 100%|██████████| 200/200 [00:21<00:00, 9.25it/s]
----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +-------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +-------------------+-------------------+------------------+ | accuracy | 0.7800 | 0.7737 | | balanced_accuracy | 0.7802 | 0.7737 | | recall | 0.7893 | 0.7815 | | precision | 0.7779 | 0.7720 | +-------------------+-------------------+------------------+
100%|██████████| 200/200 [00:41<00:00, 4.79it/s]
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +-------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +-------------------+-------------------+------------------+ | accuracy | 0.7775 | 0.7700 | | balanced_accuracy | 0.7773 | 0.7704 | | recall | 0.8015 | 0.7835 | | precision | 0.7672 | 0.7525 | +-------------------+-------------------+------------------+ ***************************************************************************************************** Finished all outer fold computations.
100%|██████████| 200/200 [00:52<00:00, 3.84it/s]
***************************************************************************************************** ANALYSIS INFORMATION ================================================================================ Project Folder: /home/runner/work/photonai_graph/photonai_graph/documentation/docs/examples/networkx_example_pipe_results_2023-02-17_16-36-40, Computation Time: 2023-02-17 16:36:40.365564 - 2023-02-17 16:38:09.836040 Duration: 0:01:29.470476 Optimized for: accuracy Hyperparameter Optimizer: sk_opt DUMMY RESULTS ======================================================================================= +-------------------+--+ | PERFORMANCE DUMMY | | +-------------------+--+ +-------------------+--+ AVERAGE PERFORMANCE ACROSS OUTER FOLDS ============================================================== +-------------------+---------------+--------------+-----------+----------+ | Metric Name | Training Mean | Training Std | Test Mean | Test Std | +-------------------+---------------+--------------+-----------+----------+ | accuracy | 0.7775 | 0.0 | 0.77 | 0.0 | | balanced_accuracy | 0.777319 | 0.0 | 0.770393 | 0.0 | | recall | 0.801489 | 0.0 | 0.783505 | 0.0 | | precision | 0.767221 | 0.0 | 0.752475 | 0.0 | +-------------------+---------------+--------------+-----------+----------+ BEST HYPERPARAMETER CONFIGURATION =================================================================== {} +--------+----------+-------------------+--------+-----------+----------------------------+ | fold # | accuracy | balanced_accuracy | recall | precision | Best Hyperparameter Config | +--------+----------+-------------------+--------+-----------+----------------------------+ | 1* | 0.7700 | 0.7704 | 0.7835 | 0.7525 | {} | +--------+----------+-------------------+--------+-----------+----------------------------+ PHOTONAI 2.3.0 ====================================================================================== Your results are stored in /home/runner/work/photonai_graph/photonai_graph/documentation/docs/examples/networkx_example_pipe_results_2023-02-17_16-36-40 Go to https://explorer.photon-ai.com and upload your photonai_results.json for convenient result visualization! For more info and documentation visit https://www.photon-ai.com
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/IPython/core/formatters.py:972, in MimeBundleFormatter.__call__(self, obj, include, exclude) 969 method = get_real_method(obj, self.print_method) 971 if method is not None: --> 972 return method(include=include, exclude=exclude) 973 return None 974 else: File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:631, in BaseEstimator._repr_mimebundle_(self, **kwargs) 629 output = {"text/plain": repr(self)} 630 if get_config()["display"] == "diagram": --> 631 output["text/html"] = estimator_html_repr(self) 632 return output File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:417, in estimator_html_repr(estimator) 403 fallback_msg = ( 404 "In a Jupyter environment, please rerun this cell to show the HTML" 405 " representation or trust the notebook. <br />On GitHub, the" 406 " HTML representation is unable to render, please try loading this page" 407 " with nbviewer.org." 408 ) 409 out.write( 410 f"<style>{style_with_id}</style>" 411 f'<div id="{container_id}" class="sk-top-container">' (...) 415 '<div class="sk-container" hidden>' 416 ) --> 417 _write_estimator_html( 418 out, 419 estimator, 420 estimator.__class__.__name__, 421 estimator_str, 422 first_call=True, 423 ) 424 out.write("</div></div>") 426 html_output = out.getvalue() File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:151, in _write_estimator_html(out, estimator, estimator_label, estimator_label_details, first_call) 149 """Write estimator to html in serial, parallel, or by itself (single).""" 150 if first_call: --> 151 est_block = _get_visual_block(estimator) 152 else: 153 with config_context(print_changed_only=True): File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:127, in _get_visual_block(estimator) 123 # check if estimator looks like a meta estimator wraps estimators 124 if hasattr(estimator, "get_params"): 125 estimators = [ 126 (key, est) --> 127 for key, est in estimator.get_params(deep=False).items() 128 if hasattr(est, "get_params") and hasattr(est, "fit") 129 ] 130 if estimators: 131 return _VisualBlock( 132 "parallel", 133 [est for _, est in estimators], 134 names=[f"{key}: {est.__class__.__name__}" for key, est in estimators], 135 name_details=[str(est) for _, est in estimators], 136 ) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:211, in BaseEstimator.get_params(self, deep) 209 out = dict() 210 for key in self._get_param_names(): --> 211 value = getattr(self, key) 212 if deep and hasattr(value, "get_params") and not isinstance(value, type): 213 deep_items = value.get_params().items() AttributeError: 'Hyperpipe' object has no attribute 'best_config_metric'
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/IPython/core/formatters.py:342, in BaseFormatter.__call__(self, obj) 340 method = get_real_method(obj, self.print_method) 341 if method is not None: --> 342 return method() 343 return None 344 else: File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:625, in BaseEstimator._repr_html_inner(self) 620 def _repr_html_inner(self): 621 """This function is returned by the @property `_repr_html_` to make 622 `hasattr(estimator, "_repr_html_") return `True` or `False` depending 623 on `get_config()["display"]`. 624 """ --> 625 return estimator_html_repr(self) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:417, in estimator_html_repr(estimator) 403 fallback_msg = ( 404 "In a Jupyter environment, please rerun this cell to show the HTML" 405 " representation or trust the notebook. <br />On GitHub, the" 406 " HTML representation is unable to render, please try loading this page" 407 " with nbviewer.org." 408 ) 409 out.write( 410 f"<style>{style_with_id}</style>" 411 f'<div id="{container_id}" class="sk-top-container">' (...) 415 '<div class="sk-container" hidden>' 416 ) --> 417 _write_estimator_html( 418 out, 419 estimator, 420 estimator.__class__.__name__, 421 estimator_str, 422 first_call=True, 423 ) 424 out.write("</div></div>") 426 html_output = out.getvalue() File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:151, in _write_estimator_html(out, estimator, estimator_label, estimator_label_details, first_call) 149 """Write estimator to html in serial, parallel, or by itself (single).""" 150 if first_call: --> 151 est_block = _get_visual_block(estimator) 152 else: 153 with config_context(print_changed_only=True): File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:127, in _get_visual_block(estimator) 123 # check if estimator looks like a meta estimator wraps estimators 124 if hasattr(estimator, "get_params"): 125 estimators = [ 126 (key, est) --> 127 for key, est in estimator.get_params(deep=False).items() 128 if hasattr(est, "get_params") and hasattr(est, "fit") 129 ] 130 if estimators: 131 return _VisualBlock( 132 "parallel", 133 [est for _, est in estimators], 134 names=[f"{key}: {est.__class__.__name__}" for key, est in estimators], 135 name_details=[str(est) for _, est in estimators], 136 ) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:211, in BaseEstimator.get_params(self, deep) 209 out = dict() 210 for key in self._get_param_names(): --> 211 value = getattr(self, key) 212 if deep and hasattr(value, "get_params") and not isinstance(value, type): 213 deep_items = value.get_params().items() AttributeError: 'Hyperpipe' object has no attribute 'best_config_metric'
Out[4]:
Hyperpipe(name='networkx_example_pipe')