Numeric and Numarray packages in Python tutorial _python

Source: Internet
Author: User
Tags exception handling scalar python list

The first thing to understand about the numerical Python package is that numerical python will not let you do any work that standard Python cannot do. It just allows you to do the same tasks that standard Python can accomplish with much faster speed. More than that, many array operations are much more elegant to express in Numeric or Numarray than by using standard Python data types and syntax. However, the astonishing speed is the main reason to attract users to use numerical Python.

In fact, numerical Python just implements a new data type: an array. Unlike lists, tuples, and dictionaries that can contain different types of elements, a Numarray array can contain only the same type of data. Another advantage of the Numarray array is that it can be multidimensional-but the dimensions of the array are slightly different from the simple nesting of the lists. Numerical Python draws on the programmer's hands-on experience (especially those with a scientific computing background that abstracts the best features of the array in APL, FORTRAN, MATLAB, and S), creating arrays that can flexibly change shapes and dimensions. We'll be back soon to continue the subject.

The operations of an array in numerical Python are based on elements. Although two-dimensional arrays are similar to matrices in linear algebra, their operations (such as multiplication) and operations in linear algebra (such as matrix multiplication) are completely different.

Let's take a look at a concrete example of the above question. In pure Python, you can create a two-dimensional list like this:
Listing 1. Nested Arrays of Python

>>> Pyarr = [[1,2,3],
...     [4,5,6],
...     [7,8,9]]
>>> print Pyarr
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> pyarr[1][1] = 0
>>> print pya RR
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]

Good, but what you can do with this structure is simply to set and retrieve the elements through a separate (or multidimensional) index. Numarray arrays are more flexible than this:
Listing 2. Numerical Python Array

>>> from numarray import *
>>> Numarr = Array (pyarr)
>>> print Numarr
[[1 2 3]
   
    [4 0 6]
 [7 8 9]]


   

It's not a big change, but what about the operation with Numarray? Here is an example:
Listing 3. Element Operations

>>> NUMARR2 = Numarr * 2
>>> print numarr2
[[2 4 6]
 [8 0]
 []]
> >> print NUMARR2 + Numarr
[[3 6 9]
 [0]
 [21 24 27]]

To change the shape of an array:
listing 4. Changing Shape

>>> Numarr2.shape = (9,)
>>> print NUMARR2
[2 4 6 8 0 12 14 16 18]

The difference between Numeric and Numarray

Overall, the new Numarray package is API-compatible with the early Numeric. However, developers have made some improvements that are incompatible with numric based on user experience. Instead of destroying any application that relies on Numeric, developers have created a new project called Numarray. While completing this article, Numarray also lacks some of the features of Numeric, but it is planned to implement these features.

Some improvements made by Numarray:

    • organizes element types in a layered class structure to support Isinstance () validation. Numeric only uses character type encoding when specifying a data type (but the initialization software in Numarray still accepts the old character encoding).
    • The type coercion rule was changed to keep the type in the array (more common) rather than to the type of the Python scalar.
    • Additional array properties appear (no longer only getter and setter).
    • Enables more flexible exception handling.

New users don't have to worry about these changes, so it's best to start with Numarray rather than Numeric.

Example of the timing

Let's take a look at the speed advantage of the operations in numerical Python relative to standard python. As a "demo task," We'll create a sequence of numbers and then double them. First, some variants of the standard Python method:
Listing 5. Timing of pure Python operations

def timer (fun, N, comment= ""): From time
  import clock
  start = Clock ()
  print comment, Len (fun (n)), "Elements",
  print ' in%.2f seconds '% (clock ()-start)
def double1 (n): Return map (Lambda n:2*n, xrange (n))
timer (double1 , 5000000, "Running map () on xrange iterator:")
def double2 (n): return [2*n to N in Xrange (n)]
timer (double2, 500 0000, "Running listcomp on Xrange iter:")
def double3 (n):
  double = [] for
  N in xrange (n):
    Double.appen D (2*n) return
  double
timer (double3, 5000000, "building new list from iterator:")

We can see the speed difference between the map () method, List comprehension, and the traditional loop method. So, do you need a standard array module of the same type of element? It might be quicker:
listing 6. Timing of the standard array module

