Numpy and its basics!

Anoop Gupta
4 min readSep 21, 2020

So what is NumPy (or sometimes Num-pee)? It is simple but very effective that allows you to play around with array manipulations. Well, many of you must be thinking that when we can manipulate array in python itself, then what is the need for NumPy. The reason we use NumPy is that:

  • It consumes less memory in comparison to lists.
  • It is faster.
  • It is convenient to use.
  • It has the various function, methods, and variables, that ease our task of matrix manipulations.
  • Elements of an array are stored contiguously in memory that makes all the data available at one place.

This package is very useful when it comes to Data Science. So, how is it used in data science? So as you know data science deals in Machine learning, artificial intelligence, and optimizing your code, and all the data that you see out there that you want to manipulate mostly comprises images. However, images are itself in the form of a multidimensional array. Now, this is where the Numpy package comes into the picture.

Now the question that arises is that I don’t know how to use the NumPy package. So for that here I am, in my this blog I am going to clear your basics about the NumPy package.

Well to get started with NumPy, that there is a prerequisite, that you should be familiar with basic coding in Python. Well if you are not, you can always refer to Python documentation. Trust me, it is the simplest language that you can ever learn from scratch.

So, now as you are familiar with python now let’s get started with.

The first step in any language is to import the package. And how you do it well this way!

Once imported you are good to go will all the tools.

NumPy’s is usually operated on homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. If in case the dimensions are not same, it will show error. If the elements are of not same datatype, they are casted in one. Likewise if in an array, any one element is of string datatype and others of integer, all the entries will be casted to string datatype.

Now in Numpy, the dimensions are called axis. For example, the coordinates of a point in 3D space [1, 2, 1] have one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.

Now the code right down demonstrates what is the datatype, when an list is passed to np.array( ).

Now there are bunch of basic commands that you can run on your pc, some of which are mentioned below.

Adding to it, you can also create arrays with initial placeholders. This minimizes the heavy operation of growing arrays. Likewise,

Now, there is one feature that can be used reshape the matrix keeping the content of the matrix same. While reshaping the array, the array size should be set keeping the original array in mind, that is if original array is of 40*1 dimension, that it can be converted to 8*5 or 10*4 matrix but not to 4*5 or other.

Now since we have learned so much lets go through one last concept, that is slicing the array.

Slicing means listing out elements from one given index to another given index. How we define slicing is like this. We pass index like this ArrName[StartIndex : EndIndex]. We can also define the step size, like this ArrName[Start : End : Step]. If we don’t pass start index its considered to be 0. If we don’t pass end its considered length of array in that dimension. If we don’t pass step its considered 1.

That’s all folks!

You have successfully learned the basics of NumPy.

GitHub file available at:

Feel free to give any suggestions or ask queries ;)

~Anoop Gupta

--

--