Machine Learning, Deep Learning for beginners: Numpy Library and TensorFlow

Tram Ho

Let’s review the relevant knowledge from the previous article and learn more about the APIs of the Numpy library

I. Numpy

II Introduction to NumPy

  • NumPy’s homepage
  • NumPy stands for “Numerical Python”, is a library specializing in processing, calculating vectors, matrices.
  • NumPy is often used with SciPy and Matplotlib to replace the extremely expensive MatLab.
  • Written in Python and C, so performance is good.

I.2 APIs

1. NumPy Arrays – NumPy Array

2. Access through index and cut Array – Array Indexing and Array Slicing

a. Indexing and Slicing

Indexing and slicing are similar to lists in Python.

There are lots of ways to do this, but I just show you the most understandable and practical ways.

You can refer to the details here

  • Basic indexing

import numpy as np

2-dimensional arrays. We can think of a 2-dimensional matrix.

NumPy’s multi-dimensional matrix simulation is based on the use of the list inside the list (nested list) so accessing the elements in this array is exactly the same as we have done in Python.

Counter item

Access the first row of the matrix

Access the last row of the matrix

Access column

Access the first column of the matrix: this is equivalent to accessing all rows and taking the first element.

Access the last column of the matrix

Access the middle column of the matrix

Access arrays in matrices

Steps to proceed

  • Access row 2 and row 3 through indexes 1 and 2
  • Access column 2 and column 3 of the above 2 rows through indexes 2 and 3

Access element

Access element

Steps to take:

  • Access row 1 via index is 0
  • Accessing column element 2 row 1 through index is 1

Access the row element 2 column 3

Steps to take:

  • Access row 2 via index is 1
  • Access column element 3 of row 2 via index is 2

3. Spreading – Broadcasting

Watch carefully about Broadcasting

Boardcasting we can translate is viral, the way we simultaneously change the value of many elements of an array. More specifically, Numpy allows performing calculations between arrays with numbers and arrays with different dimensions.

Image source: Python Data Science Handbook – Jake VanderPlas

a. Spread the array with numbers

Addition of arrays with numbers

One-dimensional array

As we saw above, 3 is added to all (broadcasting) elements of x.

A simple analogy like the first line of Figure 2, Numpy converts the number 3 into a one-dimensional array, [3, 3, 3] to perform this addition.

Two-dimensional array

Multiplication arrays with numbers

One-dimensional array

Two-dimensional array

b. Spread the array with the array

Addition of 2 arrays of different dimensions

2-dimensional array with 1-dimensional array

There’s more …

Share the news now

Source : Viblo