Numpy array (Numerical Python) is an open source library used for mathematical calculations and doing scientific computing with Python. We will take a look at some NumPy array example but first we have to understand more about NumPy.

So it helps in creating a multi-dimensional array and perform all the mathematical function easier and faster. NumPy has an important N-dimensional array object which is Linear algebra for Python. There are two very important functions of Numpy. Vectors and Matrices. Matrices are 2-d array containing rows and columns where as Vectors contain 1-d array.

## How to Install NumPy:

You can just copy paste the following command to install Numpy in your local.

`pip install numpy`

This is how it will look when pip installs the numpy packages.

## Numpy Array Examples and Explanations:

#### Creating a Numpy Array:

To create a array you have to pass a list. Firstly, The list has to be passed inside a square bracket. And it has to have a argument under (np.array) function.

A 3D array is collection of 2D arrays. It can specified using 3 subscripts row size, column size and block size. A 3D array can be a list where every element in the array can be a list.

Example:

```import numpy as np
array1d = np.array([1, 2, 3, 4, 5, 6])
array2d = np.array([[1, 2, 3], [4, 5, 6]])
array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(array1d)

print("-" * 10)
print(array2d)

print("-" * 10)
print(array3d)```

Output:

### Important functions of ndarray object:

The main data structure for multi-dimensional array for NumPy is (ndarray).

ndarray.shape: It gives the dimension of the array. This tuple defines the shape the of each and every array.

ndarray.size: It gives the shape of the array. This tuple helps in defining the size of the array. This is equal to the products of the elements of the shape.

ndarray.Ndim: It determines the dimension of the array.

ndarray.nbytes: Number of bytes are used in storing the data.

### Data Types In Numpy:

Data types is a method that defines the data types that are stored in the numpy array. Also there is an option of defining the datatype by yourself by using the function dtype. So here are dtypes there variants and there function.

Example explaining the dtypes:

```import numpy as np

type1 = np.array([1, 2, 3, 4, 5, 6])
type2 = np.array([1.5, 2.5, 0.5, 6])
type3 = np.array(['a', 'b', 'c'])
type5 = np.array([555, 666], dtype=float)

print(type1.dtype)
print(type2.dtype)
print(type3.dtype)
print(type4.dtype)
print(type5.dtype)

print(type4)```

Output:

#### Defining the shape of an array:

Shape of an array defines the shape of an numpy array. It is in the form of x*y (x representing the rows) and (y representing the columns).

So here is an example for using the shape function:

```import numpy as np

array1d = np.array([1, 2, 3, 4, 5, 6])
array2d = np.array([[1, 2, 3], [4, 5, 6]])
array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(array1d.shape)
print(array2d.shape)
print(array3d.shape)```

Output:

Dimension of an array:

The ndim is a function that defines the dimension of the array.

Example:

```import numpy as np

array1d = np.array([1, 2, 3, 4, 5, 6])
print(array1d.ndim)  # 1

array2d = np.array([[1, 2, 3], [4, 5, 6]])
print(array2d.ndim)  # 2

array3d = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
array3d = array3d.reshape(2, 3, 2)
print(array3d.ndim)  # 3```

Output:

#### Resize an array:

The resize array is used in modifying the existing shape of an array.

Example:

```import numpy as np

thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
thearray.resize(4)
print(thearray)

print("-" * 10)
thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
thearray.resize(2, 4)
print(thearray)

print("-" * 10)
thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
thearray.resize(3, 3)
print(thearray)```

Output:

#### Transform List or Tuple into NumPy array:

The numpy array also accepts lists, tuple and other functions of numpy.ndarray to create a new object in array.

Example:

```import numpy as np

thelist = [1, 2, 3]
print(type(thelist))  # <class 'list'>

array1 = np.array(thelist)
print(type(array1))  # <class 'numpy.ndarray'>

thetuple = ((1, 2, 3))
print(type(thetuple))  # <class 'tuple'>

array2 = np.array(thetuple)
print(type(array2))  # <class 'numpy.ndarray'>

array3 = np.array([thetuple, thelist, array1])
print(array3)```

Output:

#### Some Important Mathematical Numpy functions for generating arrays:

The mathematical numpy functions takes a single array of any dimension and return a new array with the same shape. So here are some important mathematical functions:

Example to explain some of the mathematical functions:

```import numpy as np

array1 = np.array([[10, 20, 30], [40, 50, 60]])

print(np.sin(array1))
print("-" * 40)

print(np.cos(array1))
print("-" * 40)

print(np.tan(array1))
print("-" * 40)

print(np.sqrt(array1))
print("-" * 40)

print(np.exp(array1))
print("-" * 40)

print(np.log10(array1))
print("-" * 40)```

Output:

#### Mathematical Operations Element wise:

Example to explain the mathematical functions element wise:

```import numpy as np

array1 = np.array([[10, 20, 30], [40, 50, 60]])
array2 = np.array([[2, 3, 4], [4, 6, 8]])
array3 = np.array([[-2, 3.5, -4], [4.05, -6, 8]])

print("-" * 40)

print(np.power(array1, array2))
print("-" * 40)

print(np.remainder((array2), 5))
print("-" * 40)

print(np.reciprocal(array3))
print("-" * 40)

print(np.sign(array3))
print("-" * 40)

print(np.ceil(array3))
print("-" * 40)

print(np.round(array3))
print("-" * 40)```

Output:

## Conclusion:

From this blog, you will learn about the characteristics of the NumPy library. Also, you will get to know about NumPy’s data structure for N-dimensional arrays and range of functions. Because of ndarray, the functionalities of Python can be extended. So it will become a suitable language for data analysis, scientific computing etc.

Therefore, we can say that understanding of NumPy is necessary for anyone who wants to take the road of data analysis. We hope that this will be helpful to you!

Categorized in:

Tagged in:

,