This article will tell you what is NumPy Ndarray, how to create and manipulate Ndarray objects with examples.
1. What is NDarray.
- NumPy defines an n-dimensional array object, referred to as the Ndarray object.
- It is an array collection composed of a series of elements of the same type.
- Each element in the array occupies a memory block of the same size.
- You can get each element in the array by index or slice.
- 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.
- There are two common layout methods, by row or by column.
- The Ndarray object has a dtype attribute that describes the data type of the element.
2. How To Create NDarray Object.
- 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.
- Below are examples of creating a Ndarray object.
- 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'>
- Create multi-dimensional ndarray.
import numpy >>> y = numpy.array([['python', 'javascript', 'java'],['Linux','macOS','Windows']]) >>> >>> print(y) [['python' 'javascript' 'java'] ['Linux' 'macOS' 'Windows']]
- 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.
- 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
- 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.
- The shape of an array refers to the number of rows and columns of a multidimensional array.
- 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] ] ).
- 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.
- The reshape() function can accept a tuple as a parameter to specify the number of rows and columns of the new array.
- 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