Learn Python

Learn Data Structure & Algorithm

Numpy Introduction

Numpy Array

Numpy Attributes

Numpy generate random numbers

Numpy Slicing

Numpy Concatenate & split

Numpy Insert & delete

Numpy Math

Numpy Condition

Numpy Reshaping

Learn Pandas

Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Learn everything about numpy array

Types of array

  • 1D array
  • 2D array
  • 3D array
  • nD array
  • 1D array

    Here D means dimension. One dimensional array contains elements only in one dimension. It looks like a simple python list but it is not a list. We use python list to create a numpy array. If you convert a simple list into an array(1-dimensional matrix ) then it will be a 1D or 1 dimensional array. To convert a list into an array or matrix array() function is used. In the function bracket, write the variable name where the list is stored.

    Example:

    Input
    import numpy as np
    x=[1 2 3 4 5]
    y=np.array(x)
    print(y)
    Output
    [1,2,3,4,5]

    In the example we have a python list. Now we want to convert it into a numpy array. To do this we just passed the list in the array() function. Now the list become a one dimensional array.

    2D array

    Here D means dimension. Two dimensional array contains elements in two dimension. It looks like some python lists are stored in an python list but those are not lists. We use python list to create a numpy array. Suppose you have a list and inside that list you have some other list. Now if you convert this outer list into an array then it will called 2D array. To convert python list into an array or matrix array() function is used. In the function bracket, write the variable name where the list is stored.

    Example:

    Input
    import numpy as np
    x=[[1,2],[3,4]]
    y=np.array(x)
    print(y)
    Output
    [[1,2]
    [3,4]]

    In the example we have two python list inside list. Now we want to convert this list into a two dimensional array. To do this we will pass this list in the array() function. After passing the list become a two dimensional array.

    3D array

    Suppose you have a python list that contains data like 2D array means(a list contains some other list in it) then it is called a 3D array. It is a multi-dimensional array, so it has length, width, and depth. To convert a python list into an array use the array() function. In function, bracket write the variable name where the list is stored.

    Example:

    Input
    import numpy as np
    x=[[[1,2] ,[3,4]],[[5,6],[7,8]]]
    y=np.array(x)
    print(y)
    Output
    [[[1,2] [3,4]],
    [[5,6] [7,8]]]

    We have a python list. Inside that list another list and inside this list we have two lists. Now we want to convert it into an array. To do this we will use numpy array function and after this we will get a three dimensional array.

    Zeros matrix

    All values of the zeros matrix are zero. To create zeros matrix use zeros() function. In the function bracket, pass the row and column number. Row and column number means how many columns and rows you want in the matrix. In the bracket first value is for the row and the second value is for the column.

    Example:

    Input
    import numpy as np
    import numpy as np
    x=np.zeros(2,3)
    print(x)
    Output
    [[0. 0. 0.]
    [0. 0. 0.]]

    Ones matrix

    All values of ones matrix are one. To create ones matrix use ones() function. In the function bracket, pass the row and column number. It mean how many columns and rows you want in the matrix. In the bracket first value is for the row and the second value is for the column.

    Example:

    Input
    import numpy as np
    x=np.ones(2,2)
    print(x)
    Output
    [[1. 1.]
    [1. 1. ]]

    Eye matrix

    In eye matrix all the diagonal values are 1 and remaining all the values will be 0. To create an eye matrix eye() function is used. In the function bracket, pass the row and column number. It means how many columns and rows we want in the matrix. In the bracket first value is for the row and the second value is for the column.

    Example:

    Input
    import numpy as np
    x=np.eye(2,2)
    print(x)
    Output
    [[1. 0.]
    [0. 1. ]]

    Diag matrix

    To create diagonal matrix diag() function is used. In the function bracket, pass a list that will contain the diag values of the matrix. Diagonal value means, all the values will be zero except diagonal values and the diagonal values will be those values that you passed in the array. In the array, if you have 5 values then 5 rows and 5 columns will be created and if you have 2 values then 2 rows and 2 columns will be created.

    Example 1:

    Input
    import numpy as np
    x=np.diag([1,2,3])
    print(x)
    Output
    [[1 0 0]
    [0 2 0]
    [0 0 3]]

    To find the diag value of a diag matrix write the diag function and in the bracket pass diag matrix variable name.

    Example 2:

    Input
    import numpy as np
    x=np.diag([1,2,3])
    y=np.diag(x)
    print(y)
    Output
    [1 2 3]

    Matrix function

    The matrix function is used to create a matrix. In the bracket, pass the values row wise. For separate row semi-color is used. It takes tree parameter, 1.data or value, 2.data type 3.copy(value True or False). The mathematical operation can be also performed which can be performed with the array.

    Example 1:

    Input
    import numpy as np
    x=np.matrix("1 2;3 4;5 6")
    print(x)
    Output
    [[1 2]
    [3 4]
    [5 6]

    Example 2:

    Input
    import numpy as np
    x=np.matrix([[1,2],[3,4],[5,6]])
    print(x)
    Output
    [[1 2]
    [3 4]
    [5 6]

    Difference between matrix and array

    1.You can create a matrix with string notation but can't create an array.
    2.Matrix is always 2D but the array can be 3D,4D, and more.
    3.Array and matrix multiplication are different.

    CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

    © Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.