Array Operations¶
# import numpy
import numpy as np
Copy¶
Copies array to new memory
Syntax:
np.copy(array)
# create an array `A1`
A1 = np.arange(10)
print(A1)
[0 1 2 3 4 5 6 7 8 9]
# copy `A1` into A2
A2 = np.copy(A1)
print(A2)
[0 1 2 3 4 5 6 7 8 9]
View¶
Creates view of array elements with type(dtype)
Syntax:
array.view(np.dtype)
# view of array A2
A3 = A2.view(np.float16)
print(A3)
[0.0e+00 0.0e+00 0.0e+00 0.0e+00 6.0e-08 0.0e+00 0.0e+00 0.0e+00 1.2e-07
0.0e+00 0.0e+00 0.0e+00 1.8e-07 0.0e+00 0.0e+00 0.0e+00 2.4e-07 0.0e+00
0.0e+00 0.0e+00 3.0e-07 0.0e+00 0.0e+00 0.0e+00 3.6e-07 0.0e+00 0.0e+00
0.0e+00 4.2e-07 0.0e+00 0.0e+00 0.0e+00 4.8e-07 0.0e+00 0.0e+00 0.0e+00
5.4e-07 0.0e+00 0.0e+00 0.0e+00]
Sorting¶
Returns a sorted copy of an array.
Syntax:
array.sort()
element-wise sorting(default)
axis = 0; row
axis = 1; column
# Unsorted array
A4 = np.array([9, 2, 3,1, 5, 10])
print(A4)
[ 9 2 3 1 5 10]
# Call sort function
A4.sort()
print(A4)
[ 1 2 3 5 9 10]
# Row and column unsorted
A5 = np.array([[4, 1, 3], [9, 5, 8]])
print(A5)
[[4 1 3]
[9 5 8]]
A5[0]
array([4, 1, 3])
A5[0][1]
1
A5[1]
array([9, 5, 8])
A5[1][2]
8
# Apply sort function on column axis=1
A5.sort(axis=1)
print(A5)
[[1 3 4]
[5 8 9]]
# Apply sort function on row axis=0
A5.sort(axis=0)
print(A5)
[[1 3 4]
[5 8 9]]
Flatten: Flattens 2D array to 1D array¶
A6 = np.array([[4, 1, 3], [9, 5, 8]])
A6
array([[4, 1, 3],
[9, 5, 8]])
# 2D array
A6 = np.array([[4, 1, 3], [9, 5, 8]])
# 1D array
A6.flatten()
array([4, 1, 3, 9, 5, 8])
Transpose: Transposes array (rows become columns and vice versa)¶
A7 = np.array([[4, 1, 3], [9, 5, 8]])
A7
array([[4, 1, 3],
[9, 5, 8]])
# Transpose A7
A7.T
array([[4, 9],
[1, 5],
[3, 8]])
Reshape: Reshapes arr to r
rows, c
columns without changing data¶
A8 = np.array([(8,9,10),(11,12,13)])
A8
array([[ 8, 9, 10],
[11, 12, 13]])
# Reshape --> 3x4
A8.reshape(3,2)
array([[ 8, 9],
[10, 11],
[12, 13]])
Resize: Changes arr shape to rxc
and fills new values with 0¶
A9 = np.array([(8,9,10),(11,12,13)])
A9
array([[ 8, 9, 10],
[11, 12, 13]])
# Resize
A9.resize(3, 2)
A9
array([[ 8, 9],
[10, 11],
[12, 13]])
np.info(np.resize)
resize(a, new_shape)
Return a new array with the specified shape.
If the new array is larger than the original array, then the new
array is filled with repeated copies of `a`. Note that this behavior
is different from a.resize(new_shape) which fills with zeros instead
of repeated copies of `a`.
Parameters
----------
a : array_like
Array to be resized.
new_shape : int or tuple of int
Shape of resized array.
Returns
-------
reshaped_array : ndarray
The new array is formed from the data in the old array, repeated
if necessary to fill out the required number of elements. The
data are repeated in the order that they are stored in memory.
See Also
--------
np.reshape : Reshape an array without changing the total size.
np.pad : Enlarge and pad an array.
np.repeat: Repeat elements of an array.
ndarray.resize : resize an array in-place.
Notes
-----
When the total size of the array does not change `~numpy.reshape` should
be used. In most other cases either indexing (to reduce the size)
or padding (to increase the size) may be a more appropriate solution.
Warning: This functionality does **not** consider axes separately,
i.e. it does not apply interpolation/extrapolation.
It fills the return array with the required number of elements, taken
from `a` as they are laid out in memory, disregarding strides and axes.
(This is in case the new shape is smaller. For larger, see above.)
This functionality is therefore not suitable to resize images,
or data where each axis represents a separate and distinct entity.
Examples
--------
>>> a=np.array([[0,1],[2,3]])
>>> np.resize(a,(2,3))
array([[0, 1, 2],
[3, 0, 1]])
>>> np.resize(a,(1,4))
array([[0, 1, 2, 3]])
>>> np.resize(a,(2,4))
array([[0, 1, 2, 3],
[0, 1, 2, 3]])