Python NumPy Arrays
In this tutorial, we will cover Numpy arrays, how they can be created, dimensions in arrays, and how to check the number of Dimensions in an Array.
The NumPy library is mainly used to work with arrays. An array is basically a grid of values and is a central data structure in Numpy. The N-Dimensional array type object in Numpy is mainly known as ndarray.
-
Every single element of the ndarray always takes the same size of the memory block.
-
All the elements that are stored in the ndarray are of the same type, referred to as the array dtype
.
-
Indexing in NumPy always starts from the '0' index.
-
In order, to create an ndarray
, we need to pass a list, tuple or an array-like object into the array()
method, and then it will be converted into an ndarray
.
-
If you want to extract any item from ndarray
object then it can be done with the help of slicing after which it is represented by a Python object of one of array scalar types.
Here we have an image below to show you ndarray
, dtype
and array
scalar
type
:
The whole figure is representing a ndarray
and after extracting an element from ndarray
using slicing we get an object that is of scalar type.
We will cover various operations like slicing, indexing and sorting in next few pages.
After understanding NumPy arrays, now we further move on to how to create ndarray
object.
Creation of NumPy ndarray
object
To create the NumPy ndarray
object the array()
function is used in Python.
-
ndarray can also be created with the use of various data types such as lists, tuples, etc.
-
The type of array can be explicitly defined at the time of creating the array
-
It is important to note that the type of the resultant array is simply deduced from the type of the elements in the sequences.
Syntax:
Below we have the required syntax for the array()
function:
numpy.array(object, dtype, copy, order, subok, ndmin)
Let us now discuss the parameters taken by array()
function:
-
object
This parameter is used to indicate an object that exposes the array interface method and returns either an array or any (nested) sequence
-
dtype
It is an optional parameter and used to indicate the desired data type of the array.
-
copy
This parameter indicates that the object is copied. It is an optional parameter with true as its default value.
-
order
This parameter is used to represent the order. The value of this parameter can be C(row-major), F(column-major), or any default value.
-
subok
With this parameter by default returned array is forced to be a base class array. If the value of this parameter is set to true, sub-classes passed through
-
ndmin
This parameter is used to specify the minimum dimensions of the resultant array.
Now it's time to cover a few examples to create ndarray:
Example 1:
Below we have the code to create an ndarray:
import numpy as np
x = np.array([23,56,2])
print (x)
print(type(x))
The output of the above code snippet to create an array is as follows:
[23 56 2]
<class 'numpy.ndarray'>
Example 2:
In this code given below we will create an array using a Python tuple:
import numpy as np
y = np.array((13, 24, 35, 45, 50))
print(y)
print(type(y))
The Output of the above-mentioned code is given below:
[13 24 35 45 50]
<class 'numpy.ndarray'>
Dimensions in the Array
The dimensions in the array means the level of depth. It simply indicates the nested arrays(those arrays which contain arrays as their elements).
There can be any number of dimensions in an array. But we are going to discuss the given below:
-
0-D Arrays
-
1-D Arrays
-
2-D Arrays
-
3-D Arrays
1. 0-D Arrays
0-D arrays are also known as Scalars and these represent the elements in an array. Thus each value in an array is basically a 0-D array.
Example:
Now we will create a 0-D Array with value 100:
import numpy as np
# directly specify the single value
arr = np.array(100)
print(arr)
100
1. 1-D Arrays
1-D Arrays are basic and most common arrays. It is an array that is having 0-D arrays as its elements and thus is called as a uni-dimensional or 1-D array.
Example:
Now we will create a 1-D array that contains 0-d arrays as its elements(scalar values):
import numpy as np
# 4 scalar values
z = np.array([11, 72, 83, 84])
print(z)
[11 72 83 84]
2. 2-D arrays
The 2-D Arrays are those arrays that contain 1-D arrays as its elements are called as 2-D arrays. 2-D arrays are often used to represent a matrix.
Example:
Now we will construct a 2-D array:
import numpy as np
arr = np.array([[11,22,33], [45, 90, 6]])
print(arr)
[[11 22 33]
[45 90 6]]
2. 3-D arrays
The 3-D arrays are those arrays that contain 2-D arrays (matrices) as its elements and are mainly used to represent a 3rd order tensor.
Example:
import numpy as np
arr = np.array([[[11, 2, 33], [43, 54, 6]], [[11, 22, 3], [14, 15, 16]]])
print(arr)
[[[11 2 33]
[43 54 6]]
[[11 22 3]
[14 15 16]]]
Checking the Number of Dimensions of Array:
The ndim
attribute of the NumPy Arrays that returns an integer that tells us how many dimensions an array has. Now in our following example, we will check the dimensions of the array:
import numpy as np
# 0-d array
x = np.array(4)
# 1-d array
y = np.array([1, 2, 3])
# 2-d array
z = np.array([[11, 62, 3], [46,95,96]])
# 3-d array
c = np.array([[[11, 2, 3], [48,85, 6]], [[17,78,78], [44,95, 6]]])
print(x.ndim)
print(y.ndim)
print(z.ndim)
print(c.ndim)
0
1
2
3
Summary
This tutorial is all about Numpy arrays where we learned what is ndarray object in the numpy library, how it can be created, it's syntax and parameters. Then there were a few examples related to this. After that, we covered the concept of dimensions in the Numpy array and then we covered different dimensional arrays with their examples.