An efficient way to create an array is to use a generator ...

Creating arrays from iterators and functions:

An efficient way to create an array is to use a generator G

>>> G=(i**2 for i in range(10)) >>> a=np.fromiter(G,dtype=int) >>> a array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])

There is also np.fromfunction:

def f(i,j): return i == j

def g(i,j): return i+j

ba=np.fromfunction(f,(3,3)) ca=np.fromfunction(g,(3,3))

>>> ba array([[ True, False, False],

[False, True, False], [False, False, True]])

>>> ca array([[0., 1., 2.],

[1., 2., 3.], [2., 3., 4.]])

>>> da=np. fromfunction(g,(3,3),dtype=int) >>> da array([[0, 1, 2],

[1, 2, 3], [2, 3, 4]])

11/10/2019

1

Broadcasting

Broadcasting allows universal functions to deal in a meaningful way with inputs that do not have exactly the same shape.

--------The first rule: if all input arrays do not have the same number of dimensions, a "1" will be repeatedly prepended to the shapes of the smaller arrays until all the arrays have the same number of dimensions.

The second rule: arrays with a size of 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is assumed to be the same along that dimension for the "broadcast" array.

After application of the broadcasting rules, the sizes of all arrays must match. -----

Examples: The simplest one:

>>> a=np.array([1]) >>> b=np.array([7,8,9]) >>> a+b array([ 8, 9, 10])

a

1-dim

1

b

1-dim

3

By the second rule, a acts on the first dimension "as if" it was 3 (the largest shape along that dimension). Therefore, a and b have now the same shape (3,), which will be the shape of the result. Again, by the second rule, the value present on the 1 dimension broadcast, so that we consider a as

array([ 1, 1, 1])

and now the sum is taken elementwise.

Another, more interesting, example:

>>> a=np.array([[1,2,3],[4,5,6]]) >>> b=np.array([7,8,9]) >>> a.shape (2, 3) >>> b.shape (3,)

a

2-dim 2 x 3

b

1-dim

3

11/10/2019

2

By the first rule, the shape of b is first extended to 1x3. By the second rule, b acts on the first dimension "as if" it was 2 (the largest shape along that dimension). Therefore, a and b have now the same shape (2,3), which will be the shape of the result. Again, by the second rule, the value of the only "real" row of b is broadcast, so that we consider b as

array([7, 8, 9], [7, 8, 9])

At this point the sum is performed element-wise:

>>> a+b array([[ 8, 10, 12],

[11, 13, 15]])

[The following essentially from ]

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions and works its way forward. Two dimensions are compatible when

1. they are equal, or 2. one of them is 1

If these conditions are not met, a ValueError: operands could not be broadcast together exceptio n is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the maximum size along each dimension of the input arrays.

Arrays do not need to have the same number of dimensions. For example, if you have a 256x256x3 array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that they are compatible:

Image (3d array): 256 x 256 x 3

Scale (1d array):

3

Result (3d array): 256 x 256 x 3

When either of the dimensions compared is one, the other is used. In other words, dimensions with size 1 are stretched or "copied" to match the other.

[Simo: observe that, except for the case of shape (1,), a dimension 1 does not contain relevant data: it is merely an outer level of nesting of [].

For (1,) on the contrary, we have data:

array([10]) array([3.1415])

An array of shape (2,1,3):

11/10/2019

3

array([[[0, 1, 2]], [[3, 4, 5]]])

Compare: an array of shape (2,3):

array([[0, 1, 2], [3, 4, 5]])

An array of shape (2,1,3,1):

array([[[[0], [1], [2]]],

[[[3], [4], [5]]]])

Broadcasting provides a convenient way of taking the outer product (or any other outer operation) of two arrays. The following example shows an outer addition operation of two 1-d arrays:

>>> a = np.array([0.0, 10.0, 20.0, 30.0]) >>> b = np.array([1.0, 2.0, 3.0]) >>> a[:, np.newaxis] + b array([[ 1., 2., 3.],

[ 11., 12., 13.], [ 21., 22., 23.], [ 31., 32., 33.]])

Here the newaxis index operator inserts a new axis into a, making it a twodimensional 4x1 array. Combining the 4x1 array with b, which has shape (3,), yields a 4x3 array.

11/10/2019

4

Indexing and slicing

A Numpy array is indexed like a Python sequence, but n-dim arrays may be referred with n-indexes inside a single pair of square brackets.

>>> x=np.array([[1,2,3,4],[5,6,7,8]])

array([[1, 2, 3, 4],

[5, 6, 7, 8]])

>>> x[0][2]

3

>>> x[0,2]

#more efficient; x[0][2] first construct

3

#the array of the second component

>>> x[0]

array([1, 2, 3, 4])

>>> x[0]=np.array([5,5,5,5])

>>> x

array([[5, 5, 5, 5],

[5, 6, 7, 8]])

Slicing

>>> x = np.arange(10)

>>> x[2:5]

array([2, 3, 4])

>>> x[:-7]

array([0, 1, 2])

>>> x[1:7:2]

array([1, 3, 5])

>>> y = np.arange(35).reshape(5,7)

>>> y

array([[ 0, 1, 2, 3, 4, 5, 6],

[ 7, 8, 9, 10, 11, 12, 13],

[14, 15, 16, 17, 18, 19, 20],

[21, 22, 23, 24, 25, 26, 27],

[28, 29, 30, 31, 32, 33, 34]])

>>> y[1:5:2,::3]

array([[ 7, 10, 13],

[21, 24, 27]])

>>> y[:,1]

# the second (ie, index 1) column of y

array([ 1, 8, 15, 22, 29])

You may use slicing to set values in the array, but (unlike lists) you can never make the array grow.

Example from

a= np.arange(6) + np.arange(0, 51, 10)[:, np.newaxis]

11/10/2019

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download