Array Manipulation Functions#
Functions for manipulating array shapes and combining arrays.
Example#
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#
Function |
Description |
Example |
|---|---|---|
reshape(a, newshape) |
Give new shape to array |
|
ravel(a) |
Return contiguous flattened array |
|
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 |
|
atleast_2d(*arys) |
Convert to at least 2-D |
|
atleast_3d(*arys) |
Convert to at least 3-D |
|
broadcast_to(array, shape) |
Broadcast array to new shape |
Transpose Operations#
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#
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#
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#
Function |
Description |
Example |
|---|---|---|
tile(A, reps) |
Construct by repeating A |
|
repeat(a, repeats, axis) |
Repeat elements of array |
Rearranging Elements#
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 |
Code Examples#
The following examples are extracted from the test suite.
ravel (test_array_creation_extended.py:431)
421 nc_result = test_bind.np.flatnonzero(nc_arr)
422
423 assert_arrays_equal(np_result, nc_result, "flatnonzero", atol=0)
424
425
426# ============================================================================
427# NEW NUMPYCORE BINDINGS TESTS (ravel, meshgrid)
428# ============================================================================
429
430def test_ravel_numpycore_1d():
431 """Test numpycore.ravel() with 1-D array"""
432 np_arr = test_bind.np.array([1.0, 2.0, 3.0])
433 nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0])
434
435 np_result = test_bind.np.ravel(np_arr)
436 nc_result = test_bind.numpycore.ravel(nc_arr)
437
438 assert_arrays_equal(np_result, nc_result, "ravel_numpycore_1d")
439
440
441def test_ravel_numpycore_2d():
atleast_1d (test_array_creation_extended.py:512)
502 np_xv, np_yv, np_zv = test_bind.np.meshgrid(np_x, np_y, np_z, indexing='xy')
503 nc_xv, nc_yv, nc_zv = test_bind.numpycore.meshgrid(nc_x, nc_y, nc_z, indexing='xy')
504
505 assert_arrays_equal(np_xv, nc_xv, "meshgrid_numpycore_3d_xv")
506 assert_arrays_equal(np_yv, nc_yv, "meshgrid_numpycore_3d_yv")
507 assert_arrays_equal(np_zv, nc_zv, "meshgrid_numpycore_3d_zv")
508
509
510def test_atleast_1d_numpycore():
511 """Test numpycore.atleast_1d()"""
512 np_arr = test_bind.np.array([1.0, 2.0, 3.0])
513 nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0])
514
515 np_result = test_bind.np.atleast_1d(np_arr)
516 nc_result = test_bind.numpycore.atleast_1d(nc_arr)
517
518 assert_arrays_equal(np_result, nc_result, "atleast_1d_numpycore")
519
520
521def test_atleast_2d_numpycore():
atleast_2d (test_array_creation_extended.py:523)
513 np_arr = test_bind.np.array([1.0, 2.0, 3.0])
514 nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0])
515
516 np_result = test_bind.np.atleast_1d(np_arr)
517 nc_result = test_bind.numpycore.atleast_1d(nc_arr)
518
519 assert_arrays_equal(np_result, nc_result, "atleast_1d_numpycore")
520
521
522def test_atleast_2d_numpycore():
523 """Test numpycore.atleast_2d()"""
524 np_arr = test_bind.np.array([1.0, 2.0, 3.0])
525 nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0])
526
527 np_result = test_bind.np.atleast_2d(np_arr)
528 nc_result = test_bind.numpycore.atleast_2d(nc_arr)
529
530 assert_arrays_equal(np_result, nc_result, "atleast_2d_numpycore")
531
532
533def test_atleast_3d_numpycore():
atleast_3d (test_array_creation_extended.py:534)
524 np_arr = test_bind.np.array([1.0, 2.0, 3.0])
525 nc_arr = test_bind.numpycore.array([1.0, 2.0, 3.0])
526
527 np_result = test_bind.np.atleast_2d(np_arr)
528 nc_result = test_bind.numpycore.atleast_2d(nc_arr)
529
530 assert_arrays_equal(np_result, nc_result, "atleast_2d_numpycore")
531
532
533def test_atleast_3d_numpycore():
534 """Test numpycore.atleast_3d()"""
535 np_arr = test_bind.np.array([[1.0, 2.0], [3.0, 4.0]])
536 nc_arr = test_bind.numpycore.array([[1.0, 2.0], [3.0, 4.0]])
537
538 np_result = test_bind.np.atleast_3d(np_arr)
539 nc_result = test_bind.numpycore.atleast_3d(nc_arr)
540
541 assert_arrays_equal(np_result, nc_result, "atleast_3d_numpycore")
542
543
544# ============================================================================
roll (test_manipulation_phase17.py:66)
56# ============================================================================
57# ROLL TESTS
58# ============================================================================
59
60def test_roll_1d_positive():
61 """Test rolling 1D array with positive shift"""
62 np_arr = test_bind.np.arange(0, 10, 1, dtype='float64')
63 nc_arr = test_bind.numpycore.arange(0, 10, 1)
64
65 np_result = test_bind.np.roll(np_arr, 2)
66 nc_result = test_bind.numpycore.roll(nc_arr, 2)
67
68 assert_arrays_equal(np_result, nc_result, "roll_1d_positive_shift")
69
70
71def test_roll_1d_negative():
72 """Test rolling 1D array with negative shift"""
73 np_arr = test_bind.np.arange(0, 10, 1, dtype='float64')
74 nc_arr = test_bind.numpycore.arange(0, 10, 1)
75
76 np_result = test_bind.np.roll(np_arr, -2)