Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] NumPy argpatition()

#1
NumPy argpatition()

<div><pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">numpy.argpartition(a, kth, axis=-1, kind='introselect', order=None)</pre>
<p class="has-pale-cyan-blue-background-color has-background">The NumPy <a href="https://numpy.org/doc/stable/reference/generated/numpy.argpartition.html#numpy.argpartition" target="_blank" rel="noreferrer noopener" title="https://numpy.org/doc/stable/reference/generated/numpy.argpartition.html#numpy.argpartition">argpatition function</a> performs an indirect partition along the given axis using the algorithm specified by the <code>kind</code> keyword. It returns an array of indices of the same shape as a that index data along the given axis in partitioned order.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Arguments</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>c</strong></td>
<td>array_like or poly1d object</td>
<td>The input polynomials to be multiplied</td>
</tr>
<tr>
<td>kth</td>
<td>integer or sequence of integers</td>
<td>Element index to partition by. The k-th element will be in its final sorted position and all smaller elements will be moved before it and all larger elements behind it. The order all elements in the partitions is undefined. If provided with a sequence of k-th it will partition all of them into their sorted position at once.</td>
</tr>
<tr>
<td>axis</td>
<td>integer or <code>None</code></td>
<td>(Optional.) Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.</td>
</tr>
<tr>
<td>kind</td>
<td><code>{'introselect'}</code></td>
<td>(Optional.) Selection algorithm. Default is <code>'introselect'</code>. </td>
</tr>
<tr>
<td>order</td>
<td>string or <a href="https://blog.finxter.com/string-joinlist-vs-list-joinstring-why-did-pythons-creators-choose-the-former/" target="_blank" rel="noreferrer noopener" title="string.join(list) vs list.join(string) | Why Python’s Creators Chose The Former">list of strings</a></td>
<td>(Optional.) When <em>a</em> is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.</td>
</tr>
</tbody>
</table>
</figure>
<p>The following table shows the return value of the function:</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th></th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong><strong>Return Value</strong></strong></td>
<td><strong>index_array</strong> : ndarray, int</td>
<td>Array of indices that partition <em>a</em> along the specified axis. If <em>a</em> is one-dimensional, <code>a[index_array]</code> yields a partitioned <em>a</em>. More generally, <code>np.take_along_axis(a, index_array, axis=a)</code> always yields the partitioned <em>a</em>, irrespective of dimensionality.</td>
</tr>
</tbody>
</table>
</figure>
<p><strong>Related</strong>: See <a href="https://numpy.org/doc/stable/reference/generated/numpy.partition.html#numpy.partition" target="_blank" rel="noreferrer noopener"><code>partition</code></a> for notes on the different selection algorithms.</p>
<p>Let’s dive into some examples to show how the function is used in practice:</p>
<h3>Examples</h3>
<p>One-dimensional array:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np x = np.array([3, 4, 2, 1]) print(x[np.argpartition(x, 3)])
# [2 1 3 4] print(x[np.argpartition(x, (1, 3))])
# [1 2 3 4]</pre>
<p>Multi-dimensional array:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np x = np.array([3, 4, 2, 1]) print(x[np.argpartition(x, 3)])
# [2 1 3 4] print(x[np.argpartition(x, (1, 3))])
# [1 2 3 4] x = [3, 4, 2, 1]
print(np.array(x)[np.argpartition(x, 3)])
# [2 1 3 4]</pre>
<p>Any <a href="https://finxter.com/" target="_blank" rel="noreferrer noopener" title="https://finxter.com/">master coder </a>has a “hands-on” mentality with a bias towards action. Try it yourself—play with the function in the following interactive code shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/ThriftyUtterStack?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p><em><strong>Exercise</strong>: Change the parameters of your polynomials and print them without the comparisons. Do you understand where they come from?</em></p>
<p><em><strong>Master NumPy—and become a data science pro:</strong></em></p>
<figure class="wp-block-image size-large is-resized"><a href="https://blog.finxter.com/coffee-break-numpy/" target="_blank" rel="noopener noreferrer"><img src="https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy_v2-1-683x1024.png" alt="Coffee Break NumPy" class="wp-image-2860" width="342" height="512" srcset="https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy_v2-1.png 683w, https://blog.finxter.com/wp-content/uplo...00x300.png 200w, https://blog.finxter.com/wp-content/uplo...8x1152.png 768w, https://blog.finxter.com/wp-content/uplo...00x150.png 100w, https://blog.finxter.com/wp-content/uplo...0x1005.png 670w" sizes="(max-width: 342px) 100vw, 342px" /></a></figure>
<h2>Related Video</h2>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="NumPy Tutorial - Everything You Need to Know to Get Started" width="1400" height="788" src="https://www.youtube.com/embed/s55BEAJRTOk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
</div>


https://www.sickgaming.net/blog/2020/07/...gpatition/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016