This article will help you get you in touch with the widely used array-processing library in Python, NumPy.

Pretty cool!

NumPy is a library for the Python, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. Numpy is immensely fast when comes to processing.

It is the fundamental package for machine learning with Python. It contains various features including these important ones:

  • Random number capabilities
  • Best functions
  • A powerful N-dimensional array object

Besides its obvious machine learning uses, NumPy can also be used as an efficient multi-dimensional container of generic data(integer, float etc.)
But, here I’m only concerned with machine learning, so I’ll be sticking onto that, isn’t?

It’s installation is as easy as pie!

First, your computer must have python installed in it. Python can be download from here:https://www.python.org/downloads/. Along with the installation, pip(package install python) are also get downloaded. PIP is responsible for downloading all the libraries in python.

For Windows, Mac and Linux installation process is almost same!

pip install numpy

Since, I’ve already installed it that’s why it is highlighting this message(Requirement already satisfied).

Come on, Text me!

Arrays in NumPy: NumPy’s main object is the homogeneous multidimensional array.

  • It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.
  • In NumPy dimensions are called axes.
  • NumPy’s array class is called ndarray.
Let’s go…
>>> import numpy as np
# Creating array object
>>> arr = np.array( [[ 1, 2, 3],[ 4, 2, 5]] )
# Printing type of arr object
>>> print("Array is of type: ", type(arr))
# Printing array dimensions (axes)
>>> print("No. of dimensions: ", arr.ndim)
# Printing shape of array
>>> print("Shape of array: ", arr.shape)
# Printing size (total number of elements) of array
>>> print("Size of array: ", arr.size)
# Printing type of elements in array
>>> print("Array stores elements of type: ", arr.dtype)

Output :

Array is of type:  
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64

2. Array creation: There are various ways to create arrays in NumPy.

  • For example, you can create an array from a regular Python list or tuple using the array function.
  • Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content
    For example: np.zeros, np.ones, np.full, np.empty, etc.
  • arange: returns evenly spaced values within a given interval. step size is specified.
  • linspace: returns evenly spaced values within a given interval. num no. of elements are returned.
  • Reshaping array: We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM). The only required condition is:
    a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)
  • Flatten array: We can use flatten method to get a copy of array collapsed into one dimension.
>>>import numpy as np
# Creating array from list with type float
>>>a = np.array([[1, 2, 4], [5, 8, 7]], dtype = 'float')
>>> print ("Array created using passed list:\n", a)
# Creating array from tuple
>>> b = np.array((1 , 3, 2))
>>> print ("\nArray created using passed tuple:\n", b)
# Creating a 3X4 array with all zeros
>>> c = np.zeros((3, 4))
>>>print("\nAn array initialized with all zeros:\n", c)
# Create a constant value array of complex type
>>> d = np.full((3, 3), 6, dtype = 'complex')
>>> print("\nAn array initialized with all 6s.""Array type is complex:\n", d)
# Create an array with random values
>>> e = np.random.random((2, 2))
>>> print("\nA random array:\n", e)
# Create a sequence of integers
# from 0 to 30 with steps of 5
>>> f = np.arange(0, 30, 5)
>>> print("\nA sequential array with steps of 5:\n", f)
# Create a sequence of 10 values in range 0 to 5
>>> g = np.linspace(0, 5, 10)
>>> print("\nA sequential array with 10 values between"
"0 and 5:\n", g)
# Reshaping 3X4 array to 2X2X3 array
>>> arr = np.array([[1, 2, 3, 4],[5, 2, 4, 2],[1, 2, 0, 1]])
>>> newarr = arr.reshape(2, 2, 3)
>>> print("\nOriginal array:\n", arr)
>>> print("Reshaped array:\n", newarr)
# Flatten array
>>> arr = np.array([[1, 2, 3], [4, 5, 6]])
>>> flarr = arr.flatten()
>>> print("\nOriginal array:\n", arr)
>>>print("Fattened array:\n", flarr)

Output :

Array created using passed list:
[[ 1. 2. 4.]
[ 5. 8. 7.]]
Array created using passed tuple:
[1 3 2]
An array initialized with all zeros:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
An array initialized with all 6s. Array type is complex:
[[ 6.+0.j 6.+0.j 6.+0.j]
[ 6.+0.j 6.+0.j 6.+0.j]
[ 6.+0.j 6.+0.j 6.+0.j]]
A random array:
[[ 0.46829566 0.67079389]
[ 0.09079849 0.95410464]]
A sequential array with steps of 5:
[ 0 5 10 15 20 25]
A sequential array with 10 values between 0 and 5:
[ 0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778
3.33333333 3.88888889 4.44444444 5. ]
Original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 1]]
Reshaped array:
[[[1 2 3]
[4 5 2]]
 [[4 2 1]
[2 0 1]]]
Original array:
[[1 2 3]
[4 5 6]]
Fattened array:
[1 2 3 4 5 6]

Congratulations! You’ve done quite a lot today. Grab a cup of coffee and relax!

Youtube:https://www.youtube.com/channel/UCkASXCpgVpOcCosveu5-9qA

LinkedIn:https://www.linkedin.com/in/ansh-srivastava-60168a15a/

Twitter:https://twitter.com/_iamansh

Github:https://github.com/meansh