numpy

 

Helo Folks...!!!

Today let us look at the package numpy in Python.


What does the numpy package provide?

The numpy package is concerned with providing the n-dimensional arrays (ndarrays) which is very much useful for scientific computing. This package allows various operations on the whole array and also on the individual elements in it. This can be installed using the pip command like other packages. To recollect how to install a package visit this link. Type the command given below in the anaconda prompt.

pip install numpy

The numpy package can contain only homogeneous elements. Unlike lists, the elements in this array must be of same data type. The size of the numpy arrays are fixed. There are provisions to alter it, but while doing, it will delete the previous array and the new one with the new size will replace it.

Attributes in numpy 

The basic things for working with these arrays are explained below. A numpy array looks like a n dimensional table of elements. Mostly it is used to store numerical values and manipulate them. Here the dimensions (rows and columns) are referred to as axes. The total number axes are called as rank.
For example : A numpy array of dimension 3X4 is given below.

[ [ 1 , 2 , 3 , 4],
  [ 2 , 1 , 3 , 4],
   [ 4 , 2 , 1 , 3] ]

Now the rank of the ndarray is 2 as it has 2 dimensions only (Tip: The number of opening square brackets in the beginning is the rank of the array). The axis-1 is of length 3 and axis-2 is of length 4.

1.  ndarray.ndim

Denotes the rank (number of axes) of the array.

2. ndarray.shape

Denotes the dimensions of the array. It can be viewed as a tuple, for say (x,y), where 'x' is number of rows and 'y' is number of columns.

3. ndarray.size

Denotes the total number of elements in the array. It can also be obtained by multiplying the tuple values of the size attributes- (x,y).

4.ndarray.dtype

Denotes the data type of the elements in the given numpy array. Some of the data types provided by numpy itself are numpy.int32, numpy.int16, and numpy.float64.

5. ndarray.itemsize

Denotes the size of each element in the array. The unit in which the sizes are mentioned is bytes.

Look at the example given below. To create an array, np.array( array elements ) is used which will be explained in the upcoming posts.





Next Page👉

Comments

  1. Thanks for the comment. Please post if you require some other topics. I will write in a simpler way.

    ReplyDelete

Post a Comment