More often than not, data analysis will involve some form of mathematical calculations. Having a large data set also means that you would not want to have to manually count each data point. What you can do is to perform mathematical calculations using arrays.
Basic mathematical functions on arrays are element wise operators. Let's take a look at what this means.
Let's start by creating some arrays to work with:
There are 2 ways to perform element-wise addition with arrays.
Method 1: Simply just add them together using +
operator
print(x + y)
Method 2: Use np.add
print(np.add(x,y))
Similarly, to subtract use np.subtract
or -
, to multiply use np.multiply
or *
, and to divide use np.divide
or /
.
To perform element-wise square root we can use np.sqrt
Syntax
print(np.sqrt(x+y))
To look for the inner product, or matrix multiplication of arrays, use np.dot
.
We have to first make sure that the rows and columns match, just like we would for any other matrix multiplication.
To sum up elements in an array, use np.sum
Syntax
(np.sum(array))
To find the sum of each column (adding up all row values for each column) we just need to specify the axis = 0
Syntax
print(np.sum(x, axis=0))
To find the sum of each row we use axis = 1
Syntax
print(np.sum(x, axis=1))
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.