Bookmark this hands-on experience to understand NumPy in Data Science and Machine Learning

NumPy (Numerical Python) is a linear algebra library for Python.

Almost all of the other libraries in the Python data ecosystem rely on NumPy as one of their main building blocks.
This is the reason it is so important for data science. Additionaly, NumPy is incredibly fast and has bindings to C libraries.

To get ready using Numpy into your Python distribution, go to your terminal or command prompt, and type the following command:

pip install numpy

Prerequisites

If you do not familiar with Python, the article below will give you a little bit of introduction to Python.

Python: Procedural or Object-Oriented Programming?

It is a little bit argue between procedural and object-oriented, and why we should care about it?

towardsdatascience.com


— array

Numpy¹ array essentially comes in two flavors, one-dimensional vectors or two-dimensional matrices. They can both be called an array. Vectors are strictly one-dimension array and matrices are two-dimensional array. Also, note that a matrix can still only have one row or one column.

np.array method in one-dimensional and two-dimensional array

np.array method

We might see that list can be used as an array. Additionally, we can assign an array to a variable.


— arange

This is really similar to Python’s built-in range function. It is represented with a syntax np.arrange and then passes in a start and stop value. Notice that we have a start, stop, and step size in the third argument. It will return evenly spaced values within the given interval. If we just put it from 0 to 10, remember just like in Python that range indexing starts at 0. It will return a value from 0 up to but not including 10. As a result, we will get an array back from 0 all the way up to 9, it is a 10 digits number. If we want to get the value from 0 all the way to 10, we have to type np.arange(0,11).

np.arange method in actions

np.arange method

Finally, we can add a third argument, which is a step size. For example, we want an array of even numbers from 0 to 10. We could insert an argument 0,11. Following a step size of 2. So that, it will jump in steps of 2. All in all, a range is going to be one of the most useful functions for quickly generating an array using NumPy.


— zeros and ones

NumPy can generate another type of array. For instance, if we want to generate arrays of all 0, we can use zeros method. We can pass in a single-digit or tuple. For a single digit, it will give a result of a dimensional vector. In tuple, on the other hand, the first number is going to represent the number of rows and the second number in that tuple is going to represent the number of columns. For example, to generate 3 rows by 4 columns we can pass in the tuple (3,4).

np.zeros method in one-dimensional and two-dimensional array

np.zeros method

Furthermore, to generate pure ones, we can call method ones. Similarly, we can pass in either a single number for a one-dimensional array or a tuple of dimension for a two-dimensional matrix.

np.ones method in one-dimensional and two-dimensional array

#python #programming #machine-learning #data-science #startup

Data Science with Python: How to Use NumPy Library
1.20 GEEK