Database Reference
In-Depth Information
all, NumPy provides a new data type: a multidimensional array. The NumPy array
data type uses contiguous memory, and operations are implemented in the C language
for raw speed. As a result, operations on this data type, even over very large datasets,
are very fast.
In order to provide the raw speed necessary to operate on large, multidimensional
arrays, NumPy imposes a few restrictions that might be unfamiliar to users of the very
f lexible core Python data types. First of all, once a NumPy array is defined, it can't be
altered directly. (You can, however, create new arrays as a result of slicing and math-
ematical operations.) Unlike regular Python lists, NumPy arrays are homogeneous,
meaning that they can hold only one type of data; in other words, a NumPy array
can't represent both integer and f loating-point numbers at the same time.
Another useful feature of NumPy arrays is that operations are broadcast to every
element in the array at once. It's possible to run calculations across the entire data-
set at once, rather than being forced to manually loop through every field separately.
Even better, it's possible to broadcast an operation involving multiple arrays. You can
multiply all the values in respective cell locations of two arrays together in a single
command.
The examples in Listing 12.1 demonstrate the basics of using the NumPy array data
type. A simple two-dimensional array can be generated by using two lists containing
the same type of data. In the example that follows, we initialize our array with integer
values, but we have defined the array as having f loating-point numbers.
Listing 12.1 NumPy fundamentals
# Conventional method of importing NumPy module
> import numpy as np
# Initialize a NumPy array
sample_array = np.array([[3, 4, 5],
[6, 7, 8]], float)
# Returns the value of cell 0,1
> print sample_array[0,1]
4.0
# Generate a 3x3 array of random sample data
> np.random.random_sample((3, 3))
array([[ 0.73292341, 0.22336877, 0.71491504],
[ 0.54102249, 0.47380184, 0.40844874],
[ 0.8506573 , 0.23022121, 0.19739788]])
# Broadcast examples
# Multiply every cell by 3
> print sample_array * 3
[[ 9. 12. 15.]
[ 18. 21. 24.]]
 
Search WWH ::




Custom Search