You are currently viewing Introduction to Numpy array python (Zero dimension, One dimension, two dimension and three dimension) with examples
Understanding Numpy array in Simple Way

Introduction to Numpy array python (Zero dimension, One dimension, two dimension and three dimension) with examples

Loading

Introduction

Numpy arrays are the basic building block of image processing and computer vision. Python is fun and numpy array stands between pre-processing and model training. Data in string form or integer form is converted into numpy array before feeding to machine for training. This tutorial is about discussing numpy arrays in zero dimension, one dimension, two dimension and three dimension

What this tutorial will cover

1.) What is numpy array and its significance with respect to AI.
2.) Numpy array in zero dimension along with shape and live examples
3.) Numpy array in one dimension along with shape and live examples
4.) Numpy array in two dimension along with shape and live examples
5.) Numpy array in three dimension along with shape and live examples

What is numpy array and its significance with respect to AI

Numpy is a math library for python and is used to perform computation easily and effectively. If you desire to become a Data Scientist, solid knowledge of numpy array is must. If you heard about pandas library, it is fine otherwise it is a library for transforming the data into data frame and manipulating it accordingly. It would be amazing to know that it is build on top of numpy package. If you are a Data Scientist, you are working with scikit learn many times. It would be again surprising to know that numpy array and Scikit learn works together hands to hands. I think know you may some idea why this package is so important and one must learn how to operate using numpy array. Let us see some of the methods available with the numpy package.

import numpy as np
print("please show the methods available with numpy package:")
print(dir(np))

Output of this code

[‘ALLOW_THREADS’, ‘AxisError’, ‘BUFSIZE’, ‘CLIP’, ‘ComplexWarning’, ‘DataSource’, ‘ERR_CALL’, ‘ERR_DEFAULT’, ‘ERR_IGNORE’, ‘ERR_LOG’, ‘ERR_PRINT’, ‘ERR_RAISE’, ‘ERR_WARN’, ‘FLOATING_POINT_SUPPORT’, ‘FPE_DIVIDEBYZERO’, ‘FPE_INVALID’, ‘FPE_OVERFLOW’, ‘FPE_UNDERFLOW’, ‘False_’, ‘Inf’, ‘Infinity’, ‘MAXDIMS’, ‘MAY_SHARE_BOUNDS’, ‘MAY_SHARE_EXACT’, ‘MachAr’, ‘ModuleDeprecationWarning’, ‘NAN’, ‘NINF’, ‘NZERO’, ‘NaN’, ‘PINF’, ‘PZERO’, ‘PackageLoader’, ‘RAISE’, ‘RankWarning’, ‘SHIFT_DIVIDEBYZERO’, ‘SHIFT_INVALID’, ‘SHIFT_OVERFLOW’, ‘SHIFT_UNDERFLOW’, ‘ScalarType’, ‘Tester’, ‘TooHardError’, ‘True_’, ‘UFUNC_BUFSIZE_DEFAULT’, ‘UFUNC_PYVALS_NAME’, ‘VisibleDeprecationWarning’, ‘WRAP’, ‘_NoValue’, ‘__NUMPY_SETUP__’, ‘……………]

Numpy array in zero dimension along with shape and live examples

Numpy array in zero dimension is an scalar. You cannot access it via indexing. Zero dimensional array is mutable. It has shape = () and dimensional =0. let us do this with the help of example.

import numpy as np
a = np.array(1)
print("Printing the shape of numpy array")
print(a.shape)
print("printing the dimension of numpy array")
print(a.ndim)

Output of this code
Printing the shape of numpy array
()
printing the dimension of numpy array
0

Numpy array in one dimension along with shape and live examples

Numpy array in one dimension can be thought of a list where you can access the elements with the help of indexing. If you want me to throw light on shape of the array. it would be number of the elements present in the array. Array contains the elements of the same datatype. Please refer to the below code to understand my point of view.

import numpy as np
a = np.array([1, 2, 3, 4, 5])
print("Accessing the second element from the array ")
print(a[2])
print("Printing the shape of numpy array")
print(a.shape)
print("Printing the dimension of numpy array")
print(a.ndim)

Output of this code
Accessing the second element from the array
3
Printing the shape of numpy array
(5,)
Printing the dimension of numpy array
1

Numpy array in two dimension along with shape and live examples

Numpy array in three dimension along with shape and live examplesWhen you are working with matrix, you can think yourself of getting result in 2 dimensional numpy arrays or three dimensional numpy arrays which can be distinguished based on number of square brackets used. I mean to say that this is an important part while working with the images or training the images for images classification. Understanding two dimensional numpy array is very easy. It has rows and columns. We have two axis (axis 0 and axis 1). Axis depends on dimension of array. For n dimensional array there are n axis. Axis 0 means operation is column wise while axis 1 means operation is row wise. Shape will be determined by number of rows and columns in the matrix. Donot worry if you have not understood the above lines. I will explain you these lines with the help of following code.

