How to declare an Array Object (ndarray) using NumPy Library

How to declare an array Object using NumPy Library

NumPy which stands for “Numerical Python” is a fundamental package of python used for data analysis and scientific computing projects. NumPy Library provides a feature of n-dimensional array object by which we can contain our large data in flexible way, which is very fast to make scientific or mathematical operations on whole data set that it holds.

There are many ways by which you can create or declare n-dimensional arrays.  In this article, we will discuss about some useful functions for this purpose and learn about how to declare an n-dimensional array using following basic functions discussed below:
  •          array( )
  •          reshape( )
  •          zeros( )
  •          ones( )
  •          arange( )
  •          eyes( )
  •          identity( )
  •          empty( )
  •          linspace( )
  •          repeat( )
  •          vstack( )
  •          hstack( )


array( )
Suppose, we want to declare an array with name 'my_array' of 3x3 dimensions having integer elements as mentioned below;


reshape( )
Using the reshape( ) function, we can also convert one dimensional array to multi-dimensional array;

zeros( ) & ones( )
Now you will create an array of any dimension with all elements zeros or ones using function zeros( ) and ones( ) respectively with given dimensions as mentioned in following example;

arange( )
Another function from NumPy library i.e. arange( ) which is used to create an array with a list having elements in the range given as parameters of this function.


eye( ) & identity( )
As shown in the following examples, you can use eye( ) or identity( ) functions for creating NxN identity matrix;

empty( )
You can also use function empty( ) for creating an array with shape or dimensions defined in it. Created array will hold only memory with no any elements populating in it.


linspacce( )
You can use linespace(start,stop,total_elements) function for creating an array of a list with elements as per parameters passed in this functions;

repeat( )
Using function repeat(list or array, repeats) function, you can create an array of a list with every element repeated by repeats times as shown in following example;

vstack( ) & hstack( )
Here, you can find that using vstack( ) and hstack( ) functions, we can create an array while stacking arrays vertically and horizontally respectively in sequence as shown below;

Comments