×

NumPy ndarray Class (Numpy Array)

The most important class defined in NumPy is an N-dimensional array type called ndarray.

The ndarray class contains the data structures for representing the multi-dimensional arrays of homogeneous data (all elements in an array having the same data type).

This data structure also contains important metadata about the array and its elements such as its shape, size, data type, and other attributes.

Each element in ndarray is a data-type object (called dtype) and takes the same block size in the memory.

An instance of the ndarray class can be constructed using different array creation functions. A ndarray in NumPy is created using the array() function as follows:

np.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)

The np.array() function takes the following parameters:

  • object: Any array interface method which returns an array, or any (nested) sequence(array_like).
  • dtype: The desired data type for the array. If not given, then then the type will be determined as the minimum type required to hold the objects in the sequence(data-type, optional).
  • ndmin: Specifies the minimum number of dimensions that the resulting array should have(int, optional).

The parameters object, dtype, and ndmin is the more commonly used attributes of the array() function. Other parameters like copy, order, subok, and like are less common in practice and very specifically used as per conditions.

Let us move on, and learn the commonly used parameters of array() function in depth:

Passing object parameter in ndarray

The object parameter is the parameter that holds the array-like structure like lists, tuples. It can be either the variable name of a list/tuple or directly a list/tuple can be passed as an object parameter.

Syntax:

np.array(object)

The object is a mandatory argument(parameter) which means without passing an object as a parameter the array() function will not execute. The following code shows what happens when we don’t pass an object argument in the array():

import numpy as np
np_arr = np.array()

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-81-c8b17c356b49> in <module>
      1 import numpy as np
----> 2 np_arr = np.array()

TypeError: array() missing required argument 'object' (pos 0)

This error can be fixed once we pass a list/tuple as parameter in array() function. Here are some examples of it:

Creating Numpy Array from Python list :

This time we pass a python list named lst as an object parameter in the array() function and there is no error in execution. The array() function gives an ndarray in return which is stored in a variable called np_lst. Then we just write np_lst to see what’s inside in it and the output shows np_lst holds a ndarray having the same values as py_lst.

import numpy as np #importing numpy as np
lst=[1,2,3,4] #A python list
#passing python list to array function which returns an
# ndarray stored as np_lst  
np_lst = np.array(lst)
np_lst   # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([1, 2, 3, 4])

Creating a NumPy array from nested list :

We are passing a nested list; (which can be said as a list of lists) as an object parameter in the array() function and again store array() function as a variable name np_lst.

#pasing a list to array() having more than 1 dimensions
#list of lists
np_lst = np.array([[1,2,3],[4,5,6],[7,8,9]])
np_lst      # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Notice that in this code, we haven’t written import as np, wonder why? The answer is quite simple, the import statement needed to be imported only once in your code (writing import statements at the top of code is a good practice).

If you are using a command line then execute the import statement first, in the case of Python file (.py) mention it at the first line of your code and if you are using any Python Notebook then execute the import statement in the first cell, then again mentioning it is not required.

Creating Array from Tuple :

A tuple is a sequence of elements which is similar to Python lists. The only difference is that Tuple is immutable, that means elements in tuple cannot be manipulated or updated.

We are directly passing a simple tuple initialized with integer elements as an object parameter to the array() function.

#Creating numpy array by passing a tuple
np_lst = np.array((1,4,9))
np_lst     # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([1, 4, 9])

Creating NumPy array from Nested tuple :

We are passing a tuple variable named tup having more than 1 dimension (a tuple of tuples) as an object parameter in the array() function.

#Creating numpy array by passing a tuple of turples
#turple more than 1 dimension
tup = ((1,2,3),(3,6,9),(10,11,12),(13,14,15))
np_lst = np.array(tup)
np_lst     # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([[ 1,  2,  3],
       [ 3,  6,  9],
       [10, 11, 12],
       [13, 14, 15]])

So it can be seen that list, tuple, a list of lists, and a tuple of tuples all can be converted into ndarray using the array() but what if we give a list of tuples or a tuple of lists? The following segments will show the outcomes of it.

Creating Array from Tuple of Lists :

We are passing a tuple of lists named tup_of_lst(having 2 dimensions as tuple contains multiple lists) as an object parameter in the array() function.

#Creating numpy array by passing a tuple of lists
tup_of_lst = ([1,2],[3,4],[5,6])
np_lst = np.array(tup_of_lst)
np_lst    # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([[1, 2],
       [3, 4],
       [5, 6]])

Creating Array from List of Tuples :

