Skip to content
Snippets Groups Projects
Commit 18ae351f authored by Dominique Benielli's avatar Dominique Benielli
Browse files

algo

parent b20911ac
No related branches found
No related tags found
No related merge requests found
Showing
with 1035 additions and 0 deletions
# -*- coding: utf-8 -*-
# import os, sys
#
# MultiviewPlatform documentation build configuration file, created by
# sphinx-quickstart on Mon Jan 29 17:13:09 2018.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('../../multiconfusion'))
sys.path.insert(0, os.path.abspath('../..'))
file_loc = os.path.split(__file__)[0]
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(file_loc), '.')))
import multiconfusion
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'
add_module_names = False
# sys.path.append(os.path.abspath('sphinxext'))
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc',
# 'sphinx.ext.doctest',
# 'sphinx.ext.intersphinx',
# 'sphinx.ext.todo',
# 'nbsphinx',
'sphinx.ext.coverage',
'sphinx.ext.imgmath',
# 'sphinx.ext.mathjax',
# 'sphinx.ext.ifconfig',
# 'sphinx.ext.viewcode',
# 'sphinx.ext.githubpages',
'sphinx.ext.napoleon',
'm2r',]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = {'.rst': 'restructuredtext', '.md': 'markdown'}
# source_suffix = '.rst'
source_suffix = ['.rst', '.md']
# source_parsers = {
# '.md': CommonMarkParser,
# }
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'MultiConfusion'
copyright = u'2019, Dominique Benielli'
author = u'Dominique Benielli'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u'0.0.0'
# The full version, including alpha/beta/rc tags.
release = u'0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = []
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
# html_theme = 'sphinx_rtd_theme'
html_theme = 'classic'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []
# -- Options for HTMLHelp output ------------------------------------------
# Output file base name for HTML help builder.
htmlhelp_basename = 'MultiConfusiondoc'
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'MultiConfusion.tex', u'MultiConfusion Documentation',
u'Dominique Benielli', 'manual'),
]
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'confusion', u'MultiConfusion Documentation',
[author], 1)
]
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'MultiConfusion', u'MultiConfusion Documentation',
author, 'MultiConfusion', 'One line description of project.',
'Miscellaneous'),
]
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'https://docs.python.org/': None}
# def setup(app):
# app.add_config_value('recommonmark_config', {
# 'auto_toc_tree_section': 'Contents',
# }, True)
# app.add_transform(AutoStructify)
Credits
=======
**multiconfusion* is developped by the
`development team <https://developpement.lis-lab.fr/>`_ of the
`LIS <http://www.lis-lab.fr/>`_.
If you use **multiconfusion** in a scientific publication, please cite the
following paper::
@InProceedings{Koco:2011:BAM,
author={Ko\c{c}o, Sokol and Capponi, C{\'e}cile},
editor={Gunopulos, Dimitrios and Hofmann, Thomas and Malerba, Donato
and Vazirgiannis, Michalis},
title={A Boosting Approach to Multiview Classification with Cooperation},
booktitle={Proceedings of the 2011 European Conference on Machine Learning
and Knowledge Discovery in Databases - Volume Part II},
year={2011},
location={Athens, Greece},
publisher={Springer-Verlag},
address={Berlin, Heidelberg},
pages={209--228},
numpages = {20},
isbn={978-3-642-23783-6}
url={https://link.springer.com/chapter/10.1007/978-3-642-23783-6_14},
keywords={boosting, classification, multiview learning,
supervised learning},
}
References
----------
* Sokol Koço, Cécile Capponi,
`"A boosting approach to multiview classification with cooperation"
<https://link.springer.com/chapter/10.1007/978-3-642-23783-6_14>`_,
Proceedings of the 2011 European Conference on Machine Learning (ECML),
Athens, Greece, pp.209-228, 2011, Springer-Verlag.
* Sokol Koço,
`"Tackling the uneven views problem with cooperation based ensemble
learning methods" <http://www.theses.fr/en/2013AIXM4101>`_,
PhD Thesis, Aix-Marseille Université, 2013.
Copyright
---------
Université d'Aix Marseille (AMU) -
Centre National de la Recherche Scientifique (CNRS) -
Université de Toulon (UTLN).
Copyright © 2019-2020 AMU, CNRS, UTLN
License
-------
**multiconfusion** is free software: you can redistribute it and/or modify
it under the terms of the **GNU Lesser General Public License** as published by
the Free Software Foundation, either **version 3** of the License, or
(at your option) any later version.
.. Multiconfusion documentation master file, created by
sphinx-quickstart on Mon Sep 2 12:12:08 2019.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Mucumbo's documentation!
===================================
**multiconfusion** is a Python package implementing boost algorithms for
machine learning with multimodal with confusion matrix data.
It is compatible with `scikit-learn <http://scikit-learn.org/>`_, a popular
package for machine learning in Python.
.. toctree::
:maxdepth: 2
:caption: Contents:
install_devel
reference/index
credits
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
Installation and development
============================
Dependencies
------------
**multiconfusion** works with **Python 3.5 or later**.
**multiconfusion** depends on **scikit-learn** (version >= 0.19).
Optionally, **matplotlib** is required when running the examples.
Installation
------------
**multiconfusion** is
`available on PyPI <https://pypi.org/project/multimodalboost/>`_
and can be installed using **pip**::
pip install multiconfusion
If you prefer to install directly from the **source code**, clone the **Git**
repository of the project and run the **setup.py** file with the following
commands::
git clone git@gitlab.lis-lab.fr:dev/multiconfusion.git
cd multimodalboost
python setup.py install
or alternatively use **pip**::
pip install git+https://gitlab.lis-lab.fr/dev/multiconfusion.git
Development
-----------
The development of multimodalboost follows the guidelines provided by the
scikit-learn community.
Refer to the `Developer's Guide <http://scikit-learn.org/stable/developers>`_
of the scikit-learn project for more details.
Source code
-----------
You can get the **source code** from the **Git** repository of the project::
git clone git@gitlab.lis-lab.fr:dev/multiconfusion.git
Testing
-------
**pytest** and **pytest-cov** are required to run the **test suite** with::
pytest-3
A code coverage report is displayed in the terminal when running the tests.
An HTML version of the report is also stored in the directory **htmlcov**.
Generating the documentation
----------------------------
The generation of the documentation requires **sphinx**, **sphinx-gallery**,
**numpydoc** and **matplotlib** and can be run with::
python setup.py build_sphinx
ou::
cd doc
sphinx-build -b html . ./build
The resulting files are stored in the directory **build/sphinx/html**.
Welcome to Multi-View Mu Cumbo's reference!
===========================================
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
multiconfusion
==============
.. toctree::
:maxdepth: 3
.. automodule:: multiconfusion.cumbo
:members:
:inherited-members:
datasets
# -*- coding: utf-8 -*-
from datetime import date
import os
import sys
sys.path.insert(0, os.path.abspath('../metriclearning'))
sys.path.insert(0, os.path.abspath('../..'))
sys.path.insert(0, os.path.abspath("."))
sys.path.append(os.path.join(os.path.dirname(__name__), '..'))
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'sphinxext'))
import metriclearning
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
# needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.imgmath',
'numpydoc',
# 'sphinx_gallery.gen_gallery'
]
# Add any paths that contain templates here, relative to this directory.
# templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
source_encoding = 'utf-8'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = 'metriclearning'
author = 'Dominique Benielli'
copyright = '2017-{}, LIS UMR 7020'.format(date.today().year)
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = metriclearning.__version__
# The full version, including alpha/beta/rc tags.
release = metriclearning.__version__
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
# language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = []
# The reST default role (used for this markup: `text`) to use for all
# documents.
# default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []
# If true, keep warnings as "system message" paragraphs in the built documents.
# keep_warnings = False
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'nature'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
# html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
# html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
# html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
# html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
# html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
# html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
# html_extra_path = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
# html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
# html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}
# If false, no module index is generated.
# html_domain_indices = True
# If false, no index is generated.
# html_use_index = True
# If true, the index is split into individual pages for each letter.
# html_split_index = False
# If true, links to the reST sources are added to the pages.
# html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
# html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
# html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None
# Output file base name for HTML help builder.
htmlhelp_basename = '{}doc'.format(project)
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
'papersize': 'a4paper',
# The font size ('10pt', '11pt' or '12pt').
'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
# 'preamble': '',
# Latex figure (float) alignment
'figure_align': 'htbp'}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, '{}.tex'.format(project), '{} Documentation'.format(project),
author, 'manual')]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
# latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
# latex_use_parts = False
# If true, show page references after internal links.
# latex_show_pagerefs = False
# If true, show URL addresses after external links.
# latex_show_urls = False
# Documents to append as an appendix to all manuals.
# latex_appendices = []
# If false, no module index is generated.
# latex_domain_indices = True
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, project, '{} Documentation'.format(project),
[author], 1)
]
# If true, show URL addresses after external links.
# man_show_urls = False
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, project, '{} Documentation'.format(project), author, project,
'Multi-View Metric Learning in Vector-Valued Kernel Spaces for machine learning.',
'Miscellaneous')]
# Documents to append as an appendix to all manuals.
# texinfo_appendices = []
# If false, no module index is generated.
# texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
# texinfo_show_urls = 'footnote'
# If true, do not generate a @detailmenu in the "Top" node's menu.
# texinfo_no_detailmenu = False
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
'sklearn': ('http://scikit-learn.org/stable', None)
}
numpydoc_show_class_members = False
sphinx_gallery_conf = {
'doc_module': (project,),
'backreferences_dir': 'backreferences',
# path to your examples scripts
'examples_dirs': '../examples',
# path where to save gallery generated examples
'gallery_dirs': 'auto_examples'}
# Generate the plots for the gallery
plot_gallery = 'True'
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.metrics.pairwise import rbf_kernel
from metriclearning.mvml import MVML
from metriclearning.datasets.data_sample import DataSample
from metriclearning.tests.datasets.get_dataset_path import get_dataset_path
import pickle
"""
Demonstration on how MVML (in file mvml.py) is intended to be used with very simple simulated dataset
Demonstration uses scikit-learn for retrieving datasets and for calculating rbf kernel function, see
http://scikit-learn.org/stable/
"""
np.random.seed(4)
# =========== create a simple dataset ============
n_tot = 200
half = int(n_tot/2)
n_tr = 120
# create a bit more data than needed so that we can take "half" amount of samples for each class
X0, y0 = datasets.make_moons(n_samples=n_tot+2, noise=0.3, shuffle=False)
X1, y1 = datasets.make_circles(n_samples=n_tot+2, noise=0.1, shuffle=False)
# make multi-view correspondence (select equal number of samples for both classes and order the data same way
# in both views)
yinds0 = np.append(np.where(y0 == 0)[0][0:half], np.where(y0 == 1)[0][0:half])
yinds1 = np.append(np.where(y1 == 0)[0][0:half], np.where(y1 == 1)[0][0:half])
X0 = X0[yinds0, :]
X1 = X1[yinds1, :]
Y = np.append(np.zeros(half)-1, np.ones(half)) # labels -1 and 1
# show data
# =========== create a simple dataset ============
n_tot = 200
half = int(n_tot/2)
n_tr = 120
# create a bit more data than needed so that we can take "half" amount of samples for each class
X0, y0 = datasets.make_moons(n_samples=n_tot+2, noise=0.3, shuffle=False)
X1, y1 = datasets.make_circles(n_samples=n_tot+2, noise=0.1, shuffle=False)
# make multi-view correspondence (select equal number of samples for both classes and order the data same way
# in both views)
yinds0 = np.append(np.where(y0 == 0)[0][0:half], np.where(y0 == 1)[0][0:half])
yinds1 = np.append(np.where(y1 == 0)[0][0:half], np.where(y1 == 1)[0][0:half])
X0 = X0[yinds0, :]
X1 = X1[yinds1, :]
Y = np.append(np.zeros(half)-1, np.ones(half)) # labels -1 and 1
# show data
plt.figure(1)
plt.subplot(121)
plt.scatter(X0[:, 0], X0[:, 1], c=Y)
plt.title("all data, view 1")
plt.subplot(122)
plt.scatter(X1[:, 0], X1[:, 1], c=Y)
plt.title("all data, view 2")
# plt.show()
# shuffle
order = np.random.permutation(n_tot)
X0 = X0[order, :]
X1 = X1[order, :]
Y = Y[order]
# make kernel dictionaries
kernel_dict = {}
test_kernel_dict = {}
kernel_dict[0] = rbf_kernel(X0[0:n_tr, :])
kernel_dict[1] = rbf_kernel(X1[0:n_tr, :])
test_kernel_dict[0] = rbf_kernel(X0[n_tr:n_tot, :], X0[0:n_tr, :])
test_kernel_dict[1] = rbf_kernel(X1[n_tr:n_tot, :], X1[0:n_tr, :])
# input_x = get_dataset_path("input_x_dic.pkl")
# f = open(input_x, "wb")
# pickle.dump(input_x, f)
d= DataSample(kernel_dict)
a = d.data
# =========== use MVML in classifying the data ============
# demo on how the code is intended to be used; parameters are not cross-validated, just picked some
# mvml = MVML(kernel_dict, Y[0:n_tr], [0.1, 1], nystrom_param=0.2)
mvml = MVML( [0.1, 1], nystrom_param=0.2)
A1, g1, w1 = mvml.fit(a, Y[0:n_tr])
# with approximation
# mvml = MVML(kernel_dict, Y[0:n_tr], [0.1, 1], nystrom_param=1) # without approximation
A1, g1, w1 = mvml.learn_mvml() # default: learn A, don't learn w (learn_A=1, learn_w=0)
pred1 = np.sign(mvml.predict_mvml(test_kernel_dict, g1, w1)) # take sign for classification result
A2, g2, w2 = mvml.learn_mvml(learn_A=2, learn_w=1) # learn sparse A and learn w
pred2 = np.sign(mvml.predict_mvml(test_kernel_dict, g2, w2))
# print(w2)
A3, g3, w3 = mvml.learn_mvml(learn_A=3) # use MVML_Cov, don't learn w
pred3 = np.sign(mvml.predict_mvml(test_kernel_dict, g3, w3))
A4, g4, w4 = mvml.learn_mvml(learn_A=4) # use MVML_I, don't learn w
pred4 = np.sign(mvml.predict_mvml(test_kernel_dict, g4, w4))
# =========== show results ============
# accuracies
acc1 = accuracy_score(Y[n_tr:n_tot], pred1)
acc2 = accuracy_score(Y[n_tr:n_tot], pred2)
acc3 = accuracy_score(Y[n_tr:n_tot], pred3)
acc4 = accuracy_score(Y[n_tr:n_tot], pred4)
# display obtained accuracies
print("MVML: ", acc1)
print("MVMLsparse: ", acc2)
print("MVML_Cov: ", acc3)
print("MVML_I: ", acc4)
# plot data and some classification results
plt.figure(2)
plt.subplot(341)
plt.scatter(X0[n_tr:n_tot, 0], X0[n_tr:n_tot, 1], c=Y[n_tr:n_tot])
plt.title("orig. view 1")
plt.subplot(342)
plt.scatter(X1[n_tr:n_tot, 0], X1[n_tr:n_tot, 1], c=Y[n_tr:n_tot])
plt.title("orig. view 2")
pred1[np.where(pred1[:, 0] != Y[n_tr:n_tot])] = 0
pred1 = pred1.reshape((pred1.shape[0]))
plt.subplot(343)
plt.scatter(X0[n_tr:n_tot, 0], X0[n_tr:n_tot, 1], c=pred1)
plt.title("MVML view 1")
plt.subplot(344)
plt.scatter(X1[n_tr:n_tot, 0], X1[n_tr:n_tot, 1], c=pred1)
plt.title("MVML view 2")
pred2[np.where(pred2[:, 0] != Y[n_tr:n_tot])] = 0
pred2 = pred2.reshape((pred2.shape[0]))
plt.subplot(345)
plt.scatter(X0[n_tr:n_tot, 0], X0[n_tr:n_tot, 1], c=pred2)
plt.title("MVMLsparse view 1")
plt.subplot(346)
plt.scatter(X1[n_tr:n_tot, 0], X1[n_tr:n_tot, 1], c=pred2)
plt.title("MVMLsparse view 2")
pred3[np.where(pred3[:, 0] != Y[n_tr:n_tot])] = 0
pred3 = pred3.reshape((pred3.shape[0]))
plt.subplot(347)
plt.scatter(X0[n_tr:n_tot, 0], X0[n_tr:n_tot, 1], c=pred3)
plt.title("MVML_Cov view 1")
plt.subplot(348)
plt.scatter(X1[n_tr:n_tot, 0], X1[n_tr:n_tot, 1], c=pred3)
plt.title("MVML_Cov view 2")
pred4[np.where(pred4[:, 0] != Y[n_tr:n_tot])] = 0
pred4 = pred4.reshape((pred4.shape[0]))
plt.subplot(349)
plt.scatter(X0[n_tr:n_tot, 0], X0[n_tr:n_tot, 1], c=pred4)
plt.title("MVML_I view 1")
plt.subplot(3,4,10)
plt.scatter(X1[n_tr:n_tot, 0], X1[n_tr:n_tot, 1], c=pred4)
plt.title("MVML_I view 2")
# plt.figure(3)
# plt.spy(A2)
# plt.title("sparse learned A")
plt.show()
.. metriclearning documentation master file, created by
sphinx-quickstart on Mon Sep 2 12:12:08 2019.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to metriclearning's documentation!
==========================================
.. toctree::
:maxdepth: 2
:caption: Contents:
reference/index
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
datasets
========
.. automodule:: metriclearning.datasets.base
:members:
:undoc-members:
:show-inheritance:
.. automodule:: metriclearning.datasets.data_sample
:members:
:undoc-members:
:show-inheritance:
Welcome to Multi-View metriclearning's reference!
=================================================
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
lpMKL
=====
.. automodule:: metriclearning.lpMKL
:members: MKL
:undoc-members:
:show-inheritance:
abstract mkernel
================
.. automodule:: metriclearning.mkernel
:members: MKernel
:undoc-members:
:show-inheritance:
metriclearning
==============
.. toctree::
:maxdepth: 3
mkernel
mvml
lpMKL
datasets
MVML of metriclearning
======================
.. automodule:: metriclearning.mvml
:members: MVML
:undoc-members:
:show-inheritance:
import numpy as np
from sklearn import datasets
from sklearn.metrics.pairwise import rbf_kernel
from metriclearning.mvml import MVML
from metriclearning.lpMKL import MKL
from metriclearning.datasets.data_sample import DataSample
from metriclearning.tests.datasets.get_dataset_path import get_dataset_path
import pickle
np.random.seed(4)
# =========== create a simple dataset ============
n_tot = 200
half = int(n_tot/2)
n_tr = 120
# create a bit more data than needed so that we can take "half" amount of samples for each class
X0, y0 = datasets.make_moons(n_samples=n_tot+2, noise=0.3, shuffle=False)
X1, y1 = datasets.make_circles(n_samples=n_tot+2, noise=0.1, shuffle=False)
# make multi-view correspondence (select equal number of samples for both classes and order the data same way
# in both views)
yinds0 = np.append(np.where(y0 == 0)[0][0:half], np.where(y0 == 1)[0][0:half])
yinds1 = np.append(np.where(y1 == 0)[0][0:half], np.where(y1 == 1)[0][0:half])
X0 = X0[yinds0, :]
X1 = X1[yinds1, :]
Y = np.append(np.zeros(half)-1, np.ones(half)) # labels -1 and 1
n_tot = 200
half = int(n_tot/2)
n_tr = 120
# create a bit more data than needed so that we can take "half" amount of samples for each class
X0, y0 = datasets.make_moons(n_samples=n_tot+2, noise=0.3, shuffle=False)
X1, y1 = datasets.make_circles(n_samples=n_tot+2, noise=0.1, shuffle=False)
# make multi-view correspondence (select equal number of samples for both classes and order the data same way
# in both views)
yinds0 = np.append(np.where(y0 == 0)[0][0:half], np.where(y0 == 1)[0][0:half])
yinds1 = np.append(np.where(y1 == 0)[0][0:half], np.where(y1 == 1)[0][0:half])
X0 = X0[yinds0, :]
X1 = X1[yinds1, :]
Y = np.append(np.zeros(half)-1, np.ones(half)) # labels -1 and 1
# shuffle
order = np.random.permutation(n_tot)
X0 = X0[order, :]
X1 = X1[order, :]
Y = Y[order]
# make kernel dictionaries
kernel_dict = {}
test_kernel_dict = {}
kernel_dict[0] = rbf_kernel(X0[0:n_tr, :])
kernel_dict[1] = rbf_kernel(X1[0:n_tr, :])
test_kernel_dict[0] = rbf_kernel(X0[n_tr:n_tot, :], X0[0:n_tr, :])
test_kernel_dict[1] = rbf_kernel(X1[n_tr:n_tot, :], X1[0:n_tr, :])
d= DataSample(kernel_dict)
a = d.data
# np.save(input_x, kernel_dict)
# np.save(input_y, Y)
# f = open(input_x, "wb")
# pickle.dump(input_x, f)
#input_x = get_dataset_path("input_x_dic.pkl")
#f = open(input_x, "r")
#dicoc = pickle.load(f)
# pickle.dump(kernel_dict, f)
#f.close()
# =========== use MVML in classifying the data ============
# demo on how the code is intended to be used; parameters are not cross-validated, just picked some
# mvml = MVML(kernel_dict, Y[0:n_tr], [0.1, 1], nystrom_param=0.2)
mvml = MVML( [0.1, 1], nystrom_param=0.2)
mvml.fit(a, Y[0:n_tr])
print("x shape", mvml.X_.shape)
print("x shape int",mvml.X_.shapes_int)
dd = DataSample(test_kernel_dict)
X_test = dd.data
red1 = mvml.predict(X_test)
mkl = MKL(lmbda=0.1)
mkl.fit(kernel_dict,Y[0:n_tr] )
mkl.predict(X_test)
#red1 = np.sign(mvml.predict_mvml(test_kernel_dict, g1, w1))
API Documentation
=================
multimodalboost.mumbo
---------------------
.. automodule:: multimodalboost.mumbo
:members:
:inherited-members:
File added
File added
doc/docmumbo/auto_examples/images/sphx_glr_plot_2_views_2_classes_001.png

84.1 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment