Add confidence intervals exercise
This commit is contained in:
parent
03ccfbdd33
commit
1be7effea7
17
README.html
17
README.html
@ -4,12 +4,12 @@
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="Docutils 0.12: http://docutils.sourceforge.net/" />
|
||||
<title>100 numpy exercises</title>
|
||||
<title>100 numpy exercises</title>
|
||||
<link rel="stylesheet" href="numpy.css" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="document" id="numpy-exercises">
|
||||
<h1 class="title">100 numpy exercises</h1>
|
||||
<h1 class="title">100 numpy exercises</h1>
|
||||
<h2 class="subtitle" id="a-joint-effort-of-the-numpy-community">A joint effort of the numpy community</h2>
|
||||
|
||||
<p>The goal is both to offer a quick reference for new and old users and to
|
||||
@ -949,7 +949,20 @@ the rows which only contain integers and which sum to n. (★★★)</p>
|
||||
<span class="keyword">print</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">[</span><span class="name">M</span><span class="punctuation">])</span>
|
||||
</pre>
|
||||
</li>
|
||||
<li><p class="first">Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means).</p>
|
||||
</li>
|
||||
</ol>
|
||||
<blockquote>
|
||||
<pre class="code python literal-block">
|
||||
<span class="comment single"># Author: Jessica B. Hamrick</span>
|
||||
|
||||
<span class="name">N</span> <span class="operator">=</span> <span class="literal number integer">1000</span> <span class="comment single"># number of bootstrap samples</span>
|
||||
<span class="name">idx</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="name">X</span><span class="operator">.</span><span class="name">size</span><span class="punctuation">,</span> <span class="punctuation">(</span><span class="name">N</span><span class="punctuation">,</span> <span class="name">X</span><span class="operator">.</span><span class="name">size</span><span class="punctuation">))</span>
|
||||
<span class="name">means</span> <span class="operator">=</span> <span class="name">X</span><span class="punctuation">[</span><span class="name">idx</span><span class="punctuation">]</span><span class="operator">.</span><span class="name">mean</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
|
||||
<span class="name">confint</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">percentile</span><span class="punctuation">(</span><span class="name">means</span><span class="punctuation">,</span> <span class="punctuation">[</span><span class="literal number float">2.5</span><span class="punctuation">,</span> <span class="literal number float">97.5</span><span class="punctuation">])</span>
|
||||
<span class="keyword">print</span><span class="punctuation">(</span><span class="name">confint</span><span class="punctuation">)</span>
|
||||
</pre>
|
||||
</blockquote>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
12
README.rst
12
README.rst
@ -1106,3 +1106,15 @@ Thanks to Michiaki Ariga, there is now a
|
||||
M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1)
|
||||
M &= (X.sum(axis=-1) == n)
|
||||
print(X[M])
|
||||
|
||||
#. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means).
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Author: Jessica B. Hamrick
|
||||
|
||||
N = 1000 # number of bootstrap samples
|
||||
idx = np.random.randint(0, X.size, (N, X.size))
|
||||
means = X[idx].mean(axis=1)
|
||||
confint = np.percentile(means, [2.5, 97.5])
|
||||
print(confint)
|
||||
|
Loading…
Reference in New Issue
Block a user