Import Array
def double4 (n): return [2*n to N in Array.array (' I ', Range (n)]
timer (double4, 5000000, "Running l Istcomp on Array.array: ")

Finally, let's see how fast Numarray is. As an extra comparison, let's see whether it has the same advantages if you have to revert an array to a standard list:
Listing 7. Timing of the Numarray operation

From Numarray import *
def double5 (n): Return 2*arange (n)
timer (double5, 5000000, "Numarray scalar Multiplication: ")
def double6 (n): Return (2*arange (n)). ToList ()
timer (double6, 5000000," Numarray mult, Returning list:  ")

Now run it:
Listing 8. Comparison results

$ python2.3 timing.py
Running map () on xrange iterator:5000000 elements at 13.61 seconds Running listcomp on
XR Ange iter:5000000 elements in 16.46 seconds
building new list to iterator:5000000 elements in 20.13
seconds Running Listcomp on array.array:5000000 elements in 25.58 seconds Numarray scalar multiplication
:  5000000 Elem Ents in 0.61 seconds
Numarray mult, returning list:  5000000 elements in 3.70

The speed difference between the different technologies that deal with the list is small and perhaps noteworthy, since this is a methodological problem when trying the standard array module. But Numarray typically takes less than 1/20 of the time to complete the operation. Restoring an array to the standard list has lost a great speed advantage.

No conclusion should be drawn through such a simple comparison, but this acceleration may be typical. For large scale scientific calculations, it is very valuable to reduce the computation time from several months to a few days or a few days.

System modeling

A typical use case for numerical Python is scientific modeling, or it may be related to areas such as graphics processing and rotation, or signal processing. I will illustrate many of the features of Numarray through a more practical question. Suppose you have a three-dimensional physical space with variable parameters. In an abstract way, any parametric space, no matter how many dimensions, Numarray is applicable. It's really easy to imagine, for example, a room where the temperature of each point is different. My home in New England has been winter, so the question seems more relevant.

For the sake of simplicity, the following example I have given is using a smaller array (although this may be obvious, it is still necessary to point out clearly). However, even if you are dealing with an array of millions of elements rather than just dozens of elements, Numarray is also very fast; the former may be more common in real scientific models.

First, let's create a "room". There are many ways to accomplish this task, but the most common use is the callable array () method. Using this method, we can generate a numerical array with a variety of initialization parameters, including initial data from any Python sequence. But for our room, a zeros () function can produce a cold room with a uniform temperature:
Listing 9. Initialize the temperature of the room

>>> from numarray import *
>>> room = Zeros ((4,3,5), Float)
>>> print Room
[[0.0 . 0.0. 0.]
 [0. 0.0. 0.0.]
 [0.0. 0.0. 0.]]
 [[0. 0.0. 0.0.]
 [0.0. 0.0. 0.]
 [0. 0.0. 0.0.]]
 [[0.0. 0.0. 0.]
 [0. 0.0. 0.0.]
 [0.0. 0.0. 0.]]
 [[0. 0.0. 0.0.]
 [0.0. 0.0. 0.]
 [0. 0.0. 0.0.]]]

From top to bottom, each two-dimensional "matrix" represents a horizontal level of a three-dimensional room.

First, we increase the temperature of the entire room to a comfortable 70 degrees Fahrenheit (about 20 degrees Celsius):
listing 10. Turn on the heater

>>> room +
>>> print room [[
70. 70.70. .]
 [70.70. 70.70. ]
 [70. 70.70. .]]
 [[70.70. 70.70. ]
 [70. 70.70. .]
 [70.70. 70.70. ]
 [[70]. 70.70. .]
 [70.70. 70.70. ]
 [70. 70.70. .]]
 [[70.70. 70.70. ]
 [70. 70.70. .]
 [70.70. 70.70. 70.]]]

Note that there is a very important difference in our next operation on the Numarray array and the Python list. When you select the level of an array--we'll see that the layered approach in multidimensional arrays is very flexible and powerful--you get not a copy but a "view." There are several ways to point to the same data.

Let's look at it specifically. Let's say we have a ventilation unit in our room that will reduce the temperature of the ground by four degrees:
listing 11. Changes in temperature

>>> floor = room[3]
>>> Floor-= 4
>>> print Room
[[[70]. 70.70. .]
 [70.70. 70.70. ]
 [70. 70.70. .]]
 [[70.70. 70.70. ]
 [70. 70.70. .]
 [70.70. 70.70. ]
 [[70]. 70.70. .]
 [70.70. 70.70. ]
 [70. 70.70. .]]
 [[66.66. 66.66. "
 [66]." 66.66. .
 [66.66. 66.66. 66.]]]

In contrast, the fireplace on the north wall raised the temperature of each adjacent position by 8 degrees, while its location was at a temperature of 90 degrees.
listing 12. Heating with a fireplace

>>> north = room[:,0]
>>> near_fireplace = north[2:4,2:5]
>>> Near_fireplace + 8
>>> north[3,2] = # The Fireplace cell itself
>>> print Room
[[70. 70.70. .]
 [70.70. 70.70. ]
 [70. 70.70. .]]
 [[70.70. 70.70. ]
 [70. 70.70. .]
 [70.70. 70.70. ]
 [[70]. 78.78. .]
 [70.70. 70.70. ]
 [70. 70.70. .]]
 [[66.74. 90.74. "
 [66]." 66.66. .
 [66.66. 66.66. 66.]]]

