NumPy array for machine leaning

Numpy array is one of the most popular libraries available in python for machine learning. Numpy stands for numerical python which was created in 2005 by Travis Oliphant. Additionally, It is one of the most popular python libraries which is used for working with arrays. To solve the machine learning problem a large number of developers prefer to use NumPy because it helps to execute our code very fast. It also has a different function to work with a different domain like linear algebra, Fourier transform and matric. You can find the documentation of Numpy from here

Why is Numpy

We use NumPy for memory efficiency, meaning it has the capability to handle vast amounts of data and is more accessible than any other library available in python. Besides, Numpy is very convenient to work with multidimensional data. On the other hand, it is a very fast and popular libraries TensorFlow and Scikit learn to use the NumPy array to compute matric multiplication.

How to Install Numpy

Installing NumPy in your system is very easy. If you have python and pip installed in your system then you will install it simply by the following command.

pip install numpy

After installed import it by following the code and checking the version.

import numpy as np
print(np.__version__)

If you have a problem regarding installing NumPy visit this article

Numpy basic

Numpy array is the same as python list but it has very much different at the same time. It is a central data structure of NumPy. It can be created by the calling method. The following code shows how can we create a NumPy array?

myPythonList = [1,9,8,3]
numpy_array_from_list = np.array(myPythonList)  
print(numpy_array_from_list)

Shape of array

In Numpy array you can check the shape of array easily by calling shape object preceded by name of the array. Like wise you can check the data type of multidimensional data with a help of dtype object. Finally, for checking dimensions of the array you can lend your hand by calling ndim object preceded by array name. Following program shows that how can we check the shape of array, data type and dimensions.

1D array

arr = np.array([1, 2, 3, 4, 5])
print(arr) 
print(arr.dtype)
print(arr.ndim)
Output:
[1 2 3 4 5]
int32
1

2D array

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
print(arr.dtype)
print(arr.ndim)
Output:
[[1 2 3]
 [4 5 6]]
int32
2

3D array

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
print(arr.dtype)
print(arr.ndim)
Output:
[[[1 2 3]
  [4 5 6]]
 [[1 2 3]
  [4 5 6]]]
int32
3

Access Array Elements

Accessing array elements is also known as array indexing. You can access element of array by referring to its array index, and one important thing about the array index is it starts with index 0 and end with n-1, it means that the first array index is 0 like wise second index is 1 and so on. Finally, if you put the index which is not available in your array range the you will find Arrayindex error. The following line of code illustrate how can we access single dimensional array by indexing method.

arr = np.array([1, 2, 3, 4])
print(arr[2]) 
print(arr[3])
Output:
3
4

Accessing multidimensional array

In previous step we were learn about how can we implement array indexing for single dimensional array ,but for multidimensional array process is different. To access element from multidimensional element we can use comma separated integers representing dimension and the array of element. The following line of code depicts how can we access multidimensional array.

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st dim: ', arr[0, 1])
print('5th element on 2nd dim: ', arr[1, 4])
Output:
2nd element on 1st dim:  2
5th element on 2nd dim:  10

Slicing Array

Slicing array means you can take element of array by certain range, and the range always need to be between the lower and the highest index. Additionally, We pass slice instead of index like this: [start:end] or like this [start: end: step]. If you don’t pass the starting index it considers as 0 like wise , if you don’t pass the ending index it considers length of array of that dimension. Moreover, if you want to get element by certain steps we need to define at the end otherwise it will be taken as 1 by default. The given code shows how slicing operation work with Numpy array.

arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5]) 
print(arr[4:])
print(arr[1:5:2])
Output:
[2 3 4 5]
[5 6 7]
[2 4]

Slicing Multidimensional Arrays

For slicing multidimensional array the syntax is like this [dimension, start: end: step], where dimension refer for which array you want to do slicing operation. we can also define the multiple dimension in a range. Additionally, second part of the above syntax is similar with single dimensional Numpy array. Following code show how can we do slicing operation with multidimensional Numpy array.

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4]) # From the second element, slice elements from index 1 to index 4 (not included)
print(arr[0:2, 2:4]) # from both array return 2nd index
Output:
[7 8 9]
[[3 4]
 [8 9]]

Shape of array

Numpy array has an attribute call shape which return a tuple with each index having number of elements. Following code show how can we use shape attributes.

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr.shape)

Output:
(2, 5)

Reshaping Arrays

Reshaping means changing the shape of an array. The shape of an array is defined as the number of elements in each dimensions. For reshaping we can add or remove dimensions as well as remove element from dimensions. Reshaping is only possible if required number of elements in an array is available. Following code depicts how can we reshape Numpy array.

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
Output:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Flattening the Array

Flatting the array means converting multidimensional data into single dimension. We can flatten an array by this reshape(-1) .

arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr = arr.reshape(-1)
print(newarr) 
Output:
[1 2 3 4 5 6]

Leave a Reply