This commit is contained in:
Morten Hjorth-Jensen
2025-09-12 21:14:24 +02:00
16 changed files with 1668 additions and 20 deletions
BIN
View File
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,764 @@
<!DOCTYPE html>
<html lang="en" data-content_root="../../../../../../" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A guide to masked arrays in NumPy &#8212; Applied Data Analysis and Machine Learning</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="../../../../../../_static/styles/theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="../../../../../../_static/styles/bootstrap.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="../../../../../../_static/styles/pydata-sphinx-theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="../../../../../../_static/vendor/fontawesome/6.5.2/css/all.min.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../_static/vendor/fontawesome/6.5.2/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../_static/vendor/fontawesome/6.5.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../_static/vendor/fontawesome/6.5.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/styles/sphinx-book-theme.css?v=eba8b062" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/togglebutton.css?v=13237357" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/mystnb.8ecb98da25f57f5357bf6f572d296f466b2cfe2517ffebfabe82451661e28f02.css?v=6644e6bb" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/sphinx-thebe.css?v=4fa983c6" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/sphinx-design.min.css?v=95c83b7e" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../../../../../../_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b" />
<link rel="preload" as="script" href="../../../../../../_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b" />
<script src="../../../../../../_static/vendor/fontawesome/6.5.2/js/all.min.js?digest=dfe6caa3a7d634c4db9b"></script>
<script src="../../../../../../_static/documentation_options.js?v=9eb32ce0"></script>
<script src="../../../../../../_static/doctools.js?v=9a2dae69"></script>
<script src="../../../../../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../../../../../../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../../../../../../_static/copybutton.js?v=f281be69"></script>
<script src="../../../../../../_static/scripts/sphinx-book-theme.js?v=887ef09a"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="../../../../../../_static/togglebutton.js?v=4a39c7ea"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script src="../../../../../../_static/design-tabs.js?v=f930bc37"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
<script async="async" src="../../../../../../_static/sphinx-thebe.js?v=c100c467"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
<script>DOCUMENTATION_OPTIONS.pagename = '.venv/lib/python3.13/site-packages/numpy/ma/README';</script>
<link rel="index" title="Index" href="../../../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../../../search.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<input type="checkbox"
class="sidebar-toggle"
id="pst-primary-sidebar-checkbox"/>
<label class="overlay overlay-primary" for="pst-primary-sidebar-checkbox"></label>
<input type="checkbox"
class="sidebar-toggle"
id="pst-secondary-sidebar-checkbox"/>
<label class="overlay overlay-secondary" for="pst-secondary-sidebar-checkbox"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="../../../../../../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search this book..."
aria-label="Search this book..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="../../../../../../intro.html">
<img src="../../../../../../_static/logo.png" class="logo__image only-light" alt="Applied Data Analysis and Machine Learning - Home"/>
<script>document.write(`<img src="../../../../../../_static/logo.png" class="logo__image only-dark" alt="Applied Data Analysis and Machine Learning - Home"/>`);</script>
</a></div>
<div class="sidebar-primary-item">
<script>
document.write(`
<button class="btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
`);
</script></div>
<div class="sidebar-primary-item"><nav class="bd-links bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<ul class="nav bd-sidenav bd-sidenav__home-link">
<li class="toctree-l1">
<a class="reference internal" href="../../../../../../intro.html">
Applied Data Analysis and Machine Learning
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">About the course</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../schedule.html">Course setting</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../teachers.html">Teachers and Grading</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../textbooks.html">Textbooks</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Review of Statistics with Resampling Techniques and Linear Algebra</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../statistics.html">1. Elements of Probability Theory and Statistical Data Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../linalg.html">2. Linear Algebra, Handling of Arrays and more Python Features</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">From Regression to Support Vector Machines</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter1.html">3. Linear Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter2.html">4. Ridge and Lasso Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter3.html">5. Resampling Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter4.html">6. Logistic Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapteroptimization.html">7. Optimization, the central part of any Machine Learning algortithm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter5.html">8. Support Vector Machines, overarching aims</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Decision Trees, Ensemble Methods and Boosting</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter6.html">9. Decision trees, overarching aims</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter7.html">10. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Dimensionality Reduction</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter8.html">11. Basic ideas of the Principal Component Analysis (PCA)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../clustering.html">12. Clustering and Unsupervised Learning</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Deep Learning Methods</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter9.html">13. Neural networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter10.html">14. Building a Feed Forward Neural Network</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter11.html">15. Solving Differential Equations with Deep Learning</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter12.html">16. Convolutional Neural Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter13.html">17. Recurrent neural networks: Overarching view</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Weekly material, notes and exercises</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek34.html">Exercises week 34</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week34.html">Week 34: Introduction to the course, Logistics and Practicalities</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek35.html">Exercises week 35</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week35.html">Week 35: From Ordinary Linear Regression to Ridge and Lasso Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek36.html">Exercises week 36</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week36.html">Week 36: Linear Regression and Gradient descent</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek37.html">Exercises week 37</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week37.html">Week 37: Gradient descent methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek38.html">Exercises week 38</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek39.html">Exercises week 39</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Projects</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../project1.html">Project 1 on Machine Learning, deadline October 6 (midnight), 2025</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</button></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<div class="dropdown dropdown-download-buttons">
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
<i class="fas fa-download"></i>
</button>
<ul class="dropdown-menu">
<li><a href="../../../../../../_sources/.venv/lib/python3.13/site-packages/numpy/ma/README.rst" target="_blank"
class="btn btn-sm btn-download-source-button dropdown-item"
title="Download source file"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file"></i>
</span>
<span class="btn__text-container">.rst</span>
</a>
</li>
<li>
<button onclick="window.print()"
class="btn btn-sm btn-download-pdf-button dropdown-item"
title="Print to PDF"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file-pdf"></i>
</span>
<span class="btn__text-container">.pdf</span>
</button>
</li>
</ul>
</div>
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<script>
document.write(`
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto"></i>
</button>
`);
</script>
<script>
document.write(`
<button class="btn btn-sm pst-navbar-icon search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button>
`);
</script>
<button class="sidebar-toggle secondary-toggle btn btn-sm" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</button>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>A guide to masked arrays in NumPy</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#history">History</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#main-differences">Main differences</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#new-features">New features</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#using-the-new-package-with-numpy-core-ma">Using the new package with numpy.core.ma</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#using-maskedarray-with-matplotlib">Using maskedarray with matplotlib</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#masked-records">Masked records</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#optimizing-maskedarray">Optimizing maskedarray</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#should-masked-arrays-be-filled-before-processing-or-not">Should masked arrays be filled before processing or not?</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#thanks">Thanks</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#revision-notes">Revision notes</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="a-guide-to-masked-arrays-in-numpy">
<h1><a class="toc-backref" href="#id1" role="doc-backlink">A guide to masked arrays in NumPy</a><a class="headerlink" href="#a-guide-to-masked-arrays-in-numpy" title="Link to this heading">#</a></h1>
<nav class="contents" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#a-guide-to-masked-arrays-in-numpy" id="id1">A guide to masked arrays in NumPy</a></p>
<ul>
<li><p><a class="reference internal" href="#history" id="id2">History</a></p></li>
<li><p><a class="reference internal" href="#main-differences" id="id3">Main differences</a></p></li>
<li><p><a class="reference internal" href="#new-features" id="id4">New features</a></p></li>
<li><p><a class="reference internal" href="#using-the-new-package-with-numpy-core-ma" id="id5">Using the new package with numpy.core.ma</a></p></li>
<li><p><a class="reference internal" href="#using-maskedarray-with-matplotlib" id="id6">Using maskedarray with matplotlib</a></p></li>
<li><p><a class="reference internal" href="#masked-records" id="id7">Masked records</a></p></li>
<li><p><a class="reference internal" href="#optimizing-maskedarray" id="id8">Optimizing maskedarray</a></p></li>
<li><p><a class="reference internal" href="#should-masked-arrays-be-filled-before-processing-or-not" id="id9">Should masked arrays be filled before processing or not?</a></p></li>
<li><p><a class="reference internal" href="#thanks" id="id10">Thanks</a></p></li>
<li><p><a class="reference internal" href="#revision-notes" id="id11">Revision notes</a></p></li>
</ul>
</li>
</ul>
</nav>
<p>See <a class="reference external" href="http://www.scipy.org/scipy/numpy/wiki/MaskedArray">http://www.scipy.org/scipy/numpy/wiki/MaskedArray</a> (dead link)
for updates of this document.</p>
<section id="history">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">History</a><a class="headerlink" href="#history" title="Link to this heading">#</a></h2>
<p>As a regular user of MaskedArray, I (Pierre G.F. Gerard-Marchant) became
increasingly frustrated with the subclassing of masked arrays (even if
I can only blame my inexperience). I needed to develop a class of arrays
that could store some additional information along with numerical values,
while keeping the possibility for missing data (picture storing a series
of dates along with measurements, what would later become the <a class="reference external" href="http://projects.scipy.org/scipy/scikits/wiki/TimeSeries">TimeSeries
Scikit</a>
(dead link).</p>
<p>I started to implement such a class, but then quickly realized that
any additional information disappeared when processing these subarrays
(for example, adding a constant value to a subarray would erase its
dates). I ended up writing the equivalent of <em>numpy.core.ma</em> for my
particular class, ufuncs included. Everything went fine until I needed to
subclass my new class, when more problems showed up: some attributes of
the new subclass were lost during processing. I identified the culprit as
MaskedArray, which returns masked ndarrays when I expected masked
arrays of my class. I was preparing myself to rewrite <em>numpy.core.ma</em>
when I forced myself to learn how to subclass ndarrays. As I became more
familiar with the <em>__new__</em> and <em>__array_finalize__</em> methods,
I started to wonder why masked arrays were objects, and not ndarrays,
and whether it wouldnt be more convenient for subclassing if they did
behave like regular ndarrays.</p>
<p>The new <em>maskedarray</em> is what I eventually come up with. The
main differences with the initial <em>numpy.core.ma</em> package are
that MaskedArray is now a subclass of <em>ndarray</em> and that the
<em>_data</em> section can now be any subclass of <em>ndarray</em>. Apart from a
couple of issues listed below, the behavior of the new MaskedArray
class reproduces the old one. Initially the <em>maskedarray</em>
implementation was marginally slower than <em>numpy.ma</em> in some areas,
but work is underway to speed it up; the expectation is that it can be
made substantially faster than the present <em>numpy.ma</em>.</p>
<p>Note that if the subclass has some special methods and
attributes, they are not propagated to the masked version:
this would require a modification of the <em>__getattribute__</em>
method (first trying <em>ndarray.__getattribute__</em>, then trying
<em>self._data.__getattribute__</em> if an exception is raised in the first
place), which really slows things down.</p>
</section>
<section id="main-differences">
<h2><a class="toc-backref" href="#id3" role="doc-backlink">Main differences</a><a class="headerlink" href="#main-differences" title="Link to this heading">#</a></h2>
<blockquote>
<div><ul class="simple">
<li><p>The <em>_data</em> part of the masked array can be any subclass of ndarray (but not recarray, cf below).</p></li>
<li><p><em>fill_value</em> is now a property, not a function.</p></li>
<li><p>in the majority of cases, the mask is forced to <em>nomask</em> when no value is actually masked. A notable exception is when a masked array (with no masked values) has just been unpickled.</p></li>
<li><p>I got rid of the <em>share_mask</em> flag, I never understood its purpose.</p></li>
<li><p><em>put</em>, <em>putmask</em> and <em>take</em> now mimic the ndarray methods, to avoid unpleasant surprises. Moreover, <em>put</em> and <em>putmask</em> both update the mask when needed. * if <em>a</em> is a masked array, <em>bool(a)</em> raises a <em>ValueError</em>, as it does with ndarrays.</p></li>
<li><p>in the same way, the comparison of two masked arrays is a masked array, not a boolean</p></li>
<li><p><em>filled(a)</em> returns an array of the same subclass as <em>a._data</em>, and no test is performed on whether it is contiguous or not.</p></li>
<li><p>the mask is always printed, even if its <em>nomask</em>, which makes things easy (for me at least) to remember that a masked array is used.</p></li>
<li><p><em>cumsum</em> works as if the <em>_data</em> array was filled with 0. The mask is preserved, but not updated.</p></li>
<li><p><em>cumprod</em> works as if the <em>_data</em> array was filled with 1. The mask is preserved, but not updated.</p></li>
</ul>
</div></blockquote>
</section>
<section id="new-features">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">New features</a><a class="headerlink" href="#new-features" title="Link to this heading">#</a></h2>
<p>This list is non-exhaustive…</p>
<blockquote>
<div><ul class="simple">
<li><p>the <em>mr_</em> function mimics <em>r_</em> for masked arrays.</p></li>
<li><p>the <em>anom</em> method returns the anomalies (deviations from the average)</p></li>
</ul>
</div></blockquote>
</section>
<section id="using-the-new-package-with-numpy-core-ma">
<h2><a class="toc-backref" href="#id5" role="doc-backlink">Using the new package with numpy.core.ma</a><a class="headerlink" href="#using-the-new-package-with-numpy-core-ma" title="Link to this heading">#</a></h2>
<p>I tried to make sure that the new package can understand old masked
arrays. Unfortunately, theres no upward compatibility.</p>
<p>For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy.core.ma</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">old_ma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">maskedarray</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">new_ma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">old_ma</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">],</span> <span class="n">mask</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">array(data =</span>
<span class="go"> [ 1 2 999999 4 5],</span>
<span class="go"> mask =</span>
<span class="go"> [False False True False False],</span>
<span class="go"> fill_value=999999)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">new_ma</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">],</span> <span class="n">mask</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span>
<span class="go">array(data = [1 2 -- 4 5],</span>
<span class="go"> mask = [False False True False False],</span>
<span class="go"> fill_value=999999)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">==</span><span class="n">y</span>
<span class="go">array(data =</span>
<span class="go"> [True True True True True],</span>
<span class="go"> mask =</span>
<span class="go"> [False False True False False],</span>
<span class="go"> fill_value=?)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">old_ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="n">new_ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">array([True, True, True, True, True])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">old_ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">==</span> <span class="n">new_ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">array([True, True, False, True, True])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">old_ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</section>
<section id="using-maskedarray-with-matplotlib">
<h2><a class="toc-backref" href="#id6" role="doc-backlink">Using maskedarray with matplotlib</a><a class="headerlink" href="#using-maskedarray-with-matplotlib" title="Link to this heading">#</a></h2>
<p>Starting with matplotlib 0.91.2, the masked array importing will work with
the maskedarray branch) as well as with earlier versions.</p>
<p>By default matplotlib still uses numpy.ma, but there is an rcParams setting
that you can use to select maskedarray instead. In the matplotlibrc file
you will find:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1">#maskedarray : False # True to use external maskedarray module</span>
<span class="c1"># instead of numpy.ma; this is a temporary #</span>
<span class="n">setting</span> <span class="k">for</span> <span class="n">testing</span> <span class="n">maskedarray</span><span class="o">.</span>
</pre></div>
</div>
<p>Uncomment and set to True to select maskedarray everywhere.
Alternatively, you can test a script with maskedarray by using a
command-line option, e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">simple_plot</span><span class="o">.</span><span class="n">py</span> <span class="o">--</span><span class="n">maskedarray</span>
</pre></div>
</div>
</section>
<section id="masked-records">
<h2><a class="toc-backref" href="#id7" role="doc-backlink">Masked records</a><a class="headerlink" href="#masked-records" title="Link to this heading">#</a></h2>
<p>Like <em>numpy.ma.core</em>, the <em>ndarray</em>-based implementation
of MaskedArray is limited when working with records: you can
mask any record of the array, but not a field in a record. If you
need this feature, you may want to give the <em>mrecords</em> package
a try (available in the <em>maskedarray</em> directory in the scipy
sandbox). This module defines a new class, <em>MaskedRecord</em>. An
instance of this class accepts a <em>recarray</em> as data, and uses two
masks: the <em>fieldmask</em> has as many entries as records in the array,
each entry with the same fields as a record, but of boolean types:
they indicate whether the field is masked or not; a record entry
is flagged as masked in the <em>mask</em> array if all the fields are
masked. A few examples in the file should give you an idea of what
can be done. Note that <em>mrecords</em> is still experimental…</p>
</section>
<section id="optimizing-maskedarray">
<h2><a class="toc-backref" href="#id8" role="doc-backlink">Optimizing maskedarray</a><a class="headerlink" href="#optimizing-maskedarray" title="Link to this heading">#</a></h2>
</section>
<section id="should-masked-arrays-be-filled-before-processing-or-not">
<h2><a class="toc-backref" href="#id9" role="doc-backlink">Should masked arrays be filled before processing or not?</a><a class="headerlink" href="#should-masked-arrays-be-filled-before-processing-or-not" title="Link to this heading">#</a></h2>
<p>In the current implementation, most operations on masked arrays involve
the following steps:</p>
<blockquote>
<div><ul class="simple">
<li><p>the input arrays are filled</p></li>
<li><p>the operation is performed on the filled arrays</p></li>
<li><p>the mask is set for the results, from the combination of the input masks and the mask corresponding to the domain of the operation.</p></li>
</ul>
</div></blockquote>
<p>For example, consider the division of two masked arrays:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">maskedarray</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">ma</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span><span class="n">mask</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">numpy</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="n">mask</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">numpy</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
</pre></div>
</div>
<p>The division of x by y is then computed as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">d1</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">filled</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># d1 = array([0., 2., 3., 4.])</span>
<span class="n">d2</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">filled</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># array([-1., 0., 1., 1.])</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">mask_or</span><span class="p">(</span><span class="n">ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">y</span><span class="p">))</span> <span class="c1"># m =</span>
<span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">True</span><span class="p">])</span>
<span class="n">dm</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">divide</span><span class="o">.</span><span class="n">domain</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span><span class="n">d2</span><span class="p">)</span> <span class="c1"># array([False, True, False, False])</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">d1</span><span class="o">/</span><span class="n">d2</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">MaskedArray</span><span class="p">)</span> <span class="c1"># masked_array([-0. inf, 3., 4.])</span>
<span class="n">result</span><span class="o">.</span><span class="n">_mask</span> <span class="o">=</span> <span class="n">logical_or</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">dm</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that a division by zero takes place. To avoid it, we can consider
to fill the input arrays, taking the domain mask into account, so that:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">d1</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="c1"># d1 = array([1., 2., 3., 4.])</span>
<span class="n">d2</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="c1"># array([-1., 0., 1., 2.])</span>
<span class="n">dm</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">divide</span><span class="o">.</span><span class="n">domain</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span><span class="n">d2</span><span class="p">)</span> <span class="c1"># array([False, True, False, False])</span>
<span class="n">numpy</span><span class="o">.</span><span class="n">putmask</span><span class="p">(</span><span class="n">d2</span><span class="p">,</span> <span class="n">dm</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># d2 = array([-1., 1., 1., 2.])</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">mask_or</span><span class="p">(</span><span class="n">ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">y</span><span class="p">))</span> <span class="c1"># m =</span>
<span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">True</span><span class="p">])</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">d1</span><span class="o">/</span><span class="n">d2</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">MaskedArray</span><span class="p">)</span> <span class="c1"># masked_array([-1. 0., 3., 2.])</span>
<span class="n">result</span><span class="o">.</span><span class="n">_mask</span> <span class="o">=</span> <span class="n">logical_or</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">dm</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that the <em>.copy()</em> is required to avoid updating the inputs with
<em>putmask</em>. The <em>.filled()</em> method also involves a <em>.copy()</em>.</p>
<p>A third possibility consists in avoid filling the arrays:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">d1</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">_data</span> <span class="c1"># d1 = array([1., 2., 3., 4.])</span>
<span class="n">d2</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">_data</span> <span class="c1"># array([-1., 0., 1., 2.])</span>
<span class="n">dm</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">divide</span><span class="o">.</span><span class="n">domain</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span><span class="n">d2</span><span class="p">)</span> <span class="c1"># array([False, True, False, False])</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">ma</span><span class="o">.</span><span class="n">mask_or</span><span class="p">(</span><span class="n">ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">ma</span><span class="o">.</span><span class="n">getmask</span><span class="p">(</span><span class="n">y</span><span class="p">))</span> <span class="c1"># m =</span>
<span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">True</span><span class="p">])</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">d1</span><span class="o">/</span><span class="n">d2</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">MaskedArray</span><span class="p">)</span> <span class="c1"># masked_array([-1. inf, 3., 2.])</span>
<span class="n">result</span><span class="o">.</span><span class="n">_mask</span> <span class="o">=</span> <span class="n">logical_or</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">dm</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that here again the division by zero takes place.</p>
<p>A quick benchmark gives the following results:</p>
<blockquote>
<div><ul class="simple">
<li><p><em>numpy.ma.divide</em> : 2.69 ms per loop</p></li>
<li><p>classical division : 2.21 ms per loop</p></li>
<li><p>division w/ prefilling : 2.34 ms per loop</p></li>
<li><p>division w/o filling : 1.55 ms per loop</p></li>
</ul>
</div></blockquote>
<p>So, is it worth filling the arrays beforehand ? Yes, if we are interested
in avoiding floating-point exceptions that may fill the result with infs
and nans. No, if we are only interested into speed…</p>
</section>
<section id="thanks">
<h2><a class="toc-backref" href="#id10" role="doc-backlink">Thanks</a><a class="headerlink" href="#thanks" title="Link to this heading">#</a></h2>
<p>Id like to thank Paul Dubois, Travis Oliphant and Sasha for the
original masked array package: without you, I would never have started
that (it might be argued that I shouldnt have anyway, but thats
another story…). I also wish to extend these thanks to Reggie Dugard
and Eric Firing for their suggestions and numerous improvements.</p>
</section>
<section id="revision-notes">
<h2><a class="toc-backref" href="#id11" role="doc-backlink">Revision notes</a><a class="headerlink" href="#revision-notes" title="Link to this heading">#</a></h2>
<blockquote>
<div><ul class="simple">
<li><p>08/25/2007 : Creation of this page</p></li>
<li><p>01/23/2007 : The package has been moved to the SciPy sandbox, and is regularly updated: please check out your SVN version!</p></li>
</ul>
</div></blockquote>
</section>
</section>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "binder-examples/jupyter-stacks-datascience",
ref: "master",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
name: "python3",
path: "./.venv/lib/python3.13/site-packages/numpy/ma"
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</article>
<footer class="prev-next-footer d-print-none">
<div class="prev-next-area">
</div>
</footer>
</div>
<div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> Contents
</div>
<nav class="bd-toc-nav page-toc">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#history">History</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#main-differences">Main differences</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#new-features">New features</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#using-the-new-package-with-numpy-core-ma">Using the new package with numpy.core.ma</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#using-maskedarray-with-matplotlib">Using maskedarray with matplotlib</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#masked-records">Masked records</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#optimizing-maskedarray">Optimizing maskedarray</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#should-masked-arrays-be-filled-before-processing-or-not">Should masked arrays be filled before processing or not?</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#thanks">Thanks</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#revision-notes">Revision notes</a></li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
<div class="bd-footer-content__inner container">
<div class="footer-item">
<p class="component-author">
By Morten Hjorth-Jensen
</p>
</div>
<div class="footer-item">
<p class="copyright">
© Copyright 2023.
<br/>
</p>
</div>
<div class="footer-item">
</div>
<div class="footer-item">
</div>
</div>
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script src="../../../../../../_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b"></script>
<script src="../../../../../../_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b"></script>
<footer class="bd-footer">
</footer>
</body>
</html>
@@ -0,0 +1,569 @@
<!DOCTYPE html>
<html lang="en" data-content_root="../../../../../../" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>NCSA Open Source License &#8212; Applied Data Analysis and Machine Learning</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="../../../../../../_static/styles/theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="../../../../../../_static/styles/bootstrap.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="../../../../../../_static/styles/pydata-sphinx-theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="../../../../../../_static/vendor/fontawesome/6.5.2/css/all.min.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../_static/vendor/fontawesome/6.5.2/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../_static/vendor/fontawesome/6.5.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../_static/vendor/fontawesome/6.5.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/styles/sphinx-book-theme.css?v=eba8b062" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/togglebutton.css?v=13237357" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/mystnb.8ecb98da25f57f5357bf6f572d296f466b2cfe2517ffebfabe82451661e28f02.css?v=6644e6bb" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/sphinx-thebe.css?v=4fa983c6" />
<link rel="stylesheet" type="text/css" href="../../../../../../_static/sphinx-design.min.css?v=95c83b7e" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../../../../../../_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b" />
<link rel="preload" as="script" href="../../../../../../_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b" />
<script src="../../../../../../_static/vendor/fontawesome/6.5.2/js/all.min.js?digest=dfe6caa3a7d634c4db9b"></script>
<script src="../../../../../../_static/documentation_options.js?v=9eb32ce0"></script>
<script src="../../../../../../_static/doctools.js?v=9a2dae69"></script>
<script src="../../../../../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../../../../../../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../../../../../../_static/copybutton.js?v=f281be69"></script>
<script src="../../../../../../_static/scripts/sphinx-book-theme.js?v=887ef09a"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="../../../../../../_static/togglebutton.js?v=4a39c7ea"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script src="../../../../../../_static/design-tabs.js?v=f930bc37"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
<script async="async" src="../../../../../../_static/sphinx-thebe.js?v=c100c467"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
<script>DOCUMENTATION_OPTIONS.pagename = '.venv/lib/python3.13/site-packages/numpy/random/LICENSE';</script>
<link rel="index" title="Index" href="../../../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../../../search.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<input type="checkbox"
class="sidebar-toggle"
id="pst-primary-sidebar-checkbox"/>
<label class="overlay overlay-primary" for="pst-primary-sidebar-checkbox"></label>
<input type="checkbox"
class="sidebar-toggle"
id="pst-secondary-sidebar-checkbox"/>
<label class="overlay overlay-secondary" for="pst-secondary-sidebar-checkbox"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="../../../../../../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search this book..."
aria-label="Search this book..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="../../../../../../intro.html">
<img src="../../../../../../_static/logo.png" class="logo__image only-light" alt="Applied Data Analysis and Machine Learning - Home"/>
<script>document.write(`<img src="../../../../../../_static/logo.png" class="logo__image only-dark" alt="Applied Data Analysis and Machine Learning - Home"/>`);</script>
</a></div>
<div class="sidebar-primary-item">
<script>
document.write(`
<button class="btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
`);
</script></div>
<div class="sidebar-primary-item"><nav class="bd-links bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<ul class="nav bd-sidenav bd-sidenav__home-link">
<li class="toctree-l1">
<a class="reference internal" href="../../../../../../intro.html">
Applied Data Analysis and Machine Learning
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">About the course</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../schedule.html">Course setting</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../teachers.html">Teachers and Grading</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../textbooks.html">Textbooks</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Review of Statistics with Resampling Techniques and Linear Algebra</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../statistics.html">1. Elements of Probability Theory and Statistical Data Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../linalg.html">2. Linear Algebra, Handling of Arrays and more Python Features</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">From Regression to Support Vector Machines</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter1.html">3. Linear Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter2.html">4. Ridge and Lasso Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter3.html">5. Resampling Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter4.html">6. Logistic Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapteroptimization.html">7. Optimization, the central part of any Machine Learning algortithm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter5.html">8. Support Vector Machines, overarching aims</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Decision Trees, Ensemble Methods and Boosting</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter6.html">9. Decision trees, overarching aims</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter7.html">10. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Dimensionality Reduction</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter8.html">11. Basic ideas of the Principal Component Analysis (PCA)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../clustering.html">12. Clustering and Unsupervised Learning</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Deep Learning Methods</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter9.html">13. Neural networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter10.html">14. Building a Feed Forward Neural Network</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter11.html">15. Solving Differential Equations with Deep Learning</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter12.html">16. Convolutional Neural Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../chapter13.html">17. Recurrent neural networks: Overarching view</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Weekly material, notes and exercises</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek34.html">Exercises week 34</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week34.html">Week 34: Introduction to the course, Logistics and Practicalities</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek35.html">Exercises week 35</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week35.html">Week 35: From Ordinary Linear Regression to Ridge and Lasso Regression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek36.html">Exercises week 36</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week36.html">Week 36: Linear Regression and Gradient descent</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek37.html">Exercises week 37</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../week37.html">Week 37: Gradient descent methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek38.html">Exercises week 38</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../exercisesweek39.html">Exercises week 39</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Projects</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../../../../../../project1.html">Project 1 on Machine Learning, deadline October 6 (midnight), 2025</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</button></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<div class="dropdown dropdown-download-buttons">
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
<i class="fas fa-download"></i>
</button>
<ul class="dropdown-menu">
<li><a href="../../../../../../_sources/.venv/lib/python3.13/site-packages/numpy/random/LICENSE.md" target="_blank"
class="btn btn-sm btn-download-source-button dropdown-item"
title="Download source file"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file"></i>
</span>
<span class="btn__text-container">.md</span>
</a>
</li>
<li>
<button onclick="window.print()"
class="btn btn-sm btn-download-pdf-button dropdown-item"
title="Print to PDF"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file-pdf"></i>
</span>
<span class="btn__text-container">.pdf</span>
</button>
</li>
</ul>
</div>
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<script>
document.write(`
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto"></i>
</button>
`);
</script>
<script>
document.write(`
<button class="btn btn-sm pst-navbar-icon search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button>
`);
</script>
<button class="sidebar-toggle secondary-toggle btn btn-sm" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</button>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>NCSA Open Source License</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">NCSA Open Source License</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#clause-bsd-license">3-Clause BSD License</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#components">Components</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<p><strong>This software is dual-licensed under the The University of Illinois/NCSA
Open Source License (NCSA) and The 3-Clause BSD License</strong></p>
<section class="tex2jax_ignore mathjax_ignore" id="ncsa-open-source-license">
<h1>NCSA Open Source License<a class="headerlink" href="#ncsa-open-source-license" title="Link to this heading">#</a></h1>
<p><strong>Copyright (c) 2019 Kevin Sheppard. All rights reserved.</strong></p>
<p>Developed by: Kevin Sheppard (<a class="reference external" href="mailto:kevin&#46;sheppard&#37;&#52;&#48;economics&#46;ox&#46;ac&#46;uk">kevin<span>&#46;</span>sheppard<span>&#64;</span>economics<span>&#46;</span>ox<span>&#46;</span>ac<span>&#46;</span>uk</a>,
<a class="reference external" href="mailto:kevin&#46;k&#46;sheppard&#37;&#52;&#48;gmail&#46;com">kevin<span>&#46;</span>k<span>&#46;</span>sheppard<span>&#64;</span>gmail<span>&#46;</span>com</a>)
<a class="reference external" href="http://www.kevinsheppard.com">http://www.kevinsheppard.com</a></p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:</p>
<p>Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimers.</p>
<p>Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimers in the documentation and/or
other materials provided with the distribution.</p>
<p>Neither the names of Kevin Sheppard, nor the names of any contributors may be
used to endorse or promote products derived from this Software without specific
prior written permission.</p>
<p><strong>THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
THE SOFTWARE.</strong></p>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="clause-bsd-license">
<h1>3-Clause BSD License<a class="headerlink" href="#clause-bsd-license" title="Link to this heading">#</a></h1>
<p><strong>Copyright (c) 2019 Kevin Sheppard. All rights reserved.</strong></p>
<p>Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:</p>
<ol class="arabic simple">
<li><p>Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.</p></li>
<li><p>Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.</p></li>
<li><p>Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.</p></li>
</ol>
<p><strong>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.</strong></p>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="components">
<h1>Components<a class="headerlink" href="#components" title="Link to this heading">#</a></h1>
<p>Many parts of this module have been derived from original sources,
often the algorithms designer. Component licenses are located with
the component code.</p>
</section>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "binder-examples/jupyter-stacks-datascience",
ref: "master",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
name: "python3",
path: "./.venv/lib/python3.13/site-packages/numpy/random"
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</article>
<footer class="prev-next-footer d-print-none">
<div class="prev-next-area">
</div>
</footer>
</div>
<div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> Contents
</div>
<nav class="bd-toc-nav page-toc">
<ul class="visible nav section-nav flex-column">
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">NCSA Open Source License</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#clause-bsd-license">3-Clause BSD License</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#components">Components</a></li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
<div class="bd-footer-content__inner container">
<div class="footer-item">
<p class="component-author">
By Morten Hjorth-Jensen
</p>
</div>
<div class="footer-item">
<p class="copyright">
© Copyright 2023.
<br/>
</p>
</div>
<div class="footer-item">
</div>
<div class="footer-item">
</div>
</div>
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script src="../../../../../../_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b"></script>
<script src="../../../../../../_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b"></script>
<footer class="bd-footer">
</footer>
</body>
</html>
@@ -0,0 +1,236 @@
==================================
A guide to masked arrays in NumPy
==================================
.. Contents::
See http://www.scipy.org/scipy/numpy/wiki/MaskedArray (dead link)
for updates of this document.
History
-------
As a regular user of MaskedArray, I (Pierre G.F. Gerard-Marchant) became
increasingly frustrated with the subclassing of masked arrays (even if
I can only blame my inexperience). I needed to develop a class of arrays
that could store some additional information along with numerical values,
while keeping the possibility for missing data (picture storing a series
of dates along with measurements, what would later become the `TimeSeries
Scikit <http://projects.scipy.org/scipy/scikits/wiki/TimeSeries>`__
(dead link).
I started to implement such a class, but then quickly realized that
any additional information disappeared when processing these subarrays
(for example, adding a constant value to a subarray would erase its
dates). I ended up writing the equivalent of *numpy.core.ma* for my
particular class, ufuncs included. Everything went fine until I needed to
subclass my new class, when more problems showed up: some attributes of
the new subclass were lost during processing. I identified the culprit as
MaskedArray, which returns masked ndarrays when I expected masked
arrays of my class. I was preparing myself to rewrite *numpy.core.ma*
when I forced myself to learn how to subclass ndarrays. As I became more
familiar with the *__new__* and *__array_finalize__* methods,
I started to wonder why masked arrays were objects, and not ndarrays,
and whether it wouldn't be more convenient for subclassing if they did
behave like regular ndarrays.
The new *maskedarray* is what I eventually come up with. The
main differences with the initial *numpy.core.ma* package are
that MaskedArray is now a subclass of *ndarray* and that the
*_data* section can now be any subclass of *ndarray*. Apart from a
couple of issues listed below, the behavior of the new MaskedArray
class reproduces the old one. Initially the *maskedarray*
implementation was marginally slower than *numpy.ma* in some areas,
but work is underway to speed it up; the expectation is that it can be
made substantially faster than the present *numpy.ma*.
Note that if the subclass has some special methods and
attributes, they are not propagated to the masked version:
this would require a modification of the *__getattribute__*
method (first trying *ndarray.__getattribute__*, then trying
*self._data.__getattribute__* if an exception is raised in the first
place), which really slows things down.
Main differences
----------------
* The *_data* part of the masked array can be any subclass of ndarray (but not recarray, cf below).
* *fill_value* is now a property, not a function.
* in the majority of cases, the mask is forced to *nomask* when no value is actually masked. A notable exception is when a masked array (with no masked values) has just been unpickled.
* I got rid of the *share_mask* flag, I never understood its purpose.
* *put*, *putmask* and *take* now mimic the ndarray methods, to avoid unpleasant surprises. Moreover, *put* and *putmask* both update the mask when needed. * if *a* is a masked array, *bool(a)* raises a *ValueError*, as it does with ndarrays.
* in the same way, the comparison of two masked arrays is a masked array, not a boolean
* *filled(a)* returns an array of the same subclass as *a._data*, and no test is performed on whether it is contiguous or not.
* the mask is always printed, even if it's *nomask*, which makes things easy (for me at least) to remember that a masked array is used.
* *cumsum* works as if the *_data* array was filled with 0. The mask is preserved, but not updated.
* *cumprod* works as if the *_data* array was filled with 1. The mask is preserved, but not updated.
New features
------------
This list is non-exhaustive...
* the *mr_* function mimics *r_* for masked arrays.
* the *anom* method returns the anomalies (deviations from the average)
Using the new package with numpy.core.ma
----------------------------------------
I tried to make sure that the new package can understand old masked
arrays. Unfortunately, there's no upward compatibility.
For example:
>>> import numpy.core.ma as old_ma
>>> import maskedarray as new_ma
>>> x = old_ma.array([1,2,3,4,5], mask=[0,0,1,0,0])
>>> x
array(data =
[ 1 2 999999 4 5],
mask =
[False False True False False],
fill_value=999999)
>>> y = new_ma.array([1,2,3,4,5], mask=[0,0,1,0,0])
>>> y
array(data = [1 2 -- 4 5],
mask = [False False True False False],
fill_value=999999)
>>> x==y
array(data =
[True True True True True],
mask =
[False False True False False],
fill_value=?)
>>> old_ma.getmask(x) == new_ma.getmask(x)
array([True, True, True, True, True])
>>> old_ma.getmask(y) == new_ma.getmask(y)
array([True, True, False, True, True])
>>> old_ma.getmask(y)
False
Using maskedarray with matplotlib
---------------------------------
Starting with matplotlib 0.91.2, the masked array importing will work with
the maskedarray branch) as well as with earlier versions.
By default matplotlib still uses numpy.ma, but there is an rcParams setting
that you can use to select maskedarray instead. In the matplotlibrc file
you will find::
#maskedarray : False # True to use external maskedarray module
# instead of numpy.ma; this is a temporary #
setting for testing maskedarray.
Uncomment and set to True to select maskedarray everywhere.
Alternatively, you can test a script with maskedarray by using a
command-line option, e.g.::
python simple_plot.py --maskedarray
Masked records
--------------
Like *numpy.ma.core*, the *ndarray*-based implementation
of MaskedArray is limited when working with records: you can
mask any record of the array, but not a field in a record. If you
need this feature, you may want to give the *mrecords* package
a try (available in the *maskedarray* directory in the scipy
sandbox). This module defines a new class, *MaskedRecord*. An
instance of this class accepts a *recarray* as data, and uses two
masks: the *fieldmask* has as many entries as records in the array,
each entry with the same fields as a record, but of boolean types:
they indicate whether the field is masked or not; a record entry
is flagged as masked in the *mask* array if all the fields are
masked. A few examples in the file should give you an idea of what
can be done. Note that *mrecords* is still experimental...
Optimizing maskedarray
----------------------
Should masked arrays be filled before processing or not?
--------------------------------------------------------
In the current implementation, most operations on masked arrays involve
the following steps:
* the input arrays are filled
* the operation is performed on the filled arrays
* the mask is set for the results, from the combination of the input masks and the mask corresponding to the domain of the operation.
For example, consider the division of two masked arrays::
import numpy
import maskedarray as ma
x = ma.array([1,2,3,4],mask=[1,0,0,0], dtype=numpy.float64)
y = ma.array([-1,0,1,2], mask=[0,0,0,1], dtype=numpy.float64)
The division of x by y is then computed as::
d1 = x.filled(0) # d1 = array([0., 2., 3., 4.])
d2 = y.filled(1) # array([-1., 0., 1., 1.])
m = ma.mask_or(ma.getmask(x), ma.getmask(y)) # m =
array([True,False,False,True])
dm = ma.divide.domain(d1,d2) # array([False, True, False, False])
result = (d1/d2).view(MaskedArray) # masked_array([-0. inf, 3., 4.])
result._mask = logical_or(m, dm)
Note that a division by zero takes place. To avoid it, we can consider
to fill the input arrays, taking the domain mask into account, so that::
d1 = x._data.copy() # d1 = array([1., 2., 3., 4.])
d2 = y._data.copy() # array([-1., 0., 1., 2.])
dm = ma.divide.domain(d1,d2) # array([False, True, False, False])
numpy.putmask(d2, dm, 1) # d2 = array([-1., 1., 1., 2.])
m = ma.mask_or(ma.getmask(x), ma.getmask(y)) # m =
array([True,False,False,True])
result = (d1/d2).view(MaskedArray) # masked_array([-1. 0., 3., 2.])
result._mask = logical_or(m, dm)
Note that the *.copy()* is required to avoid updating the inputs with
*putmask*. The *.filled()* method also involves a *.copy()*.
A third possibility consists in avoid filling the arrays::
d1 = x._data # d1 = array([1., 2., 3., 4.])
d2 = y._data # array([-1., 0., 1., 2.])
dm = ma.divide.domain(d1,d2) # array([False, True, False, False])
m = ma.mask_or(ma.getmask(x), ma.getmask(y)) # m =
array([True,False,False,True])
result = (d1/d2).view(MaskedArray) # masked_array([-1. inf, 3., 2.])
result._mask = logical_or(m, dm)
Note that here again the division by zero takes place.
A quick benchmark gives the following results:
* *numpy.ma.divide* : 2.69 ms per loop
* classical division : 2.21 ms per loop
* division w/ prefilling : 2.34 ms per loop
* division w/o filling : 1.55 ms per loop
So, is it worth filling the arrays beforehand ? Yes, if we are interested
in avoiding floating-point exceptions that may fill the result with infs
and nans. No, if we are only interested into speed...
Thanks
------
I'd like to thank Paul Dubois, Travis Oliphant and Sasha for the
original masked array package: without you, I would never have started
that (it might be argued that I shouldn't have anyway, but that's
another story...). I also wish to extend these thanks to Reggie Dugard
and Eric Firing for their suggestions and numerous improvements.
Revision notes
--------------
* 08/25/2007 : Creation of this page
* 01/23/2007 : The package has been moved to the SciPy sandbox, and is regularly updated: please check out your SVN version!
@@ -0,0 +1,71 @@
**This software is dual-licensed under the The University of Illinois/NCSA
Open Source License (NCSA) and The 3-Clause BSD License**
# NCSA Open Source License
**Copyright (c) 2019 Kevin Sheppard. All rights reserved.**
Developed by: Kevin Sheppard (<kevin.sheppard@economics.ox.ac.uk>,
<kevin.k.sheppard@gmail.com>)
[http://www.kevinsheppard.com](http://www.kevinsheppard.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimers.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimers in the documentation and/or
other materials provided with the distribution.
Neither the names of Kevin Sheppard, nor the names of any contributors may be
used to endorse or promote products derived from this Software without specific
prior written permission.
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
THE SOFTWARE.**
# 3-Clause BSD License
**Copyright (c) 2019 Kevin Sheppard. All rights reserved.**
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
**THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.**
# Components
Many parts of this module have been derived from original sources,
often the algorithm's designer. Component licenses are located with
the component code.
@@ -402,13 +402,15 @@
"# for p in range(1, 5):\n",
"# predictions = ...\n",
"# targets = ...\n",
"#\n",
"# X = ...\n",
"# X_train, X_test, y_train, y_test = ...\n",
"# for b in range(bootstraps):\n",
"# x_sample, y_sample = ...\n",
"# X = ...\n",
"# X_train, X_test, y_train, y_test = ...\n",
"# X_train_re, y_train_re = ...\n",
"#\n",
"# #this is where you fit your model on the sampled data\n",
"# # fit your model on the sampled data\n",
"#\n",
"# # make predictions on the test data\n",
"# predictions[b, :] =\n",
"# targets[b, :] =\n",
"#\n",
@@ -572,13 +572,15 @@ C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_
<span class="c1"># for p in range(1, 5):</span>
<span class="c1"># predictions = ...</span>
<span class="c1"># targets = ...</span>
<span class="c1">#</span>
<span class="c1"># X = ...</span>
<span class="c1"># X_train, X_test, y_train, y_test = ...</span>
<span class="c1"># for b in range(bootstraps):</span>
<span class="c1"># x_sample, y_sample = ...</span>
<span class="c1"># X = ...</span>
<span class="c1"># X_train, X_test, y_train, y_test = ...</span>
<span class="c1"># X_train_re, y_train_re = ...</span>
<span class="c1">#</span>
<span class="c1"># #this is where you fit your model on the sampled data</span>
<span class="c1"># # fit your model on the sampled data</span>
<span class="c1">#</span>
<span class="c1"># # make predictions on the test data</span>
<span class="c1"># predictions[b, :] =</span>
<span class="c1"># targets[b, :] =</span>
<span class="c1">#</span>
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "2c4ae3e0",
"id": "089950cb",
"metadata": {},
"source": [
"# Notebooks with MyST Markdown\n",
@@ -19,7 +19,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "bd842239",
"id": "87c2f6e9",
"metadata": {},
"outputs": [],
"source": [
@@ -28,7 +28,7 @@
},
{
"cell_type": "markdown",
"id": "0b607518",
"id": "04f8b494",
"metadata": {},
"source": [
"When your book is built, the contents of any `{code-cell}` blocks will be\n",
@@ -402,13 +402,15 @@
"# for p in range(1, 5):\n",
"# predictions = ...\n",
"# targets = ...\n",
"#\n",
"# X = ...\n",
"# X_train, X_test, y_train, y_test = ...\n",
"# for b in range(bootstraps):\n",
"# x_sample, y_sample = ...\n",
"# X = ...\n",
"# X_train, X_test, y_train, y_test = ...\n",
"# X_train_re, y_train_re = ...\n",
"#\n",
"# #this is where you fit your model on the sampled data\n",
"# # fit your model on the sampled data\n",
"#\n",
"# # make predictions on the test data\n",
"# predictions[b, :] =\n",
"# targets[b, :] =\n",
"#\n",
+6 -4
View File
@@ -402,13 +402,15 @@
"# for p in range(1, 5):\n",
"# predictions = ...\n",
"# targets = ...\n",
"#\n",
"# X = ...\n",
"# X_train, X_test, y_train, y_test = ...\n",
"# for b in range(bootstraps):\n",
"# x_sample, y_sample = ...\n",
"# X = ...\n",
"# X_train, X_test, y_train, y_test = ...\n",
"# X_train_re, y_train_re = ...\n",
"#\n",
"# #this is where you fit your model on the sampled data\n",
"# # fit your model on the sampled data\n",
"#\n",
"# # make predictions on the test data\n",
"# predictions[b, :] =\n",
"# targets[b, :] =\n",
"#\n",