# Diving into NumPy - An Important Library Required for Data Science NumPy, one of the most important and basic libraries used in data science and machine learning. NumPy consists of functionalities for multidimensional arrays, high-level mathematical functions: Linear algebra operations, Fourier transform, Random generators. The core of NumPy is well optimized C-code, so the execution speed is increased in Python while using Numpy.

NumPy, one of the most important and basic libraries used in data science and machine learning, It consists of functionalities for multidimensional arrays, high-level mathematical functions such as,

• Linear algebra operations
• Fourier transform
• Random generators

and also NumPy array forms the fundamental data structure for scikit-learn. The core of NumPy is well optimized C-code, so the execution speed is increased in Python while using Numpy.

The fundamental package for scientific computing with Python — NumPy

This article consists of the basic operations and most commonly and frequently used operations in NumPy. The article would be beginner-friendly and also act as a refresher for intermediate and advanced.

``import numpy as np``

The `as` keyword makes `np` as the alias name of NumPy, so we could use np instead of NumPy. This is a common practice that saves time and makes it easier to work.

## NumPy Arrays

For creating a NumPy array we could use the`np.array` function to create it and `dtype` as an optional argument that changes the array to the required type. Here is a list of array data types. When the elements in the array are of different array data types, then the elements will be upcasted to the highest level type. This means that if an array input has mixed `int` and `float` elements, all the integers will be cast to their floating-point equivalents. If an array is mixed with `int`, `float`, and `string` elements, everything is cast to strings.

To cast an array to the required type we could use the`astype` function. The function’s required argument is the new type of array and to know the type of array we could use `.dtype`

``````#Using arange function
arr = np.arange(5)
print(arr)
#Output: [0 1 2 3 4]
arr = np.arange(2,8,2)
print(arr)
#Output: [2,4,6]

#Playing with shape and reshape
arr1 = np.arange(6)
print(arr1.shape)
#Output: (6,)
arr2 = np.reshape(arr1,(2,3))
print(arr2)
print("The shape of arr2 is',arr2.shape)
#Output: [[0 1 2]
##         [3 4 5]]
##         The shape of arr2 is (2, 3)
arr3 = np.reshape(arr2,(-1,3,2))
print(arr3)
print("The shape of arr3 is',arr3.shape)
#Output: [[[0 1]
##          [2 3]
##          [4 5]]]
##        The shape of arr3 is (1, 3, 2)

#Flattening an array
print(arr3.flatten())
#Output: [0 1 2 3 4 5]      ``````

To copy an array we could use the inherent `copy` function and perform it. The NaN (Not a Number) value could also be used by using `np.nan` . The nan _would act as a placeholder and will not take the _integer value. _If integer type is used while containing nan, it would result in an _error.

## NumPy Basics

NumPy provides an option to create ranged data arrays using np.arange. The function acts very similarly to the `range` function in Python and returns a 1D array. If a single number `n` is passed as an argument then it would return numbers ranging from `0` to `n-1`. If two numbers are passed as argument `m`and `n`it would return numbers ranging from `m`to `n-1` . If three arguments are used `m`, `n` and `s` it will return numbers ranging from `m`to `n-1` using step size `s` .

The `shape` function is used to know the shape of the array. While using `reshape` function it takes input array and new shape as the arguments. For example, if the number of elements in an array is 10 then the new shape should be (5,2) or (2,5) as they form the multiplicative result. We are allowed to use the special value of -1 in at most one dimension of the new shape. The dimension with -1 will take on the value necessary to allow the new shape to contain all the elements of the array.

The `flatten` function would reshape an array of any size into a 1D array.

``````#Using arange function
arr = np.arange(5)
print(arr)
#Output: [0 1 2 3 4]
arr = np.arange(2,8,2)
print(arr)
#Output: [2,4,6]

#Playing with shape and reshape
arr1 = np.arange(6)
print(arr1.shape)
#Output: (6,)
arr2 = np.reshape(arr1,(2,3))
print(arr2)
print("The shape of arr2 is',arr2.shape)
#Output: [[0 1 2]
##         [3 4 5]]
##         The shape of arr2 is (2, 3)
arr3 = np.reshape(arr2,(-1,3,2))
print(arr3)
print("The shape of arr3 is',arr3.shape)
#Output: [[[0 1]
##          [2 3]
##          [4 5]]]
##        The shape of arr3 is (1, 3, 2)

#Flattening an array
print(arr3.flatten())
#Output: [0 1 2 3 4 5]      ``````

## Math Operations

With the help of NumPy arrays, we can apply arithmetic, logical and other operations to each element in the array. This helps to modify a large amount of numeric data with only a few operations. NumPy arrays perform a basic arithmetic operation to every element in an array. Apart from the basic arithmetic function, NumPy can perform other trigonometric, hyperbolic, exponents, logarithms, and a lot more functions. These functions have been listed here.

``````arr1 = np.array([[3,4],[5,6]])
arr2 = np.array([[1,2],[3,4]])
print(arr1+2)
#Output: [[5 6]
##         [7 8]]

#Performing power operation
print(arr1**2)
#Output: [[ 9 16]
##         [25 36]]

#Performing subtraction between two array
print(arr1-arr2)
#Output: [[2 2]
##         [2 2]]

#Performing basic multiplication
print(arr1*arr2)
#Output: [[ 3  8]
##         [15 24]]

#Performing exponential function
print(np.exp(arr1))
#Output: [[ 20.08553692  54.59815003]
##         [148.4131591  403.42879349]]

#Performing logarthimic function with base 10
arr3 = np.array([[10,100],[np.e,np.pi]])
print(np.log10(arr3))
#Output: [[1\.         2\.        ]
##         [0.43429448 0.49714987]]

#Raising 3 to power of each element in array
print(np.power(3,arr2))
#Output: [[ 3  9]
##         [27 81]]

#Performing matrix multiplication
print(np.matmul(arr1,arr2))
#Output:  [[15 22]
##          [23 34]]``````

The function np.exp `performs a _base e exponential_ on an array and` np.log10 `performs logarithms on input array using _base 10_ . To do a regular power operation with any base, we use` np.power . The first argument to the function is the base , while the second is the power . To perform matrix multiplication between two arrays we use `np.matmul` the function. The dimension of two input matrix in `np.matmul` must obey the principle of matrix multiplication . The second dimension of the first matrix must equal the first dimension of the second matrix, otherwise `np.matmul` will result in a `ValueError` .

## Hire Machine Learning Developers in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.

## Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

## Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

## Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

## Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.