NumPy

NumPy

NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks.

Numpy Arrays

Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).


Creating NumPy Arrays

From a Python List

We can create an array by directly converting a list or list of lists:

Built-in Methods

There are lots of built-in ways to generate Arrays

arange

Return evenly spaced values within a given interval.


zeros and ones



linspace

Return evenly spaced numbers over a specified interval.


eye

Creates an identity matrix

Random

Numpy also has lots of ways to create random number arrays:

rand

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

randn

Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform:

randint

Return random integers from low (inclusive) to high (exclusive)


Some attribitutes of array are 
  • reshape
  • max
  • min
  • argmax
  • argmin

NumPy Indexing and Selection


Bracket Indexing and Selection

The simplest way to pick one or some elements of an array looks very similar to python lists:

Broadcasting

Numpy arrays differ from a normal Python list because of their ability to broadcast:

Indexing a 2D array (matrices)

The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity.

Fancy Indexing

Fancy indexing allows you to select entire rows or columns out of order,to show this, let's quickly build out a numpy array:


Selection

Let's briefly go over how to use brackets for selection based off of comparison operators.

NumPy Operations

Arithmetic

You can easily perform array with array arithmetic, or scalar with array arithmetic. Let's see some examples:

Universal Array Functions

Numpy comes with many universal array functions, which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones:



Comments

Popular posts from this blog

Pandas

Matplotlib