Graph Kernel SVM theta 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 example data
In [2]:
Copied!
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=200)
y = get_random_labels(l_type="regression", number_of_labels=200)
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=200)
y = get_random_labels(l_type="regression", number_of_labels=200)
Design example 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=['mean_absolute_error'],
best_config_metric='mean_absolute_error')
my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95))
my_pipe.add(PipelineElement('GrakelAdapter'))
my_pipe.add(PipelineElement('GrakelTransformer', transformation='SvmTheta'))
my_pipe.add(PipelineElement("SVR", kernel='precomputed'))
my_pipe = Hyperpipe('basic_kernel_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='grid_search',
metrics=['mean_absolute_error'],
best_config_metric='mean_absolute_error')
my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95))
my_pipe.add(PipelineElement('GrakelAdapter'))
my_pipe.add(PipelineElement('GrakelTransformer', transformation='SvmTheta'))
my_pipe.add(PipelineElement("SVR", kernel='precomputed'))
Fit pipeline to example data
In [4]:
Copied!
my_pipe.fit(X, y)
my_pipe.fit(X, y)
===================================================================================================== PHOTONAI ANALYSIS: basic_kernel_pipe ===================================================================================================== 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 | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2521 | 0.2559 | +---------------------+-------------------+------------------+ ----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2526 | 0.2433 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 2 ***************************************************************************************************** ----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2601 | 0.2654 | +---------------------+-------------------+------------------+ ----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2606 | 0.2114 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 3 ***************************************************************************************************** ----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2492 | 0.2533 | +---------------------+-------------------+------------------+ ----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2497 | 0.2545 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 4 ***************************************************************************************************** ----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2486 | 0.2507 | +---------------------+-------------------+------------------+ ----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2488 | 0.2598 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 5 ***************************************************************************************************** ----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2383 | 0.2397 | +---------------------+-------------------+------------------+ ----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2385 | 0.3027 | +---------------------+-------------------+------------------+ ***************************************************************************************************** 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_16-11-00, Computation Time: 2023-02-17 16:11:00.542994 - 2023-02-17 16:12:01.203944 Duration: 0:01:00.660950 Optimized for: mean_absolute_error Hyperparameter Optimizer: grid_search DUMMY RESULTS ======================================================================================= +-------------------+--+ | PERFORMANCE DUMMY | | +-------------------+--+ +-------------------+--+ AVERAGE PERFORMANCE ACROSS OUTER FOLDS ============================================================== +---------------------+---------------+--------------+-----------+----------+ | Metric Name | Training Mean | Training Std | Test Mean | Test Std | +---------------------+---------------+--------------+-----------+----------+ | mean_absolute_error | 0.250037 | 0.007108 | 0.254335 | 0.029437 | +---------------------+---------------+--------------+-----------+----------+ BEST HYPERPARAMETER CONFIGURATION =================================================================== {} +--------+---------------------+----------------------------+ | fold # | mean_absolute_error | Best Hyperparameter Config | +--------+---------------------+----------------------------+ | 1 | 0.2433 | {} | | 2* | 0.2114 | {} | | 3 | 0.2545 | {} | | 4 | 0.2598 | {} | | 5 | 0.3027 | {} | +--------+---------------------+----------------------------+ 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_16-11-00 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')