Home Backend Development Python Tutorial A brief discussion on several sorting methods of numpy array_python

A brief discussion on several sorting methods of numpy array_python

Dec 16, 2017 pm 01:32 PM
numpy several kinds sort

This article mainly introduces several sorting methods of numpy arrays, including a brief introduction to numpy and how to create arrays. It has certain reference value. Friends who are interested in numpy can refer to it.

Brief introduction

The NumPy system is an open source array calculation extension for Python. This tool can be used to store and process large matrices much more efficiently than Python's own nested list structure (which can also be used to represent matrices).

Create an array

Create a 1-dimensional array:

data = np.array([1,3,4,8])  

View array dimensions

data.shape

View array type

data.dtype

Get or modify array elements by index

data[1] Get elements<br>data[1] = 'a' Modify Element 

Create a two-dimensional array

data = np.array([[1,2,3],[4, 5,6]]) Both elements are lists
2.data = np.arange(10) Like python’s range, range returns a list, and arange returns an array of array type
3.data2 = data.reshape(2,5) returns a 2*5 array. It does not copy the array but a reference. It just returns a different view of the array. If data changes, data2 will also change.

Create a special array

data = np.zeros((2,2)) Create a 2*2 2-dimensional array with all 0s<br>data = np.ones(( 2,3,3,)) Create a three-dimensional array with all 1s<br>data = np.eye(4) Create a 4*4 diagonal array, with diagonal elements being 1 and others being 0<br>

Array conversion

data = np.arange(16).reshape(4,4) Convert the shifted array of 0-16 to 4 *Array of 4

Sort method

Note: It is often necessary to sort arrays or lists, and python provides several Sorting function, the following describes the characteristics;

Two-dimensional array a:

1 4
3 1
Copy after login

1, ndarray.sort(axis= -1,kind='quicksort',order=None)

Usage method: a.sort

Parameter description:

axis: sort along the array Direction, 0 means by row, 1 means by column

kind: sorting algorithm, provides quick sort, mixed sort, heap sort

order: does not refer to the order, when used in the future Let’s analyze the effect of this

: Sort array a, and directly change a

after sorting. For example:

>>a.sort(axis=1)
>>print a
Copy after login

1 4
1 3
Copy after login

2、numpy.sort(a,axis=-1,kind='quicksort',order=None)

Usage :numpy.sort(a)

Parameter description:

a: Array to be sorted, other effects are the same as 1

: For array a Sort, return a sorted array (same dimension as a), a unchanged

For example:

>>print numpy.sort(a,axis=1)
1 4
1 3
>>print a
1 4
3 1
Copy after login

3, numpy.argsort (a,axis=-1,kind='quicksort',order=None)

Usage method: numpy.argsort(a)

Parameter description: Same as 2

Effect: Sort array a and return a sorted index, a remains unchanged

For example:

>>print numpy.argsort(a,axis=1)
0 1
1 0
Copy after login

4, sorted (iterable,cmp=None,key=None,reverse=False)

Description: The built-in sorting function can be used for lists, dictionaries, etc.

iterable: iterable Type;

cmp: Function used for comparison, what is compared is determined by key, has a default value, and iterates an item in the collection;

key: uses a certain attribute and function of the list element As a keyword, proceed has a default value and is an item in the iterative collection;

reverse: sorting rule.reverse=True or reverse=False, the default is False (from small to large).

Return value: It is a sorted iterable type, the same as iterable;

For example: b is a dictionary

b:

{' a':2,'c':1,'b':3}

Sort b:

