Graph Kernel Pyramid Match example¶
In [1]:
Copied!
from photonai.base import Hyperpipe, PipelineElement
from photonai_graph.GraphUtilities import get_random_connectivity_data, get_random_labels
from sklearn.model_selection import KFold
from photonai.base import Hyperpipe, PipelineElement
from photonai_graph.GraphUtilities import get_random_connectivity_data, get_random_labels
from sklearn.model_selection import KFold
Generate random matrices to simulate connectivity matrices
In [2]:
Copied!
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=200)
y = get_random_labels(l_type="classification", number_of_labels=200)
# Design your Pipeline
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=200)
y = get_random_labels(l_type="classification", number_of_labels=200)
# Design your Pipeline
Design your Pipeline
In [3]:
Copied!
my_pipe = Hyperpipe('basic_kernel_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='grid_search',
metrics=['accuracy', 'balanced_accuracy', 'recall', 'precision'],
best_config_metric='accuracy')
my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95))
my_pipe.add(PipelineElement('GrakelAdapter', node_feature_construction="sum"))
my_pipe.add(PipelineElement('GrakelTransformer', transformation='PyramidMatch'))
my_pipe.add(PipelineElement("SVC", kernel='precomputed'))
my_pipe = Hyperpipe('basic_kernel_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='grid_search',
metrics=['accuracy', 'balanced_accuracy', 'recall', 'precision'],
best_config_metric='accuracy')
my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95))
my_pipe.add(PipelineElement('GrakelAdapter', node_feature_construction="sum"))
my_pipe.add(PipelineElement('GrakelTransformer', transformation='PyramidMatch'))
my_pipe.add(PipelineElement("SVC", kernel='precomputed'))
Finally fit the pipeline to our data
In [4]:
Copied!
my_pipe.fit(X, y)
my_pipe.fit(X, y)
===================================================================================================== PHOTONAI ANALYSIS: basic_kernel_pipe ===================================================================================================== Found 2 target classes: [0. 1.] JsonTransformer was unable to create the .json file. ***************************************************************************************************** Outer Cross validation Fold 1 *****************************************************************************************************
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py:833: UserWarning: JsonTransformer was unable to create the .json file. warnings.warn(msg)
-----------------------------------------------------------------------------------------------------
BEST_CONFIG
-----------------------------------------------------------------------------------------------------
{}
-----------------------------------------------------------------------------------------------------
VALIDATION PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.5312 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 1.0000 |
| precision | 1.0000 | 0.5312 |
+-------------------+-------------------+------------------+
-----------------------------------------------------------------------------------------------------
TEST PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4000 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 1.0000 |
| precision | 1.0000 | 0.4000 |
+-------------------+-------------------+------------------+
*****************************************************************************************************
Outer Cross validation Fold 2
*****************************************************************************************************
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
-----------------------------------------------------------------------------------------------------
BEST_CONFIG
-----------------------------------------------------------------------------------------------------
{}
-----------------------------------------------------------------------------------------------------
VALIDATION PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4562 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 0.8000 |
| precision | 1.0000 | 0.3875 |
+-------------------+-------------------+------------------+
-----------------------------------------------------------------------------------------------------
TEST PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4500 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 1.0000 |
| precision | 1.0000 | 0.4500 |
+-------------------+-------------------+------------------+
*****************************************************************************************************
Outer Cross validation Fold 3
*****************************************************************************************************
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
-----------------------------------------------------------------------------------------------------
BEST_CONFIG
-----------------------------------------------------------------------------------------------------
{}
-----------------------------------------------------------------------------------------------------
VALIDATION PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4625 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 0.2000 |
| precision | 1.0000 | 0.0625 |
+-------------------+-------------------+------------------+
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +-------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +-------------------+-------------------+------------------+ | accuracy | 1.0000 | 0.3250 | | balanced_accuracy | 1.0000 | 0.5000 | | recall | 1.0000 | 0.0000 | | precision | 1.0000 | 0.0000 | +-------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 4 *****************************************************************************************************
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
-----------------------------------------------------------------------------------------------------
BEST_CONFIG
-----------------------------------------------------------------------------------------------------
{}
-----------------------------------------------------------------------------------------------------
VALIDATION PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4625 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 0.8000 |
| precision | 1.0000 | 0.3875 |
+-------------------+-------------------+------------------+
-----------------------------------------------------------------------------------------------------
TEST PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4750 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 1.0000 |
| precision | 1.0000 | 0.4750 |
+-------------------+-------------------+------------------+
*****************************************************************************************************
Outer Cross validation Fold 5
*****************************************************************************************************
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1334: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
-----------------------------------------------------------------------------------------------------
BEST_CONFIG
-----------------------------------------------------------------------------------------------------
{}
-----------------------------------------------------------------------------------------------------
VALIDATION PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.4125 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 0.4000 |
| precision | 1.0000 | 0.1562 |
+-------------------+-------------------+------------------+
-----------------------------------------------------------------------------------------------------
TEST PERFORMANCE
-----------------------------------------------------------------------------------------------------
+-------------------+-------------------+------------------+
| METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST |
+-------------------+-------------------+------------------+
| accuracy | 1.0000 | 0.5250 |
| balanced_accuracy | 1.0000 | 0.5000 |
| recall | 1.0000 | 1.0000 |
| precision | 1.0000 | 0.5250 |
+-------------------+-------------------+------------------+
*****************************************************************************************************
Finished all outer fold computations.
*****************************************************************************************************
ANALYSIS INFORMATION ================================================================================
Project Folder: /home/runner/work/photonai_graph/photonai_graph/documentation/docs/examples/basic_kernel_pipe_results_2023-02-17_15-48-15,
Computation Time: 2023-02-17 15:48:15.122467 - 2023-02-17 16:09:54.323323
Duration: 0:21:39.200856
Optimized for: accuracy
Hyperparameter Optimizer: grid_search
DUMMY RESULTS =======================================================================================
+-------------------+--+
| PERFORMANCE DUMMY | |
+-------------------+--+
+-------------------+--+
AVERAGE PERFORMANCE ACROSS OUTER FOLDS ==============================================================
+-------------------+---------------+--------------+-----------+----------+
| Metric Name | Training Mean | Training Std | Test Mean | Test Std |
+-------------------+---------------+--------------+-----------+----------+
| accuracy | 1.0 | 0.0 | 0.435 | 0.068191 |
| balanced_accuracy | 1.0 | 0.0 | 0.5 | 0.0 |
| recall | 1.0 | 0.0 | 0.8 | 0.4 |
| precision | 1.0 | 0.0 | 0.37 | 0.189341 |
+-------------------+---------------+--------------+-----------+----------+
BEST HYPERPARAMETER CONFIGURATION ===================================================================
{}
+--------+----------+-------------------+--------+-----------+----------------------------+
| fold # | accuracy | balanced_accuracy | recall | precision | Best Hyperparameter Config |
+--------+----------+-------------------+--------+-----------+----------------------------+
| 1 | 0.4000 | 0.5000 | 1.0000 | 0.4000 | {} |
| 2 | 0.4500 | 0.5000 | 1.0000 | 0.4500 | {} |
| 3 | 0.3250 | 0.5000 | 0.0000 | 0.0000 | {} |
| 4 | 0.4750 | 0.5000 | 1.0000 | 0.4750 | {} |
| 5* | 0.5250 | 0.5000 | 1.0000 | 0.5250 | {} |
+--------+----------+-------------------+--------+-----------+----------------------------+
PHOTONAI 2.3.0 ======================================================================================
Your results are stored in /home/runner/work/photonai_graph/photonai_graph/documentation/docs/examples/basic_kernel_pipe_results_2023-02-17_15-48-15
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='basic_kernel_pipe')