import numpy as np
a = np.array([[1, 2, 3, 4, 5], [2,3,4,5,6]])
print("please show the matrix")
print(a)
print("Accessing the FIFTH element from second row of the array ")
print(a[1][4])
print("Printing the shape of numpy array in form of rows and columns")
print(a.shape)
print("Printing the dimension of numpy array")
print(a.ndim)
print("Please see the sum columns wise")
print(a.sum(axis=0))
print("Please see the sum row wise")
print(a.sum(axis=1))

Numpy arraysOutput of the code
please show the matrix
[[1 2 3 4 5] [2 3 4 5 6]] Accessing the FIFTH element from second row of the array
6
Printing the shape of numpy array in form of rows and columns
(2, 5)
Printing the dimension of numpy array
2
Please see the sum columns wise
[ 3 5 7 9 11] Please see the sum row wise
[15 20]

Numpy array in three dimension along with shape and live examples

I think you have understood the code in two dimension well and is hopeful that you have run the code. Code is provided so that you can practice and visualise the concepts in a better way. One can visualise the 3 dimensional matrix by putting the three square bracket with the numpy array. It is easy. Shape tells us how many 2d matrix we have, how many rows each have and how many columns each have. This is bit complicated which is made easy when you run the below code. I suggest you running the code so that you may understand the concept in more practical sense. Please spare some time to run the code.

import numpy as np
a = np.random.rand(4,2,3)
print("please show the matrix")
print(a)
print("\n")
print("Accessing the second 2d matrix second row second element ")
print(a[1][1][1])
print("\n")
print("Printing the shape of numpy array in form of rows and columns")
print(a.shape)
print("\n")
print("Printing the dimension of numpy array")
print(a.ndim)
print("\n")
print("Please see the sum by taking the first element from each 2d numpy array ")
print(a.sum(axis=0))
print("\n")
print("Please see the sum vertical: ")
print(a.sum(axis=1))
print("\n")
print("Please see the sum horizontal: ")
print(a.sum(axis=2))

Output
please show the matrix
[[[0.3899972 0.80039376 0.25806424] [0.86861977 0.23537898 0.45386452]]

[[0.41907103 0.86557885 0.99410499] [0.83317744 0.19634474 0.55698421]]

[[0.97738715 0.96975674 0.02695369] [0.28966537 0.94950277 0.96271475]]

[[0.86536233 0.32514737 0.27583637] [0.54018335 0.25795359 0.6937266 ]]]

Accessing the second 2d matrix second row second element
0.1963447442894647

Printing the shape of numpy array in form of rows and columns
(4, 2, 3)

Printing the dimension of numpy array
3

Please see the sum by taking the first element from each 2d numpy array
[[2.65181772 2.96087672 1.55495929] [2.53164593 1.63918009 2.66729009]]

Please see the sum vertical:
[[1.25861698 1.03577274 0.71192876] [1.25224847 1.0619236 1.5510892 ] [1.26705251 1.91925951 0.98966844] [1.40554568 0.58310096 0.96956297]]

Please see the sum horizontal:
[[1.4484552 1.55786327] [2.27875487 1.5865064 ] [1.97409758 2.20188289] [1.46634607 1.49186354]]

What you have learned after reading this tutorial

You have got basic knowledge of numpy array and how it is significant with respect to artificial intelligence. You also got the knowledge of zero dimensional, one dimensional, two dimensional and three dimensional numpy arrays and calculated different parameters with respect to each dimension. In an nutshell, it can be concluded that numpy forms Numpy arraysan integral part of machine learning,deep learning and while working with high computational libraries such as SciPy, pandas and tensorflow.

Some additional information

AI Sangam is a Data science solution and consulting company in India with vision of providing intelligent solutions to solve everyday as well as complex problems. We deal with building artificial and intelligent chatbots, real time face recognition, resume filtering based on natural language processing, python teaching, django, tornado and flask live projects. We also know that IT is changing dynamic, keeping this in view point we also teach some latest technology such as docker, kubernetes, containers, google cloud engine, amazon instance and integrating application on these platform.

If doubt where to contact

If you feel some improvement in technical knowledge, you may also contact us at aisangamofficial@gmail.com or can chat with us or talk with us at skype with id: live:aisangamofficial. Please donot forget to visit aisangam you tube channel. You may also follow us at
Facebook
Twitter
Linkedln
Pinterest
Tumbler
Reddit

