Array Manipulation Functions ============================ .. currentmodule:: numpycore Functions for manipulating array shapes and combining arrays. Example ------- .. code-block:: python import numpycore as np arr = np.array([[1, 2, 3], [4, 5, 6]]) # Shape manipulation reshaped = np.reshape(arr, (3, 2)) flattened = np.ravel(arr) transposed = np.transpose(arr) # Joining stacked = np.stack([arr, arr]) concatenated = np.concatenate([arr, arr], axis=0) Shape Manipulation ------------------ .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Function - Description - Example * - reshape(a, newshape) - Give new shape to array - * - ravel(a) - Return contiguous flattened array - :ref:`View ` * - flatten(a) - Return flattened copy - * - squeeze(a) - Remove single-dimensional entries - * - expand_dims(a, axis) - Expand shape by inserting axis - * - atleast_1d(*arys) - Convert to at least 1-D - :ref:`View ` * - atleast_2d(*arys) - Convert to at least 2-D - :ref:`View ` * - atleast_3d(*arys) - Convert to at least 3-D - :ref:`View ` * - broadcast_to(array, shape) - Broadcast array to new shape - Transpose Operations -------------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Function - Description - Example * - transpose(a, axes) - Permute array dimensions - * - swapaxes(a, axis1, axis2) - Swap two axes - * - moveaxis(a, source, destination) - Move axes to new positions - * - rollaxis(a, axis, start) - Roll specified axis backwards - Joining Arrays -------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Function - Description - Example * - concatenate((a1, a2, ...), axis) - Join arrays along axis - * - stack(arrays, axis) - Join arrays along new axis - * - vstack(tup) - Stack arrays vertically (row-wise) - * - hstack(tup) - Stack arrays horizontally (column-wise) - * - dstack(tup) - Stack arrays depth-wise (3rd axis) - * - column_stack(tup) - Stack 1-D arrays as columns - * - row_stack(tup) - Stack arrays row-wise (alias for vstack) - Splitting Arrays ---------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Function - Description - Example * - split(ary, indices_or_sections, axis) - Split array into sub-arrays - * - array_split(ary, indices_or_sections, axis) - Split array (allows unequal division) - * - hsplit(ary, indices_or_sections) - Split horizontally (column-wise) - * - vsplit(ary, indices_or_sections) - Split vertically (row-wise) - * - dsplit(ary, indices_or_sections) - Split along third axis - Tiling and Repeating -------------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Function - Description - Example * - tile(A, reps) - Construct by repeating A - * - repeat(a, repeats, axis) - Repeat elements of array - Rearranging Elements -------------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Function - Description - Example * - flip(m, axis) - Reverse order of elements along axis - * - fliplr(m) - Flip left to right - * - flipud(m) - Flip up to down - * - rot90(m, k, axes) - Rotate 90 degrees - * - roll(a, shift, axis) - Roll elements along axis - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-manipulation-shape-ravel-0: .. dropdown:: ravel (test_array_creation_extended.py:431) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 421 :emphasize-lines: 11 nc_result = test_bind.np.flatnonzero(nc_arr) assert_arrays_equal(np_result, nc_result, "flatnonzero", atol=0) # ============================================================================ # NEW NUMPYCORE BINDINGS TESTS (ravel, meshgrid) # ============================================================================ def test_ravel_numpycore_1d(): """Test numpycore.ravel() with 1-D array""" np_arr = test_bind.np.array([1.0, 2.0, 3.0]) nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0]) np_result = test_bind.np.ravel(np_arr) nc_result = test_bind.numpycore.ravel(nc_arr) assert_arrays_equal(np_result, nc_result, "ravel_numpycore_1d") def test_ravel_numpycore_2d(): .. _example-manipulation-shape-atleast_1d-1: .. dropdown:: atleast_1d (test_array_creation_extended.py:512) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 502 :emphasize-lines: 11 np_xv, np_yv, np_zv = test_bind.np.meshgrid(np_x, np_y, np_z, indexing='xy') nc_xv, nc_yv, nc_zv = test_bind.numpycore.meshgrid(nc_x, nc_y, nc_z, indexing='xy') assert_arrays_equal(np_xv, nc_xv, "meshgrid_numpycore_3d_xv") assert_arrays_equal(np_yv, nc_yv, "meshgrid_numpycore_3d_yv") assert_arrays_equal(np_zv, nc_zv, "meshgrid_numpycore_3d_zv") def test_atleast_1d_numpycore(): """Test numpycore.atleast_1d()""" np_arr = test_bind.np.array([1.0, 2.0, 3.0]) nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0]) np_result = test_bind.np.atleast_1d(np_arr) nc_result = test_bind.numpycore.atleast_1d(nc_arr) assert_arrays_equal(np_result, nc_result, "atleast_1d_numpycore") def test_atleast_2d_numpycore(): .. _example-manipulation-shape-atleast_2d-2: .. dropdown:: atleast_2d (test_array_creation_extended.py:523) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 513 :emphasize-lines: 11 np_arr = test_bind.np.array([1.0, 2.0, 3.0]) nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0]) np_result = test_bind.np.atleast_1d(np_arr) nc_result = test_bind.numpycore.atleast_1d(nc_arr) assert_arrays_equal(np_result, nc_result, "atleast_1d_numpycore") def test_atleast_2d_numpycore(): """Test numpycore.atleast_2d()""" np_arr = test_bind.np.array([1.0, 2.0, 3.0]) nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0]) np_result = test_bind.np.atleast_2d(np_arr) nc_result = test_bind.numpycore.atleast_2d(nc_arr) assert_arrays_equal(np_result, nc_result, "atleast_2d_numpycore") def test_atleast_3d_numpycore(): .. _example-manipulation-shape-atleast_3d-3: .. dropdown:: atleast_3d (test_array_creation_extended.py:534) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 524 :emphasize-lines: 11 np_arr = test_bind.np.array([1.0, 2.0, 3.0]) nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0]) np_result = test_bind.np.atleast_2d(np_arr) nc_result = test_bind.numpycore.atleast_2d(nc_arr) assert_arrays_equal(np_result, nc_result, "atleast_2d_numpycore") def test_atleast_3d_numpycore(): """Test numpycore.atleast_3d()""" np_arr = test_bind.np.array([[1.0, 2.0], [3.0, 4.0]]) nc_arr = test_bind.numpycore.array([[1.0, 2.0], [3.0, 4.0]]) np_result = test_bind.np.atleast_3d(np_arr) nc_result = test_bind.numpycore.atleast_3d(nc_arr) assert_arrays_equal(np_result, nc_result, "atleast_3d_numpycore") # ============================================================================ .. _example-manipulation-rearranging-roll-4: .. dropdown:: roll (test_manipulation_phase17.py:66) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 56 :emphasize-lines: 11 # ============================================================================ # ROLL TESTS # ============================================================================ def test_roll_1d_positive(): """Test rolling 1D array with positive shift""" np_arr = test_bind.np.arange(0, 10, 1, dtype='float64') nc_arr = test_bind.numpycore.arange(0, 10, 1) np_result = test_bind.np.roll(np_arr, 2) nc_result = test_bind.numpycore.roll(nc_arr, 2) assert_arrays_equal(np_result, nc_result, "roll_1d_positive_shift") def test_roll_1d_negative(): """Test rolling 1D array with negative shift""" np_arr = test_bind.np.arange(0, 10, 1, dtype='float64') nc_arr = test_bind.numpycore.arange(0, 10, 1) np_result = test_bind.np.roll(np_arr, -2)