未加星标

A Complete Guide To NumPy Functions in Python For Beginners

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

There is a common saying among low-level language developers like those schooled in C++. They admit that python improves development time but claim that it sacrifices runtime in the process.

While this is certainly true formany applications, what few developers know is that there are ways to speed up Python operation time without compromising its ease of use. Even advanced Python developers don't always use the tools available to them for optimizing computations.

However, in the world of intensive programming where repeating millions of function calls is common practice, a runtime of 50 microseconds is considered slow . Consider that those 50 microseconds, repeated over a million function calls, translates to an additional 50 seconds of runtime and that is definitely slow .

NumPy functions like ndarray.size , np.zeros , and its all-important indexing functions can radically improve the functionality and convenience of working with large data arrays.

Quick Navigation

What Is NumPy and What Is It for?

Basic NumPy Functions

Creating an Array from a Python List

Making a Placeholder Matrix Using NP.Zeros

Other Placeholder Arrays: NP.Ones and NP.Empty

Creating a Sequenced Array Using aRange and LinSpace

What Is NumPy and What Is It for?
A Complete Guide To NumPy Functions in Python For Beginners

On its own, Python is a powerful general-purposeprogramming language. The NumPy library (along with SciPy and MatPlotLib) turns it into an even more robust environment for serious scientific computing.

NumPy establishes a homogenous multidimensional array as its main object an n-dimensional matrix. You can use this object as a table of same-type elements indexed by positive integer tuples.

For the most part, only Python programmers in academic settings make full use of these computational opportunities this approach offers. The majority of other developers rely on Python lists. This is a perfectly feasible method for dealing with relatively small matrices, but it gets very unwieldy when dealing with large ones.

For example, if you were trying to create a cube array of 1000 cells cubed a 1 billion cell 3D matrix you would be stuck at a minimum size of 12 GB using Python lists. 32-bit architecture breaks down at this size, so you're forced to create a 64-bit build using pointers to create a "list of lists". If that sounds inefficient and infeasible, that's because it is.

With NumPy, you could arrange all of this data into a 64-bit build that takes up about 4 GB of space. The amount of time it would take to manipulate or compute any of that data is much smaller than if you try to implement an iterative, nested Python list.

Basic NumPy Functions

In order to use Python NumPy, you have to become familiar with its functions and routines. One of the reasons why Python developers outside academia are hesitant to do this is because there are a lot of them. For an exhaustive list, consult SciPy.org .

However, getting started with the basics is easy to do. Knowing that NumPy establishes an N-dimensional matrix for elements of the same type, you can immediately begin working with its array functions.

NumPy refers to dimensions as axes . Keep this in mind while familiarizing yourself with the following functions:

ndarray.ndim refers to the number of axes in the current array. ndarray.shape defines the dimensions of the array. As mentioned earlier, NumPy uses the tuple of integers to indicate the size of arrays on each axis. This means that a matrix with n rows along m columns, shape is defined as (n,m) . Its length of the shape tuple is equal to ndarray.ndim . ndarray.size counts the number of elements that make up the array. It will be equal to the product of the individual elements in ndarray.shape . ndarray.dtype describes the elements located in the array using standard Python element types or NumPy's special types, such as numpy.in32 or numpy.float64 . ndarray.itemsize refers to the size of each element in the array, measured in bytes. This is how you determine how much NumPy has actually saved you in terms of storage space.

Using these functions to describe an array in NumPy would look something like this:

>>> import numpy as np

>>> a = np.arange(15).reshape(3, 5)

>>> a

array( [[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])

>>> a.shape

(3, 5)

>>> a.ndim

2

>>> a.dtype.name

'int64'

>>> a.itemsize

8

>>> a.size

15

>>> type(a)

<type 'numpy.ndarray'>

>>> b = np.array([6, 7, 8])

>>> b

array([6, 7, 8])

>>> type(b)

<type 'numpy.ndarray'>

The example defines an array as a and then identifies the size, shape, and type of its elements and axes.

How to Create Arrays

Since NumPy is all about creating and indexing arrays, it makes sense that there would be multiple ways to create new arrays. You can create arrays out of regular Python lists and create new arrays comprised of 1s and 0s as placeholder content.

Creating an Array from a Python List

If you have a regular Python list or a tuple that you would like to call using a NumPy array, you can create an array out of the types of elements in the called sequences. This would look like the following example:

>>> import numpy as np

>>> a = np.array([2,3,4])

>>> a

array([2, 3, 4])

>>> a.dtype

dtype('int64')

>>> b = np.array([1.2, 3.5, 5.1])

>>> b.dtype

dtype('float64')

In this example, there is a specific format for calling the np.array that many NumPy beginners get wrong. Notice the parenthesis and the brackets around the list of numbers that comprise the argument:

>>> a = np.array([x,y,z])

Most coders new to NumPy will only use parentheses, which establishes multiple numeric arguments. This will result in a botched array and potentially many hours of frustrated debugging followed a final "ah-hah!" moment.

Understanding how np.array works is actually quite simple. It will transform sequences of sequences into a two-dimensional array. It will transform sequences of sequences of sequences into a three-dimensional array, working in the same way to the n th degree.

This is one of the main ways that NumPy actually delivers on i

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:A Complete Guide To NumPy Functions in Python For Beginners
本站链接:https://www.codesec.net/view/621365.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(75)