This Post Has 12 Comments

  1. successmantra

    Hello AI Sangam!!

    It is pleasure to write my feedback to you. I have gone through whole of the blog and have found post very interesting. Some of the salient features of this post are as below

    1.) Concepts of the numpy arrays are explained with the proper codes and explanation.
    2.) Output is displayed for each code. This makes the learning quite easy.
    3.) I too loved the section for doubts. So overall I have a lot of confusion on numpy array and how to visualise them in different dimensions especially zero, one, two and three.

    Some advice I can provide you
    1.) Please write a lot of articles in future because we need such blogs for more knowledge and ideas.
    Question
    1.) Can you tell me how to install numpy in python. I donot know as i am new to python.

    1. AISangam

      First of all thanks successmantra for such comment.

      Reply to comment

      To install numpy, you need to specify which version of python you are using. If you are using python2, please run the below command
      pip2 install numpy

      For python 3, please execute the below command
      pip3 install numpy

  2. Arjun

    Hello AI Sangam!!!

    How are you. I have the below code.
    import numpy as np
    a = np.array(4)
    print(a[0])

    But when I run such code I got the below error:-
    usr/bin/python3.5 /home/xxxx/PycharmProjects/numpy_array/Errors_numpyarray.py
    Traceback (most recent call last):
    File “/home/xxxx/PycharmProjects/numpy_array/Errors_numpyarray.py”, line 3, in
    print(a[0])
    IndexError: too many indices for array

    Can you guide me where is the problem. I would be very thankful to you

    1. AISangam

      Thanks for replying us. Please donot worry. AI Sangam is there .

      Since your dimension of the data is zero.Please run the below command to know the dimension

      print(a.ndim)

      Zero dimension is a scalar. Zero dimension cannot be indexed.

      Hope AI Sangam has helped you in resolving the error. Just print a (variable) without indexing and you will get answer.

      Please have a look at following sites to know us more
      https://www.aisangam.com/
      http://selfawarenesshub.org/
      http://www.glbaat.com

  3. Glbaat

    Hello AI Sangam !!!

    How are you. I need to know how to create an .exe file from .py file. I have created an small application bases on concepts of numpy as mentioned above.

    I would welcome your points.

    With Regards
    Glbaat

  4. Rajat

    Hello,

    My query is that I need to understand reshape of arrays both in 2D and 3D

    1. AISangam

      Okay great. To make you understand reshape, please read this statement. It does not decrease or increase the number of elements but only change their shape. Donot worry, I will explain my words with the help of example. Please see the below code

      Numpy array reshape for 2 dimension

      import numpy as np
      a = np.arange(16).reshape(8,2)
      print('The reshaped 2d array:')
      print(a)
      #printing the dimension of a
      print()
      print("Dimension of array")
      print(a.ndim)
      

      Please see the output. Please count number of elements. It would remain 16. Only shapechanges.
      Output

      The reshaped 2d array:
      [[ 0  1]
       [ 2  3]
       [ 4  5]
       [ 6  7]
       [ 8  9]
       [10 11]
       [12 13]
       [14 15]]
      
      Dimension of array
      2
      

      Numpy array in three dimension

      import numpy as np
      a = np.arange(16)
      a= a.reshape(4,2,2)
      print("The modified 3d array: ")
      print(a)
      print()
      print("See the dimension of array")
      print(a.ndim)
      

      When you are analysing the output, count the number of elements, it would remain same
      Output

      The modified 3d array: 
      [[[ 0  1]
        [ 2  3]]
      
       [[ 4  5]
        [ 6  7]]
      
       [[ 8  9]
        [10 11]]
      
       [[12 13]
        [14 15]]]
      
      See the dimension of array
      3
      
  5. Kirti

    Hello AI Sangam,
    Please explain the difference between size and shape with regards to numpy array. I want to understand it with respect to 2 dimensional array

    1. AISangam

      Hello kirti. Very good question

      In 2D explanation for size and shape are as below

      Shape means rows and columns of the 2-dimensional numpy array.
      Size means number of element present in the array.

      For more details, please see the below code

      import numpy as np
      x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
      print("2D Numpy array:")
      print(x)
      #shape will tell us the rows and columns in 2d
      print()
      print("Shape: rows and columns")
      print(x.shape)
      #size means the number of elements present in the matrix
      print()
      print("Showing the size of the array:")
      

      With regards
      AI Sangam

  6. vurtil opmer

    I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to know where u got this from. appreciate it

    1. AISangam

      Thanks for asking for the theme. We have used free theme. We focus more on content as we have well qualified content writes in our company.

      If you have any questions please do ask us.

      Sorry for the late reply, we were busy this month so could not read comments. Sorry again and Merry Christmas.

Leave a Reply