NumPy NdarrayNdarray is the ndimensional array object defined in the numpy which stores the collection of the similar type of elements. In other words, we can define a ndarray as the collection of the data type (dtype) objects. The ndarray object can be accessed by using the 0 based indexing. Each element of the Array object contains the same size in the memory. Creating a ndarray objectThe ndarray object can be created by using the array routine of the numpy module. For this purpose, we need to import the numpy. Consider the below image. We can also pass a collection object into the array routine to create the equivalent ndimensional array. The syntax is given below. The parameters are described in the following table.
To create an array using the list, use the following syntax. To create a multidimensional array object, use the following syntax. To change the data type of the array elements, mention the name of the data type along with the collection. Finding the dimensions of the ArrayThe ndim function can be used to find the dimensions of the array. Finding the size of each array elementThe itemsize function is used to get the size of each array item. It returns the number of bytes taken by each array element. Consider the following example. ExampleOutput: Each item contains 8 bytes. Finding the data type of each array itemTo check the data type of each array item, the dtype function is used. Consider the following example to check the data type of the array items. ExampleOutput: Each item is of the type int64 Finding the shape and size of the arrayTo get the shape and size of the array, the size and shape function associated with the numpy array is used. Consider the following example. ExampleOutput: Array Size: 7 Shape: (1, 7) Reshaping the array objectsBy the shape of the array, we mean the number of rows and columns of a multidimensional array. However, the numpy module provides us the way to reshape the array by changing the number of rows and columns of the multidimensional array. The reshape() function associated with the ndarray object is used to reshape the array. It accepts the two parameters indicating the row and columns of the new shape of the array. Let's reshape the array given in the following image. ExampleOutput: printing the original array.. [[1 2] [3 4] [5 6]] printing the reshaped array.. [[1 2 3] [4 5 6]] Slicing in the ArraySlicing in the NumPy array is the way to extract a range of elements from an array. Slicing in the array is performed in the same way as it is performed in the python list. Consider the following example to print a particular element of the array. ExampleOutput: 2 5 The above program prints the 2^{nd} element from the 0^{th} index and 0^{th} element from the 2^{nd} index of the array. LinspaceThe linspace() function returns the evenly spaced values over the given interval. The following example returns the 10 evenly separated values over the given interval 515 ExampleOutput: [ 5. 6.11111111 7.22222222 8.33333333 9.44444444 10.55555556 11.66666667 12.77777778 13.88888889 15. ] Finding the maximum, minimum, and sum of the array elementsThe NumPy provides the max(), min(), and sum() functions which are used to find the maximum, minimum, and sum of the array elements respectively. Consider the following example. ExampleOutput: The array: [ 1 2 3 10 15 4] The maximum element: 15 The minimum element: 1 The sum of the elements: 35 NumPy Array AxisA NumPy multidimensional array is represented by the axis where axis0 represents the columns and axis1 represents the rows. We can mention the axis to perform rowlevel or columnlevel calculations like the addition of row or column elements. To calculate the maximum element among each column, the minimum element among each row, and the addition of all the row elements, consider the following example. ExampleOutput: The array: [[1 2 30] [10 15 4]] The maximum elements of columns: [10 15 30] The minimum element of rows [1 4] The sum of all rows [33 29] Finding square root and standard deviationThe sqrt() and std() functions associated with the numpy array are used to find the square root and standard deviation of the array elements respectively. Standard deviation means how much each element of the array varies from the mean value of the numpy array. Consider the following example. ExampleOutput: [[1. 1.41421356 5.47722558] [3.16227766 3.87298335 2. ]] 10.044346115546242 Arithmetic operations on the arrayThe numpy module allows us to perform the arithmetic operations on multidimensional arrays directly. In the following example, the arithmetic operations are performed on the two multidimensional arrays a and b. ExampleArray ConcatenationThe numpy provides us with the vertical stacking and horizontal stacking which allows us to concatenate two multidimensional arrays vertically or horizontally. Consider the following example. ExampleOutput: Arrays vertically concatenated [[ 1 2 30] [10 15 4] [ 1 2 3] [12 19 29]] Arrays horizontally concatenated [[ 1 2 30 1 2 3] [10 15 4 12 19 29]]
Next TopicNumPy DataTypes