Here we use some of the more ingenious indexing methods that can be specified in multidimensional directions. These views should be retained and will be used later. For example, you might want to know the current temperature on the entire north wall:
Listing 13. View the north wall

>>> print North
[70. 70.70. .]
 [70.70. 70.70. ]
 [70. 78.78. .]
 [66.74. 90.74. 66.]]

More action

These are just a few of the handy functions and arrays of methods/attributes in Numarray. I hope to give you some preliminary understanding; Numarray documentation is an excellent reference for in-depth learning.

Now that our room is no longer the same temperature, we may need to judge the state of the whole situation. For example, the average temperature in the current room:
listing 14. View the average array

>>> add.reduce (Room.flat)/len (Room.flat)
70.066666666666663

Here's what you need to explain. You can have a common function (UFUNC) for all operations performed on the array. So, the floor-= 4 we used in the previous code can be replaced with subtract (Floor,4,floor). Specify the three parameters of subtract (), and the operation can be completed correctly. You can also use Floor=subtract (floor,4) to create a copy of the floor, but this may not be what you expect because the change will occur in a new array, not in a child set of room.

However, Unfunc is more than just a function. They can also be callable objects with their own methods: the. reduce () is probably the most useful one. Reduce () works as a built-in function in Python, reduce (), and each operation is a basic ufunc (although these methods are much faster when applied to numerical arrays). In other words, add.reduce () represents sum (), and Multiply.reduce () represents product () (the shortcut names are also defined).

You need to get a one-dimensional view of the data before you ask for the temperature of each unit in the room. Otherwise, you get the first dimension of the and, and generate a new array that reduces the number of dimensions. For example:
listing 15. Error results for non-planar arrays

>>> add.reduce (room)
Array ([[276., 292., 308., 292., 276.], [276., 276., 276., 276., 276.
    ],
    [276., 276., 276., 276., 276.]]

Such a space and may be useful, but it is not what we want here.

Now that we're modeling a physical system, let's make it more realistic. There is a small airflow in the room which changes the temperature. In modeling, we can assume that each unit adjusts to the temperature around it for each small time period:
listing 16. Micro-airflow Simulation

>>> def equalize (room):
...  z,y,x = Map (Randint, (1,1,1), Room.shape) ...  Zmin,ymin,xmin = Maximum ([z-2,y-2,x-2],[0,0,0]). ToList ().  Zmax,ymax,xmax = [z+1,y+1,x+1]
...  region = Room[zmin:zmax,ymin:ymax,xmin:xmax].copy () ...  ROOM[Z-1,Y-1,X-1] = SUM (Region.flat)/len (Region.flat) ...  return room

The model is of course unrealistic: The unit does not adjust to the temperature around it and does not affect its adjacent cells. Still, let's take a look at what it does. First we select a random unit--or in fact, we select the value of the cell itself on each dimension plus 1, because we get the length instead of the maximum index value through the. Shape call. Zmin, ymin, and xmin ensure that our minimum index value is 0, not negative; Zmax, ymax, and xmax are not actually required because the size of each dimension of the array minus 1 is used as the maximum value (as in a list in Python).

Then, we need to define the area of the adjacent cell. Because our rooms are small, we often choose the surface, edge, or corner of the room-the region of the unit may be smaller than the maximum 27 element (3x3x3) subset. It doesn't matter; we just need to use the correct denominator to calculate the average. The new average temperature value is assigned to the previously randomly selected unit.

You can perform any number of averaging processes in your model. Only one unit is adjusted per call. Many times the call will use some parts of the room with the temperature gradually becoming average. Even if the array is dynamically changed, the equalize () function can return its array. This is useful when you only want to average a copy of the model:
listing 17. Executive Equalize ()

>>> Print Equalize (room.copy ())
[[[].    .    .   ]
 [A.    .
 [    .]    .    .   ]]
 [[A]    .    71.333333.
 [    .]    .    .   ]
 [A.    .    ]
 [[    .    .   ]
 [A.    .
 [    .]    .    .   ]]
 [[    the]. .    [A.   ]    .   .
 [A.    .    ]]   

Conclusion

This article only describes some of the features of Numarray. Its function is much more than that. For example, you can use a fill function to populate an array, which is useful for physical models. You can specify a subset of arrays not only through layers, but also through an indexed array--which allows you to not only manipulate discrete pieces of the array, but also--through the Take () function--to redefine the dimensions and shapes of the array in various ways.

Most of the actions I've described earlier are for scalar and array, and you can also perform operations between arrays, including those between arrays of different dimensions. This involves a lot of content, but it's intuitive to do all of these things through the API.

I encourage you to install Numarray and/or Numeric on your own system. It's not difficult to get started with, and the fast operation of the arrays it provides can be applied to a wide range of areas--often unexpected when you start.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.