>>c=sorted(b.iteritems(),key=operator.itemgetter(1),reverse=False)
>>print c[(&#39;c&#39;, 1), (&#39;a&#39;, 2), (&#39;b&#39;, 3)]
Copy after login

Visible: return is a list

Summary

The above is the entire content of this article about several sorting methods of numpy arrays. I hope it will be useful to everyone. helped. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point out. Thank you friends for supporting this site!

Related recommendations:

Python Scientific Computing - Quick Start with Numpy

Why is numpy array so fast?

Python NumPy library installation and usage notes

The above is the detailed content of A brief discussion on several sorting methods of numpy array_python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to sort photos by date taken in Windows 11/10 How to sort photos by date taken in Windows 11/10 Feb 19, 2024 pm 08:45 PM

This article will introduce how to sort pictures according to shooting date in Windows 11/10, and also discuss what to do if Windows does not sort pictures by date. In Windows systems, organizing photos properly is crucial to making it easy to find image files. Users can manage folders containing photos based on different sorting methods such as date, size, and name. In addition, you can set ascending or descending order as needed to organize files more flexibly. How to Sort Photos by Date Taken in Windows 11/10 To sort photos by date taken in Windows, follow these steps: Open Pictures, Desktop, or any folder where you place photos In the Ribbon menu, click

How to sort emails by sender, subject, date, category, size in Outlook How to sort emails by sender, subject, date, category, size in Outlook Feb 19, 2024 am 10:48 AM

Outlook offers many settings and features to help you manage your work more efficiently. One of them is the sorting option that allows you to categorize your emails according to your needs. In this tutorial, we will learn how to use Outlook's sorting feature to organize emails based on criteria such as sender, subject, date, category, or size. This will make it easier for you to process and find important information, making you more productive. Microsoft Outlook is a powerful application that makes it easy to centrally manage your email and calendar schedules. You can easily send, receive, and organize email, while built-in calendar functionality makes it easy to keep track of your upcoming events and appointments. How to be in Outloo

Step-by-step guide on how to install NumPy in PyCharm and get the most out of its features Step-by-step guide on how to install NumPy in PyCharm and get the most out of its features Feb 18, 2024 pm 06:38 PM

Teach you step by step to install NumPy in PyCharm and make full use of its powerful functions. Preface: NumPy is one of the basic libraries for scientific computing in Python. It provides high-performance multi-dimensional array objects and various functions required to perform basic operations on arrays. function. It is an important part of most data science and machine learning projects. This article will introduce you to how to install NumPy in PyCharm, and demonstrate its powerful features through specific code examples. Step 1: Install PyCharm First, we

Upgrading numpy versions: a detailed and easy-to-follow guide Upgrading numpy versions: a detailed and easy-to-follow guide Feb 25, 2024 pm 11:39 PM

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

Numpy version selection guide: why upgrade? Numpy version selection guide: why upgrade? Jan 19, 2024 am 09:34 AM

With the rapid development of fields such as data science, machine learning, and deep learning, Python has become a mainstream language for data analysis and modeling. In Python, NumPy (short for NumericalPython) is a very important library because it provides a set of efficient multi-dimensional array objects and is the basis for many other libraries such as pandas, SciPy and scikit-learn. In the process of using NumPy, you are likely to encounter compatibility issues between different versions, then

Numpy installation guide: Solving installation problems in one article Numpy installation guide: Solving installation problems in one article Feb 21, 2024 pm 08:15 PM

Numpy installation guide: One article to solve installation problems, need specific code examples Introduction: Numpy is a powerful scientific computing library in Python. It provides efficient multi-dimensional array objects and tools for operating array data. However, for beginners, installing Numpy may cause some confusion. This article will provide you with a Numpy installation guide to help you quickly solve installation problems. 1. Install the Python environment: Before installing Numpy, you first need to make sure that Py is installed.

Uncover the secret method to quickly uninstall the NumPy library Uncover the secret method to quickly uninstall the NumPy library Jan 26, 2024 am 08:32 AM

The secret of how to quickly uninstall the NumPy library is revealed. Specific code examples are required. NumPy is a powerful Python scientific computing library that is widely used in fields such as data analysis, scientific computing, and machine learning. However, sometimes we may need to uninstall the NumPy library, whether to update the version or for other reasons. This article will introduce some methods to quickly uninstall the NumPy library and provide specific code examples. Method 1: Use pip to uninstall pip is a Python package management tool that can be used to install, upgrade and

In-depth analysis of numpy slicing operations and application in actual combat In-depth analysis of numpy slicing operations and application in actual combat Jan 26, 2024 am 08:52 AM

Detailed explanation of numpy slicing operation method and practical application guide Introduction: Numpy is one of the most popular scientific computing libraries in Python, providing powerful array operation functions. Among them, slicing operation is one of the commonly used and powerful functions in numpy. This article will introduce the slicing operation method in numpy in detail, and demonstrate the specific use of slicing operation through practical application guide. 1. Introduction to numpy slicing operation method Numpy slicing operation refers to obtaining a subset of an array by specifying an index interval. Its basic form is:

See all articles