This article discusses some of the more advanced techniques available in NumPy.

Output:
Vertical stacking: [[1 2] [3 4] [5 6] [7 8]] Horizontal stacking: [[1 2 5 6] [3 4 7 8]] Column stacking: [[1 2 5] [3 4 6]] Concatenating to 2nd axis: [[1 2 5 6] [3 4 7 8]]

Output:
Splitting along horizontal axis into 2 parts: [array ([[1, 3, 5], [2, 4, 6]]), array ([[7, 9, 11], [8, 10, 12]])] Splitting along vertical axis into 2 parts : [array ([[1, 3, 5, 7, 9, 11]]), array ([[2, 4, 6, 8, 10, 12]])]
Broadcast provides a means of vectorizing array operations, so the looping happens in C instead of Python. This does this without making unnecessary copies of the data and usually results in an efficient implementation of the algorithm. There are also cases where broadcasting — a bad idea because it leads to inefficient use of memory, which slows down computations.
NumPy operations are usually elementwise, which requires two arrays to have the same shape. Numpy`s broadcast rule relaxes this restriction when array shapes meet certain restrictions.
Broadcast rule: For broadcast, the size of the back axes for both arrays in an operation must be either the same size or one of there should be one .
Let`s see some examples:
A (2D array): 4 x 3 B (1D array): 3 Result: 4 x 3
A (4D array): 7 x 1 x 6 x 1 B (3D array): 3 x 1 x 5 Result: 7 x 3 x 6 x 5
But that would be a mismatch:
A: 4 x 3 B: 4
The simplest broadcast example occurs when an array and a scalar value are combined in an operation.
Consider the example below:

Output:
[2. 4. 6.] [2. 4. 6.]
We can think of scalar b being stretched during arithmetic operation into an array of the same shape as a. The new elements in b, as shown in the figure above, are just copies of the original scalar. Although the analogy with stretching is only conceptual in nature.
Numpy is smart enough to use the original scalar value without actually copying, so that broadcast operations are as fast and computationally efficient as possible. Since example 1 moves less memory during the multiplication (b — is a scalar, not an array), this is about 10% faster than example 2, which uses standard numpy on Windows 2000 with a millionth array of elements!
The figure below makes the concept clearer:
In the above example, scalar b is stretched to become an array with the same shape as and a so that the shapes are compatible for elementwise multiplication.
Now let`s look at an example where both arrays are stretched.

Output:
[[0. 1. 2.] [10. 11. 12.] [20. 21. 22.] [30. 31. 32.]]
In some cases, broadcast stretches both arrays to form an output array larger than either of the initial arrays.

Output:
Date is: 20170212 Year is: 2017 Dates of February, 2017: [`20170201`` 20170202` `20170203` `20170204`` 20170205`` 20170206`` 20170207`` 20170208`` 20170209`` 20170210`` 20170211`` 20170212`` 20170213`` 20170214`` 20170215`` 20170216`` 20170217`` 2017 0218`` 20170219`` 20170220`` 20170221`` 20170222`` 20170223`` 20170224`` 2017 0225`` 20170226`` 20170227`` 20170228`] Today is February: True No. of days: 365 days No. of weeks: 52 weeks Dates in sorted order: [`20161013`` 20170212` `20190522`]
Consider the example below that explains how we we can use NumPy to do some matrix operations.

Output:
Rank of A: 3 Trace of A: 11 Determinant of A: 306.0 Inverse of A: [[0.17647059 0.00326797 0.02287582] [0.05882353 0.13071895 0.08496732] [0.11764706 0.1503268 0.05228758]] Matrix A raised to power 3: [[336 162 162 228] [406 469] [698 702 905]]
Suppose we want to solve this set of linear equations:
x + 2 * y = 8 3 * x + 4 * y = 18
This problem can be solved using the linalg.solve, method as shown in the example below:
import
numpy as np
# odds
a
=
np.array ([[
1
,
2
], [
3
,
4
]])
# constants
b
=
np.array ([
8
,
18
])
print
(
" Solution of linear equations: "
, np.linalg.solve (a, b))
Output:
Solution of linear equations: [2. 3.]
Finally, we see an example which shows how you can perform linear regression using the least squares method.
The linear regression line is w1 x + w 2 = y, and this is the line that minimizes the sum of squares the distance from each data point to the line So, given n data pairs (xi, yi), the parameters we are looking for are w1 and w2, which minimize the error:
Let`s see an example below:

Output:
So this leads to completing this series of tutorials on NumPy.
NumPy — it is a widely used general purpose library that underlies many other computational libraries such as scipy, scikitlearn, tensorflow, matplotlib, opencv, etc. Having a basic understanding of NumPy helps you work efficiently with other higher level libraries!
Links:
A ProblemSolver’s Guide to Building RealWorld Intelligent Systems. Data is the new oil and Machine Learning is a powerful concept and framework for making the best out of it. In this age of aut...
23/09/2020
The role of adaptation, learning and optimization are becoming increasingly essen tial and intertwined. The capability of a system to adapt either through modification of its physiological structure ...
10/07/2020
This book project was first presented to me during my first week in my current role of managing the data mining development at SAS. Writ ing a book has always been a bucket‐list item, and I was ver...
10/07/2020
The series “Studies in Big Data” (SBD) publishes new developments and advances in the various areas of Big Dataquickly and with a high quality. The intent is to cover the theory, research, develo...
10/07/2020