We are passing a list of tuples named lst_of_tup(having 2 dimensions as the list contains multiple tuples) as an object parameter in the array() function.

#Creating numpy array by passing a list of turples
lst_of_tup = [(1,2),(4,6)]
np_lst = np.array(lst_of_tup)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([[1, 2],
       [4, 6]])

With this, we have seen all the possibilities with lists and tuples that can be passed as an object parameter of the array() function. In all the cases, array() function converts it to a numpy array of the ndarray class.

Passing dtype parameter in ndarrays

The dtype parameter is the parameter of array() function which determines the data type of each element of the array. This dtype parameter helps in setting the closest minimum data type for an array whether its elements are of different types or the same type.

Syntax:

np.array(object, dtype=None)

This parameter is optional, which means if we do not give the dtype parameter in the array() function, the array() function will still be able to execute without any errors.

We can set dtype of array() for elements of array are having same data type as well as different data type.

For the same data type in dtype

The object parameter will be containing lists/tuples having same data type like integers, float values, etc. throughout the array.

Changing dtype to float from integer list:

We are passing a list of integers as an object parameter in the array() function. Now we use the dtype parameter and set it to float(decimal value type). Both parameters are separated by a comma(,). After that, we again store array() function as a variable name np_lst.

Also notice that as integers elements do not have decimal values, so when the data type is being converted into floating values (float) then each element is having a decimal(.) in it but no values after the decimal.

#setting dtype as float
np_lst = np.array([1,4,9], dtype = float)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

output:

array([1., 4., 9.])

Changing dtype to complex :

We are passing a list of integers as an object parameter in the array() function. Now we use the dtype parameter and set it complex(number having real and imaginary values). Both parameters are separated by a comma(,). After that, we again store array() function as a variable name np_lst.

We can now notice that as integer elements do not have imaginary part of complex numbers, so when the data type is converted into complex numbers then each element is having an imaginary part(.j) of a complex number in it but 0.j showing that there is no imaginary value.

#setting dtype as complex
np_lst = np.array([12,31,79], dtype = complex)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([12.+0.j, 31.+0.j, 79.+0.j])

Changing dtype to bool :

We are passing a list of integer values as an object parameter in the array() function. Now we use the dtype parameter and set it bool(boolean values: ‘True’ or ‘False’).

#setting dtype as bool
np_lst = np.array([2,4,0,6], dtype = bool)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Notice the output, one value is converted as False, and every other value is converted into True, can you tell why? Yes, it’s because whenever there is a non-zero value (doesn’t matter positive or negative), it will be always changing to True value in the boolean conversion. There is a zero value inside the input integer list which makes it False in the output ndarray.

Output:

array([ True,  True, False,  True])

Changing dtype to str :

We are passing a list of boolean values as an object parameter in the array() function. Now we use the dtype parameter and set it str(strings).

#setting dtype as str
np_lst = np.array([True,False,True,True], dtype = str)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Check the output, the values are single-quoted(‘ ‘) because now the type is changed to string(sequence of characters). Also, there is dtype='<U5'mentioned in the output, this shows that the string is encoded as Unicode format and the numerical value 5 next to it shows the number of bytes each string element is assigned.

Output:

array(['True', 'False', 'True', 'True'], dtype='< U5')

Changing dtype to int :

We are passing a list of floats (decimal values) as an object parameter in the array() function. Now we use the dtype parameter and set it to int(integers).

#setting dtype as int
np_lst = np.array([2.9,3.1,4.8,5.2], dtype = int)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([2, 3, 4, 5])

Setting dtype to int for boolean array :

We are passing a list of boolean values as an object parameter in the array() function. Now we use the dtype parameter and set it int(integers).

#setting dtype as int
np_lst = np.array([True, False, True], dtype = int)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([1, 0, 1])

This will be the same if boolean lists is converted into dtype = float, except the output will come as float elements(with decimals) like this:

#setting dtype as float
np_lst = np.array([True, False, True], dtype = float)
np_lst

Output:

array([1., 0., 1.])

Setting dtype to Complex for Boolean array :

We are passing a list of boolean values as an object parameter in the array() function. Now we use the dtype parameter and set it complex(real and imaginary numbers).

#setting dtype as complex
np_lst = np.array([True, False, True], dtype = complex)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

From the output, it is noticeable that the elements after conversion become 1 for True values and 0 for False values, and an additional imaginary part(.j) is added to each element of the array.

Output:

array([1.+0.j, 0.+0.j, 1.+0.j])

Setting dtype to int for complex number array :

