Numpy Ndarray Example

This article will tell you what is NumPy Ndarray, how to create and manipulate Ndarray objects with examples.

1. What is NDarray.

  1. NumPy defines an n-dimensional array object, referred to as the Ndarray object.
  2. It is an array collection composed of a series of elements of the same type.
  3. Each element in the array occupies a memory block of the same size.
  4. You can get each element in the array by index or slice.
  5. The Ndarray object adopts the index mechanism of the array, maps each element in the array to the memory block, and arranges the memory block according to a certain layout.
  6. There are two common layout methods, by row or by column.
  7. The Ndarray object has a dtype attribute that describes the data type of the element.

2. How To Create NDarray Object.

  1. We can create Ndarray objects through NumPy‘s built-in function array(), its syntax format is as follows.
    numpy.array(object, dtype = None, copy = True, order = None,ndmin = 0)
    
    object: Represents an array sequence.
    
    dtype: Optional parameter that allows you to change the data type of the array.
    
    copy: Optional parameter indicating whether the array can be copied. The default is true.
    
    order: Memory layout options for creating arrays, there are three optional values: C (row sequence) / F (column sequence) / a (default).
    
    ndim: Specifies the dimension of the array.
  2. Below are examples of creating a Ndarray object.
  3. Create a one-dimensional ndarray.
    >>> import numpy
    >>>
    # create the ndarray object with the provided list object.  
    >>> x = numpy.array(['numpy','pandas','matplotlib'])
    >>> 
    >>> print(x)
    ['numpy' 'pandas' 'matplotlib']
    >>> 
    >>> print(type(x))
    <class 'numpy.ndarray'>
  4. Create multi-dimensional ndarray.
    import numpy
    
    >>> y = numpy.array([['python', 'javascript', 'java'],['Linux','macOS','Windows']])
    >>> 
    >>> print(y)
    [['python' 'javascript' 'java']
     ['Linux' 'macOS' 'Windows']]
  5. You can change the data type of array elements by setting the dtype attribute value.
    >>> import numpy
    
    # change the numpy array's element data type to string.
    >>> z = numpy.array([1,3,5,7,9],dtype="str")
    >>>
    # the number element in the array has been changed to string.
    >>> print(z)
    ['1' '3' '5' '7' '9']

3. How To View NDArray Dimensions.

  1. Through the Ndarray‘s ndim attribute, you can view the dimensions of the Ndarray.
    >>> import numpy as np
    
    # create a 2 dimensional array.
    >>> ndarr = np.array([['a', 'b', 'c'], [1, 2, 3], ['python', 'javascript', 'java']]) 
    >>>
    
    # print out the above ndarray
    >>> print(ndarr)
    [['a' 'b' 'c']
     ['1' '2' '3']
     ['python' 'javascript' 'java']]
    >>> 
    >>> 
    # get the ndarr object's dimensions by it's ndim attribute.
    >>> print(ndarr.ndim)
    2
  2. You can also use the ndmin parameter to create Ndarray of different dimensions.
    >>> import numpy as np
    >>>
    # specify the ndarray dimension when call the arrary method.
    >>> a = np.array(['python', 'javascript', 'java'], ndmin = 3)
    >>>
    # print out the ndarray object. 
    >>> print(a)
    [[['python' 'javascript' 'java']]]
    >>> 
    # print out the ndarrary object's dimension.
    >>> print(a.ndim)
    3

4. How To Change The Dimension Of A NDArray.

  1. The shape of an array refers to the number of rows and columns of a multidimensional array.
  2. Changing the dimension of an array is reshaping the shape of the array, for example, changing a 2 rows array ( [ [1,2,3], [4,5,6] ] ) to 3 rows array ( [ [1, 2], [3,4], [5.6] ] ).
  3. The Numpy module provides the reshape() function, which can change the number of rows and columns of a multidimensional array to achieve the purpose of changing the dimension of the NDdarray.
  4. The reshape() function can accept a tuple as a parameter to specify the number of rows and columns of the new array.
  5. Below is the reshape() function example.
    >>> import numpy as np
    >>> 
    >>> source_arr = np.array([['python','javascript'],['java','c#'],['PHP','android']]) 
    >>> 
    >>> print("Source ndarray: ",source_arr) 
    Source ndarray:  [['python' 'javascript']
     ['java' 'c#']
     ['PHP' 'android']]
    >>> 
    >>> reshape_arr = source_arr.reshape(2,3) 
    >>> 
    >>> print("Reshaped ndarray: ",reshape_arr) 
    Reshaped ndarray:  [['python' 'javascript' 'java']
     ['c#' 'PHP' 'android']]
    >>> 
    >>> print(source_arr.ndim)
    2
    >>> print(reshape_arr.ndim)
    2
    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.