In my previous post, I explored how to use Pandas to work with data frames and similar structures. In this post, I want to go to the next level and discuss the magical operations available with the NumPy (Numerical Python) library, including fast array manipulation.
Numerical Python = NumPy
Why should go with NumPy
- Provides Data Structure, Algorithm for the Scientific application which requires numerical data.
- Which supports multi-dimensional array manipulation. NumPy’s array object is called ndarray.
- Easy to reshape, slice, and dice the array. And fast array process capability.
- Makes complex mathematical implementations very simple.
- To perform different numerical and trigonometry functions (i.e., sin, cos, tan, mean, median, etc.)
- Excellent support for Linear Algebra, Fourier Transformer, etc.,
- NumPy arrays are very efficient than list arrays, The way it processes manipulate id fast.
- It is often used along with other packages in Python environments like SciPy and Matplotlib.
What library supports and how should we import NumPy?
import numpy as np
What NumPy Cando?
The below picture represented the capabilities of NumPy. Let’s discuss it one by one.
I. Exploring the dimensions in the array
a. ONE Dimensions and Multi-Dimensions
(a.1) 1-D
import numpy as np
a = np.array([100,200,300,400,500])
print (a)
OUTPUT
[100 200 300 400 500]
(a.2) n-D
a = np.array([[100, 200], [300, 400]])
print (a)
OUTPUT
[[100 200]
[300 400]]
b. Number of Dimensions
x = np.array(1)
y = np.array([1, 2, 3, 4, 5])
z = np.array([[1, 2, 3], [4, 5, 6]])
print(x.ndim)
print(y.ndim)
print(z.ndim)
OUTPUT
0
1
2
c. Finding Type of the array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
OUTPUT
[1 2 3 4 5]
<class ‘numpy.ndarray’>
d. Accessing array elements
arr = np.array([1, 2, 3, 4])
print(arr[0])
OUTPUT
1
e.Slicing array element
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
OUTPUT
[2 3 4 5]
II. Data Type Objects Specfic to NumPy
NumPy has additional data types, Let’s see those and simple code to find the type of the variable.
III. Finding shape and re-shape of the ndarray.
a. Shape of the array
(a.1) The Array has the attribute that returns array dimensions.
import numpy as np
a = np.array([[1,2,3],[4,5,6],[4,5,6]])
print (“Shape of the array:”,a.shape)
a = np.array([[1,2,3,4],[3,4,5,6]])
print (“Shape of the array:”,a.shape)
OUTPUT
Shape of the array: (3, 3)
Shape of the array: (2, 4)
b. Re-shape of the array
(b.1) Certainly, you can resize the array, with simple steps
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print (“Array a(Actual shape):\n”,a)
print (“Shape of the array:\n”,a.shape)
print (“Array b(After reshape):\n”,b)
print (“After Re-shape of the array:\n”,b.shape)
OUTPUT
Array a(Actual shape):
[[1 2 3]
[4 5 6]]
Shape of the array:
(2, 3)
Array b(After reshape):
[[1 2]
[3 4]
[5 6]]
After Re-shape of the array:
(3, 2)
IV. Converting List and Tuple into ndarray
(a) List into Array
import numpy as np
x = [1,2,3]
print(“List:”,x)
a = np.asarray(x)
print(“As Array:”,a)
OUTPUT
List: [1, 2, 3]
As Array: [1 2 3]
(b) Tuple into Array
import numpy as np
x = (1,2,3)
print(“Tuple:”,x)
a = np.asarray(x)
print(“As Array:”,a)
OUTPUT
Tuple: (1, 2, 3)
As Array: [1 2 3]
V. Array Join and Split
(a) Join Array
This is similar to SQL joining two or more arrays into a single array using concatenate function
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1)
print(arr2)
arr = np.concatenate((arr1, arr2))
print(“After concatenate :”,arr)
OUTPUT
[1 2 3]
[4 5 6]
After concatenate : [1 2 3 4 5 6]
(b) Splitting Array
Splitting is the opposite operation of Joining, It breaks one array into multiple. using array_split()
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6,7,8])
print(“Actual Array:”,arr)
newarr = np.array_split(arr, 4)
print(“Split:”,newarr)
OUTPUT
Actual Array: [1 2 3 4 5 6 7 8]
Split: [array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]
VI. Create an Array with Ranges
– Start-value, Stop-value, Step-value
import numpy as np
x = np.arange(10,50,5)
print (x)
OUTPUT
[10 15 20 25 30 35 40 45]
VII. Array Multiplication
This simple multiplication provided Row and Column counts are equal. This concept is so-called broadcasting. it has limitations
- Arrays should have exactly the same shape.
- Arrays have the same number of dimensions and the length of each dimension.
a = np.array([1,2,3,4])
b = np.array([5,5,5,5])
print (a)
print (b)
c = a * b
print (c)
OUTPUT
[1 2 3 4]
[5 5 5 5]
[ 5 10 15 20]
VIII. NumPy – Mathematical Functions
As mentioned earlier, NumPy supports a number of mathematical operations to handling numbers.
- Arithmetic Operations
- Statistical Functions
- Trigonometric Functions
- Linear Algebra
(a) Arithmetic Operations
This is very straightforward operations, as we are very familiar with add(), subtract(), multiply(), and divide(). the array should have the same shape or should conform to array broadcasting rules.
a = np.array([2,2,2])
b = np.array([10,10,10])
print (‘First array:’,a)
print (‘Second array:’ ,b)
print (‘Add the two arrays:’,np.add(a,b))
print (‘Subtract the two arrays:’,np.subtract(a,b))
print (‘Multiply the two arrays:’ ,np.multiply(a,b))
print (‘Divide the two arrays:’ ,np.divide(a,b))
OUTPUT
First array: [2 2 2]
Second array: [10 10 10]
Add the two arrays: [12 12 12]
Subtract the two arrays: [-8 -8 -8]
Multiply the two arrays: [20 20 20]
Divide the two arrays: [0.2 0.2 0.2]
(b) Statistical Functions
NumPy has very useful statistical functions.
- Minimum
- Maximum
- Percentile
- Standard Deviation
- Variance
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print (‘Given array is:’)
print (a)
print (“Minimum :”,np.amax(a))
print (“Maximum :”,np.amin(a))
print (“Standard Deviation :”,np.std(a))
print (“Variance :”,np.var(a))
print(“Percentile :”,np.percentile(a, 4))
OUTPUT
Given array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
Minimum : 9
Maximum : 2
Standard Deviation : 2.309401076758503
Variance : 5.333333333333333
Percentile : 2.32
(c) Linear Algebra
NumPy package contains numpy.linalg library, it provides linear algebra functions
import numpy.matlib
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
print (“Dot Operation:”, np.dot(a,b))
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
print (“vdot :”,np.vdot(a,b))
OUTPUT
Dot Operation: [[37 40]
[85 92]]
vdot : 130
IX. NumPy with Matplotlib
As mentioned earlier, Numpy will work along with the Matplotlib library and helping us to create various charts. Quickly will see those. a really interesting piece of work.
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
#simple linear equation
y = 2 * x + 5
plt.title(“Matplotlib demo”)
plt.xlabel(“height”)
plt.ylabel(“weight”)
plt.plot(x,y)
plt.show()
OUTPUT
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 4 * np.pi, 0.2)
y = np.sin(x)
plt.title(“Sine Wave Form”)
plt.plot(x, y)
plt.show()
OUTPUT
from matplotlib import pyplot as plt
x = [5,15,10]
y = [12,16,18]
x2 = [6,9,14]
y2 = [6,15,7]
plt.bar(x, y, align = ‘center’)
plt.bar(x2, y2, align = ‘center’)
plt.ylabel(‘Performance’)
plt.xlabel(‘Slots’)
plt.show()
X. Working with a String array.
NumPy provides various options to play around with string.
import numpy as np
print (“Capitalize (hello world):”,np.char.capitalize(“hello world”))
print (“Title (hello how are you?):”,np.char.title(‘hello how are you?’))
print (“Lower (HELLO WORLD):”,np.char.lower([‘HELLO WORLD’]))
print (“Upper (hellow):”,np.char.upper(‘hellow’))
print (“Split (hello how are you?):”,np.char.split (‘hello how are you?’))
print (“Split (Python,Pandas,NumPy):”,np.char.split (‘Python,Pandas,NumPy’, sep = ‘,’))
print (“Strip (welcome watts):”,np.char.strip(‘welcome watts’,’w’))
print (“Join (dmy):”,np.char.join(‘:’,’dmy’))
print (“Join (dmy):”,np.char.join([‘:’,’-‘],[‘dmy’,’ymd’]))
print (“Replace (Python is a programming language):”,np.char.replace (‘Python is a programming language’, ‘programming’, ‘powerful programming’))
print (‘Concatenate two strings Hello,Mr.Babu:’,np.char.add([‘Hello’],[‘Mr.Babu’]))
print (‘Concatenation example [Hello, Hi],[ Shantha , Babu]:’,np.char.add([‘Hello’, ‘Hi’],[‘ Shantha ‘, ‘ Babu’]))
OUTPUT
Capitalize (hello world): Hello world
Title (hello how are you?): Hello How Are You?
Lower (HELLO WORLD): [‘hello world’]
Upper (hellow): HELLOW
Split (hello how are you?): [‘hello’, ‘how’, ‘are’, ‘you?’]
Split (Python,Pandas,NumPy): [‘Python’, ‘Pandas’, ‘NumPy’]
Strip (welcome watts): elcome watts
Join (dmy): d:m:y
Join (dmy): [‘d:m:y’ ‘y-m-d’]
Replace (Python is a programming language): Python is a powerful programming language
Concatenate two strings Hello, Mr.Babu: [‘Hello Mr.Babu’]
Concatenation example [Hello, Hi],[ Shantha , Babu]: [‘Hello Shantha ‘ ‘Hi Babu’]
Hope you all are enjoyed NumPy and its capabilities. Still many more… I have covered whichever is more important and mainly used during Data Science and Machine Learning implementation while dealing with datasets.
Thanks for your time in reading this article. Leave your comments. Shortly will get back to you with interesting topics.