0

I just started Data Science and I want to know what the np.ndarray() function's use is, and in which scenario(s) it is used.

Pluviophile
  • 3,808
  • 13
  • 31
  • 54

1 Answers1

0

numpy.ndarray is not a function but a class, as you can see in the documentation.

An ndarray is meant to store data, it is just a multidimensional matrix. You store your matrix data in an ndarray and then you operate on it, adding to other ndarrays, or any other matrix operation you want to compute. From the documentation of ndarray:

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.

While you can create yourself instances of numpy.ndarray by invoking its constructor, most of the times, arrays are created with the convenience function numpy.array, which ends up creating and returning an instance of numpy.ndarray. For instance, let's create a 3x3 identity matrix manually:

import numpy as np

a = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])

And now we can compute operations on it, e.g.:

b = a + a
print(b)

The result is:

[[2 0 0]
 [0 2 0]
 [0 0 2]]
noe
  • 26,410
  • 1
  • 46
  • 76