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…

Continue reading: https://www.datasciencecentral.com/xn/detail/6448529%3ABlogPost%3A1058699

Source: www.datasciencecentral.com