Learn about Numpy libraries in Python (Part 1)

Tram Ho

During this time I had the opportunity to work in the Python language through a corporate health project. After a period of learning and working, I realized that this is a very powerful language, it is powerful because of its versatility, it can do everything related to programming from web, app or. game, but what makes python famous probably comes from the fact that it can code both in the field of data science analysis, building artificial intelligence. One more thing that makes Python so popular is that it has an extremely large ecosystem of support libraries. It is so big that sometimes we have to decide which library to use properly.

Among the millions of libraries that support Python, there is a library that is almost nailed, forcing any programmer who works with Pytohn to learn that is the NumPy library. In today’s post I would like to introduce you to the NumPy library in Python and how to use it in Python.

1. Introduction to the numpy library

Numpy (Numeric Python): is a very popular and powerful math library of Python. NumPy is equipped with optimized functions, allowing efficient work with matrices and arrays, especially large array and matrix data, with much faster processing speed when using Python alone. .

If you want to be a data science intensive programmer, you need to know numpy well. This is one of the most useful python libraries, especially if you are learning about numbers. Since much of Data Science and Machine Learning revolves around Statistics, practice becomes much more important.

NumPy is developed by Jim Hugunin. The original version was Numarray in development, with some additional functions. In 2005, Travis Oliphant created the NumPy package by combining the Numarray features and the Numeric package.

Using NumPy, programmers can do the following:

  • Mathematical and logical operations on arrays.
  • Fourier transforms and processes for manipulating shapes.
  • Mathematical operations involving linear algebra. NumPy has built-in functions for linear algebra and random number generation.

NumPy – The perfect replacement for MatLab

NumPy is commonly used in conjunction with packages like SciPy (Python Scientific) and Mat-plotlib (graphing library). This combination is widely used as an alternative to MatLab, a popular platform for engineering computation. However, Python replaces MatLab which is now seen as a more complete and modern programming language, most importantly, Numpy is a free, open source library compared to MatLab which is a source code library. pay and pay fees.

How to install NumPy

In this article I will practice on ubuntu and Django Framework, if you are running on another OS then gg will have detailed instructions.

First open up Terminal and enter

You need to install Numpy via pip

After NumPy has been installed, we need to import it to use it as other Python libraries to use NumPy’s functions:

After the installation is complete we will learn about data types in Numpy

1. Arrays

An array is a data structure that contains a group of elements. Typically, all of these elements have the same data type, such as integers or strings. They are often used in programs to sort data so that a set of associated values ​​can be easily sorted or searched for.

When it comes to NumPy, an array is the central data structure of the library. It is a grid of values ​​and it contains information about the raw data, how to determine the position of an element, and how to interpret an element. It has a grid of elements that can be indexed in a variety of ways. All elements have the same type, called an array type (data type).

image

An array can be indexed by a set of non-negative integers, by a boolean, by another array, or by an integer. The rank of the array is the dimension number. The shape of the array is a series of integers that indicate the size of the array along each dimension. One way we can initialize the NumPy array is from a nested Python list.

An array is known as the central data structure of the NumPy library. The array in NumPy is called the NumPy Array.

image

The most important object defined in NumPy is a type of N dimensional array called ndarray. It describes a collection of similar items. Items in the collection can be accessed with a zero-based index.

Every item in a ndarray has the same block size in memory. Each element in ndarray is an object of a data type object (called a dtype).

Any item extracted from the ndarray (by trimming) object is represented by a Python object of one of the array scalar types. The following chart shows the relationship between ndarray, object data type (dtype), and array scalar type:

image

An instance of ndarray can be constructed using the different array creation procedures described later in the tutorial. Ndarray is basically created with an array function in NumPy as follows:

It generates a ndarray from any object that shows the array interface, or from any method that returns an array.

The constructor above takes the following parameters:

