Python NumPy Tutorial

Python NumPy array – Create NumPy ndarray (multidimensional array)

What is the NumPy array?

Python NumPy array is a collection of a homogeneous data type. It is most similar to the python list. You can insert different types of data in it. Like integer, floating, list, tuple, string, etc.

To create a multidimensional array and perform a mathematical operation python NumPy ndarray is the best choice. The ndarray stands for N-Dimensional arrays.

NumPy array
              Fig 1.1 Representation of NumPy ndarray (multidimensional array )

Let’s jump on practical session NumPy


Create NumPy ndarray (1D array)

To create NumPy 1D array use array() function and give one argument of items of a list to it.

Syntax: array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)

import numpy as np # import numpy package
arr_1D = np.array([2,4,6,8]) # create  NumPy 1D array which contain int value 2,4,6,8
print(arr_1D) # print arr_1D
Output >>> [2 4 6 8]

In machine learning and data science NumPy 1D array known as vector. Specially use to store and perform an operation on target values.

Create NumPy ndarray (2D array)

To create NumPy 2D array use array() function and give one argument of items of lists of the list to it.

Syntax: array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)

import numpy as np # import numpy package
arr_2D = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) # Create Numpy 2D array which contain inter type valye
print(arr_2D) # print arr_1D
Output >>>

[[0 1 1]
 [1 0 1]
 [1 1 0]]

In machine learning and data science NumPy 2D array known as a matrix. Specially use to store and perform an operation on input values.

Create NumPy ndarray (3D array)

To create NumPy 3D array use array() function and give one argument of items of lists of lists of the list to it.

Syntax: array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)

import numpy as np
arr_3D = np.array([[[0, 1, 1], [1, 0, 1], [1, 1, 0]]]) # create numpy 3D array which contain integer values
print(arr_3D)
Output >>>
 
[[[0 1 1]
  [1 0 1]
  [1 1 0]]]

In machine learning and data science NumPy 3D array known as a tensor. Specially used to store and perform an operation on three-dimensional data like color image.

 

How to store different types of data in NumPy ndarray?

The array() function accepts different types of data. You can insert it while creating an array.

import numpy as np
# store student information in NumPy 2D array
student_info = np.array([['id', 'name', 'percentage', 'pass or fail'], 
                         [101, 'Narendra', 80, 'pass'], 
                         [102, 'John', 75, 'pass'],
                         [103, 'Abraham',33, 'fail'],
                         [104, 'Oprah',52, 'pass']
                        ])
print(student_info)
Output >>> 

[['id' 'name' 'percentage' 'pass or fail']
 ['101' 'Narendra' '80' 'pass']
 ['102' 'John' '75' 'pass']
 ['103' 'Abraham' '33' 'fail']
 ['104' 'Oprah' '52' 'pass']]

How to set default data type?

By default, The python NumPy array data type is none. To set it, give an argument to array() function as the desired data type. like int, float, str, etc.

import numpy as np
percentage = np.array([80, 75, 33, 52], dtype = float) # set data type as float
print(percentage) 
Output >>> [80. 75. 33. 52.]

Generally, remaining default argument of array () function never use but you can try it. In this way, you can create a NumPy multidimensional arrays.

To learn more about python NumPy library click on the bellow button.

1 thought on “Python NumPy array – Create NumPy ndarray (multidimensional array)”

  1. Pingback: Computer Vision With OpenCV And Python | What is OpenCV for CV?

Leave a Reply