- Getting Started with Python Data Analysis
- Phuong Vo.T.H Martin Czygan
- 279字
- 2021-07-09 21:02:33
Array functions
Many helpful array functions are supported in NumPy for analyzing data. We will list some part of them that are common in use. Firstly, the transposing function is another kind of reshaping form that returns a view on the original data array without copying anything:
>>> a = np.array([[0, 5, 10], [20, 25, 30]]) >>> a.reshape(3, 2) array([[0, 5], [10, 20], [25, 30]]) >>> a.T array([[0, 20], [5, 25], [10, 30]])
In general, we have the swapaxes
method that takes a pair of axis numbers and returns a view on the data, without making a copy:
>>> a = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]) >>> a.swapaxes(1, 2) array([[[0, 3], [1, 4], [2, 5]], [[6, 9], [7, 10], [8, 11]]])
The transposing function is used to do matrix computations; for example, computing the inner matrix product XT.X
using np.dot
:
>>> a = np.array([[1, 2, 3],[4,5,6]]) >>> np.dot(a.T, a) array([[17, 22, 27], [22, 29, 36], [27, 36, 45]])
Sorting data in an array is also an important demand in processing data. Let's take a look at some sorting functions and their use:
>>> a = np.array ([[6, 34, 1, 6], [0, 5, 2, -1]]) >>> np.sort(a) # sort along the last axis array([[1, 6, 6, 34], [-1, 0, 2, 5]]) >>> np.sort(a, axis=0) # sort along the first axis array([[0, 5, 1, -1], [6, 34, 2, 6]]) >>> b = np.argsort(a) # fancy indexing of sorted array >>> b array([[2, 0, 3, 1], [3, 0, 2, 1]]) >>> a[0][b[0]] array([1, 6, 6, 34]) >>> np.argmax(a) # get index of maximum element 1
See the following table for a listing of array functions:

推薦閱讀
- 演進式架構(gòu)(原書第2版)
- Delphi程序設(shè)計基礎(chǔ):教程、實驗、習(xí)題
- C語言程序設(shè)計基礎(chǔ)與實驗指導(dǎo)
- 數(shù)據(jù)結(jié)構(gòu)簡明教程(第2版)微課版
- Python Data Analysis(Second Edition)
- JavaScript:Moving to ES2015
- Python編程:從入門到實踐
- Raspberry Pi Home Automation with Arduino(Second Edition)
- 時空數(shù)據(jù)建模及其應(yīng)用
- Python機器學(xué)習(xí)之金融風(fēng)險管理
- MINECRAFT編程:使用Python語言玩轉(zhuǎn)我的世界
- OpenCV with Python Blueprints
- SwiftUI極簡開發(fā)
- JSP程序設(shè)計與案例實戰(zhàn)(慕課版)
- LabVIEW入門與實戰(zhàn)開發(fā)100例(第4版)