You cannot use boolean indexing this way on multiple axes but you can work

You cannot use boolean indexing this way on multiple

This preview shows page 25 - 30 out of 45 pages.

You cannot use boolean indexing this way on multiple axes, but you can work around this by using the ix_ function: In [106]: In [107]: If you use a boolean array that has the same shape as the ndarray , then you get in return a 1D array containing all the values that have True at their coordinate. This is generally used along with conditional operators: In [108]: Iterating Iterating over ndarray s is very similar to iterating over regular python arrays. Note that iterating over multidimensional arrays is done with respect to the first axis. Out[105]: array([[ 1, 4, 7, 10], [13, 16, 19, 22], [25, 28, 31, 34], [37, 40, 43, 46]]) Out[106]: array([[ 1, 4, 7, 10], [25, 28, 31, 34]]) Out[107]: (array([[0], [2]]), array([[ 1, 4, 7, 10]])) Out[108]: array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 4 6]) cols_on = np.array([ False , True , False ] * 4 ) b[:, cols_on] # All rows, columns 1, 4, 7 and 10 b[np.ix_(rows_on, cols_on)] np.ix_(rows_on, cols_on) b[b % 3 == 1 ]
In [109]: In [110]: In [111]: If you want to iterate on all elements in the ndarray , simply iterate over the flat attribute: Out[109]: 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]]]) Item: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Item: [[12 13 14 15] [16 17 18 19] [20 21 22 23]] Item: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Item: [[12 13 14 15] [16 17 18 19] [20 21 22 23]] c = np.arange( 24 ).reshape( 2 , 3 , 4 ) # A 3D array (composed of two 3x4 matrices) c for m in c: print ( "Item:" ) print (m) for i in range ( len (c)): # Note that len(c) == c.shape[0] print ( "Item:" ) print (c[i])
In [112]: Stacking arrays It is often useful to stack together different arrays. NumPy offers several functions to do just that. Let's start by creating a few arrays. In [113]: In [114]: Item: 0 Item: 1 Item: 2 Item: 3 Item: 4 Item: 5 Item: 6 Item: 7 Item: 8 Item: 9 Item: 10 Item: 11 Item: 12 Item: 13 Item: 14 Item: 15 Item: 16 Item: 17 Item: 18 Item: 19 Item: 20 Item: 21 Item: 22 Item: 23 Out[113]: array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) Out[114]: array([[2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.]]) for i in c.flat: print ( "Item:" , i) q1 = np.full(( 3 , 4 ), 1.0 ) q1 q2 = np.full(( 4 , 4 ), 2.0 ) q2
In [115]: vstack Now let's stack them vertically using vstack : In [116]: In [117]: This was possible because q1, q2 and q3 all have the same shape (except for the vertical axis, but that's ok since we are stacking on that axis). hstack We can also stack arrays horizontally using hstack : In [118]: Out[115]: array([[3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.]]) Out[116]: array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.], [3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.]]) Out[117]: (10, 4) Out[118]: array([[1., 1., 1., 1., 3., 3., 3., 3.], [1., 1., 1., 1., 3., 3., 3., 3.], [1., 1., 1., 1., 3., 3., 3., 3.]]) q3 = np.full(( 3 , 4 ), 3.0 ) q3 q4 = np.vstack((q1, q2, q3)) q4 q4.shape q5 = np.hstack((q1, q3)) q5
In [119]: This is possible because q1 and q3 both have 3 rows. But since q2 has 4 rows, it cannot be stacked horizontally with q1 and q3: In [120]: concatenate The concatenate function stacks arrays along any given existing axis. In [121]: In [122]: As you might guess, hstack is equivalent to calling concatenate with axis=1 . stack The stack function stacks arrays along a new axis. All arrays have to have the same shape. Out[119]: (3, 8) all the input array dimensions except for the concatenation axis must match exactly Out[121]: array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.], [2., 2., 2., 2.], [3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.]]) Out[122]: (10, 4) q5.shape try : q5 = np.hstack((q1, q2, q3)) except ValueError as e: print (e) q7 = np.concatenate((q1, q2, q3), axis = 0 ) # Equivalent to vstack q7 q7.shape

You've reached the end of your free preview.

Want to read all 45 pages?

• Fall '16
• Array, NumPy, ndarray, 人工智能