NoParameter description
firstobject – Any object displaying the array interface method will return an array or any (nested) string.
2dtype – The desired data type of the array, optional
3copy – Not required. By default (true), the object is copied
4order – C (main row) or F (main column) or A (any) (default)
5subok – By default, the return array must be the base class array. If true, the subclasses are passed
6ndmin – Specifies the minimum size of the result array

For example to understand more:

The result: [1, 2, 3]

The result: [[1, 2] [3, 4]]

Results: [[1, 2, 3, 4, 5]]

The result: [1. + 0.j, 2. + 0.j, 3. + 0.j]

Difference between Python List and Numpy Array

  • The Python List can contain elements of different data types while elements of the Numpy Array are always identical (same data type).
  • Python Array is faster and more compact than Python List:
    • NumPy Array uses fixed memory to store data and less memory than Python List.
    • Adjacent memory allocation in NumPy Array

Data type in NumPy

NumPy supports much more number types than Python. The following table shows the different scalar data types defined in NumPy.

NoData type & description
firstbool_ – Boolean (True or False) is stored as bytes
2int_ – default integer type (like C long; usually int64 or int32)
3intc – Identical to int C (usually int32 or int64)
4intp – Integer used for indexing (same as C ssize_t; usually int32 or int64)
5int8 – Byte (-128 to 127)
6int16 – Integer (-32768 to 32767)
7int32 – Integer (-2147483648 to 2147483647)
8int64 – Integer (-9223372036854775808 to 9223372036854775807)
9uint8 – unsigned integer (0 to 255)
tenuint16 – unsigned integer (0 to 65535)
11uint32unsigned integer (0 to 4294967295)
twelfthuint64unsigned integer (0 to 18446744073709551615)
13float_ – Short for float64
14float16 – float: sign bit, 5-bit exponent, 10-bit mantissa
15float32 – float: sign bit, 8-bit exponent, 23-bit mantissa
16float64 – float: sign bit, 11-bit exponent, 52-bit mantissa
17complex_ – Abbreviation for complex128
18complex64 – A complex number, represented by two 32-bit real numbers (real and imaginary components).
19complex128 – Complex number, denoted by two 64-bit real numbers (real and imaginary components).

Data type object (dtype)

A descriptive data type object interprets the fixed memory block corresponding to an array, depending on the following aspects:

  • Data type (integer, float or Python object)
  • The size of the data
  • Byte order (little-endian or big-endian)
  • In the case of a structured type, the names of the fields, the data type of each field, and a portion of the memory block are taken by each field.
  • If the data type is a sub array, its shape and data type

The byte order is determined by prefixing the data type with ‘<‘ or ‘>’. ‘<‘ means the encryption has a small value (least significant is stored in the smallest address). ‘>’ means the encoding is big-endian (the most significant byte is stored at the smallest address).

A dtype object is constructed with the following syntax:

The parameters are:

  • Đối tượng – Converted to an object of data type
  • Căn chỉnh – If true, add a padding to the field to make the field similar to the C-struct
  • Sao chép – Creates a new copy of the dtype object. If false, the result is a reference to the builtin data type object

Array Indexing

NumPy provides several ways to access elements in arrays

Indexing and slicing: Each element in the 1-dimensional array corresponds to an index. Indexes in NumPy, like indices in python, start with 0. If a 1 dimensional array has n elements then the indices run from 0 to n – 1. And similar to list in python, NumPy arrays also has can be sliced.

Boolean array indexing:

Allows you to select arbitrary elements of an array, often used to select elements that satisfy certain conditions.

Conclude

NumPy is a popular and powerful Python math library. It allows for efficient working with matrices and arrays, especially large array and matrix data, with much faster processing speed using pure Python only.

In the article I introduced you to NumPy, its benefits, how to install it to use, learn about NumPy array, numerical data type NumPy. In the next article we will continue to learn about other data types in NumPy

Refer:

Numpy Tutorial Numpy.org Numpy Medium

Share the news now

Source : Viblo