NumPy | Python Methods and Functions

** **

** Example # 1: ** Just sort the given array based on the axis using the sort () method.

` `

` ` |

` `

** Exit:**

Along first axis: [[10 1] [12 15]] Along first axis: [[10 15] [1 12]] Along none axis: [1 10 1 2 15]

** Example # 2: ** Get the indexes that a sorted array can return using the argsort () method

` `

` ` ` # Python code for demo `

` # working of numpy.argsort `

` import `

` numpy as np `

` # Numpy array created `

` a `

` = `

` np.array ([`

` 9 `

`, `

` 3 `

`, `

` 1 `

`, `

` 7 `

`, `

` 4 `

`, `

` 3 `

`, `

< code class = "value"> 6 `]) `

` # unsorted print array `

` print `

` (`

` `Original array:` `

`, a) `

` `

` # Sort array indices `

` ` ` b `

` = `

` np.argsort (a) `

` print `

` (`

` `Sorted indices of original array- & gt;` `

`, b) `

` # Get a sorted array using sorted indices `

` # c is a temporary array created from the same len as b `

` `` c `

=

` np.zeros (`

` len `

` ( b), dtype `

` = `

` int `

`) `

` for `

` i `

` in `

` range `

` (`

` 0 `

`, `

` len `

` (b)): `

` `

` c [i] `

` = `

` a [b [i]] `

` print `

` (`

` `Sorted array- & gt;` `

`, c) `

** Output: **

Original array: [9 3 1 7 4 3 6] Sorted indices of original array- & gt; [2 1 5 4 6 3 0] Sorted array- & gt; [1 3 3 4 6 7 9]

** Example # 3: ** Get a stable sort using a sequence of keys.

` `

` ` ` import `

` numpy as np `

` `

` # Numpy array created `

` # First column `

` a `

` = `

` np.array ([`

` 9 `

`, `

` 3 `

`, `

` 1 `

`, `

` 3 `

`, `

` 4 `

`, `

` 3 `

`, `

` 6 ``]) `

` ` ` `

` # Second column `

` b `

` = `

` np.array ([`

` 4 `

`, `

` 6 `

`, `

` 9 `

`, `

` 2 `

`, `

` 1 `

`, `

` 8 `

`, `

` 7 `

`]) `

` print `

` (`

` `column a, column b` `

`) `

` for `

` (i, j) `

` in `

` zip `

` (a, b) : `

` print `

` (i, `` `` `

`, j) `

` ` ` `

` # Sort by then by b `

` ` ` ind `

` = `

` np.lexsort ((b, a)) `

` print `

` (`

` `Sorted indices- & gt;` `

`, ind) `

` `

** Exit :**

column a, column b 9 4 3 6 1 9 3 2 4 1 3 8 6 7 Sorted indices- & gt; [2 3 1 5 4 6 0]

Mastering regular expressions by Jeffrey Friedl, 3rd edition. Regular expressions are an extremely powerful tool for manipulating text and data. They are standard features today in a variety of pop...

05/09/2021

Managing and analyzing data have always offered the greatest benefits and the greatest challenges for organizations of all sizes and across all industries. Businesses have long struggled with finding ...

10/07/2020

Cracking the Coding Interview PDF: 189 Programming Questions and Solutions, 6th Edition. I am not a recruiter. I am a software engineer. And as such, I know what it's like to be asked to create ing...

31/08/2021

Roger Jennings is an author and consultant specializing in Microsoft .NET n-tier database applications and data-intensive Windows Communication Foundation (WCF) Web services with SQL Server. He’s be...

10/07/2020

X
# Submit new EBook