We are passing a list of complex numbers as an object parameter in the array() function. Now we use the dtype parameter and set it int(integers).

#setting dtype as int
np_lst = np.array([2+5j, 3+7j, 5.2+9j], dtype = int)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

So we get a TypeError in the output stating that ‘can’t convert complex to int’. This means that it is not possible to convert complex numbers into integers. That’s why we don’t get an array as output.

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
< ipython-input-111-0ed7b4db391b> in < module>
      1 #setting dtype as int
----> 2 np_lst = np.array([2+5j, 3+7j, 5.2+9j], dtype = int)
      3 np_lst 

TypeError: can't convert complex to int

Setting dtype to int for string array :

We are passing a list of strings as an object parameter in the array() function. Now we use the dtype parameter and set it int(integers).

#setting dtype as int
np_lst = np.array(['Hello','World'], dtype = int)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

So we get a ValueError in the output stating that ‘invalid literal for int() with base 10: ‘Hello”. This means that it is not possible to convert strings into integers with base 10 (means range is between 0-9). That’s why we don’t get an array as output.

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-119-b4752189caf2> in <module>
      1 #setting dtype as int
----> 2 np_lst = np.array(['Hello','World'], dtype = int)
      3 np_lst  

ValueError: invalid literal for int() with base 10: 'Hello'

This type of ValueError will also occur if we convert a list of strings to dtype = float, and dtype = complex. You can it check it out with the same code and updating the dtype parameter to float at first and then to complex.

Setting dtype to str for complex number array :

We are passing a list of complex numbers as an object parameter in the array() function. Now we use the dtype parameter and set it str(strings).

#setting dtype as str
np_lst = np.array([1.9+3j, 4.5+2j, 6.1+8j], dtype = str)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Check the output, elements are single quoted(‘ ‘) because now the type is string(sequence of characters) and each complex number is enclosed in parenthesis(open and close brackets).

Also, there is dtype='<U8'mentioned in the output, this shows that the string is encoded as Unicode format and the numerical value 8 next to it shows the number of bytes each string element is assigned.

Output:

array(['(1.9+3j)', '(4.5+2j)', '(6.1+8j)'], dtype='< U8')

It will be the same while converting a list of floats or a list of integers into strings. The only variation in output will be the byte size, as string conversion of complex number took 8 bytes for each element, for float and integer it will depend on the input range and decimal precision in case of float.

You can it check it out with the same code and updating dtype parameter to int and then to float.

Now its time to see some examples of how we can set dtype of sequence for elements having

For different data types in dtype :

dtype to str with different data type elements of array :

We are passing a list containing a float value, an integer value, and a Boolean value as an object parameter and set the dtype parameter to str(strings) in the array() function.

#setting dtype as str
np_lst = np.array([1.95, 34, True], dtype = str)
np_lst   # for other than Jupyter Notebooks, use print(np_lst)

Output:

array(['1.95', '34', 'True'], dtype='< U4')

dtype to float with different data type elements of array :

We are passing a list containing a float value, an integer value, and a Boolean value as an object parameter and set the dtype parameter to float in the array() function.

#setting dtype as float
np_lst = np.array([1.95, 34, True], dtype = float)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([ 1.95, 34.  ,  1.  ])

dtype as default :

We are passing a list containing a float value, an integer value, string value, and a Boolean value as an object parameter, this time we do not specify a dtype parameter in the array() function.

#dtype default 
np_lst = np.array([1.95, 34, 'Hello', True])
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array(['1.95', '34', 'Hello', 'True'], dtype='< U32')

ndmin parameter :

The ndmin parameter of the array() function is used to specify a minimum number of dimensions of the array given by the array() function as output. This is an optional parameter that takes integer values and its default values set to 0.

Syntax:

np.array(ndmin=0)

Here are some examples of how we can set ndmin:

Setting ndmin to 2 :

We are passing a list of integer elements as an object parameter and set ndmin parameter to 2 in the array() function.

#using ndmin
np_lst = np.array([1,2,3,4,5], ndmin = 2)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([[1, 2, 3, 4, 5]])

Setting ndmin to 3 :

We are passing a list of lists having string values as an object parameter and set ndmin parameter to 3 in the array() function.

#using ndmin
np_lst = np.array([['Hi','Hey'],['Hy','Hello']], ndmin = 3)
np_lst  # for other than Jupyter Notebooks, use print(np_lst)

Output:

array([[['Hi', 'Hey'],
        ['Hy', 'Hello']]], dtype='< U5')