2 Operations On Numpy Arrays
2 Operations On Numpy Arrays
2 Operations On Numpy Arrays
Manipulate arrays
Reshape arrays
Stack arrays
Perform operations on arrays
Perform basic mathematical operations
Apply built-in functions
Apply your own functions
Apply basic linear algebra operations
Manipulating Arrays
Let's look at some ways to manipulate arrays, i.e. changing the shape, combining and splitting
arrays, etc.
Reshaping Arrays
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Numpy/2_Operations_on_Numpy_Arrays.ipynb 1/6
1/4/2019 2_Operations_on_Numpy_Arrays
Stacking is done using the np.hstack() and np.vstack() methods. For horizontal stacking,
the number of rows should be the same, while for vertical stacking, the number of columns should
be the same.
print(array_1)
print("\n")
print(array_2)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
In [6]: # vstack
# Note that np.vstack(a, b) throws an error - you need to pass the arrays as a lis
np.vstack((array_1, array_2))
Similarly, two arrays having the same number of rows can be horizontally stacked using
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Numpy/2_Operations_on_Numpy_Arrays.ipynb 2/6
1/4/2019 2_Operations_on_Numpy_Arrays
np.hstack((a, b)) .
Numpy provides almost all the basic math functions - exp, sin, cos, log, sqrt etc. The function is
applied to each element of the array.
You can also apply your own functions on arrays. For e.g. applying the function x/(x+1) to each
element of an array.
One way to do that is by looping through the array, which is the non-numpy way. You would rather
want to write vectorized code.
The simplest way to do that is to vectorize the function you want, and then apply it on the array.
Numpy provides the np.vectorize() method to vectorize functions.
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Numpy/2_Operations_on_Numpy_Arrays.ipynb 3/6
1/4/2019 2_Operations_on_Numpy_Arrays
In [8]: print(a)
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
This also has the advantage that you can vectorize the function once, and then apply it as many
times as needed.
NumPy provides the np.linalg package to apply common linear algebra operations, such as:
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Numpy/2_Operations_on_Numpy_Arrays.ipynb 4/6
1/4/2019 2_Operations_on_Numpy_Arrays
NAME
numpy.linalg
DESCRIPTION
Core Linear Algebra Tools
-------------------------
Linear algebra basics:
Tensor operations:
Exceptions:
PACKAGE CONTENTS
_umath_linalg
info
lapack_lite
linalg
setup
DATA
absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0...
division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192...
print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...
FILE
c:\users\pratika\appdata\local\programs\python\python35-32\lib\site-packag
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Numpy/2_Operations_on_Numpy_Arrays.ipynb 5/6
1/4/2019 2_Operations_on_Numpy_Arrays
es\numpy\linalg\__init__.py
[[1 2 3]
[4 5 6]
[7 8 9]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
In [14]: # Inverse
np.linalg.inv(a)
In [15]: # Determinant
np.linalg.det(a)
Out[15]: 6.6613381477509402e-16
http://localhost:8888/notebooks/Desktop/Biplab/My%20Python/Numpy/2_Operations_on_Numpy_Arrays.ipynb 6/6