• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • AI
  • Blog

What is NumPy?

NumPy is an open source library available in Python that aids in mathematical, scientific, engineering, and data science programming. NumPy is an incredible library to perform mathematical and statistical operations. It works perfectly well for multi-dimensional arrays and matrices multiplication

For any scientific project, NumPy is the tool to know. It has been built to work with the N-dimensional array, linear algebra, random number, Fourier transform, etc. It can be integrated to C/C++ and Fortran.

NumPy is a programming language that deals with multi-dimensional arrays and matrices. On top of the arrays and matrices, NumPy supports a large number of mathematical operations. In this part, we will review the essential functions that you need to know for the tutorial on 'TensorFlow.'

In this tutorial, you will learn.

Why use NumPy?

NumPy is memory efficiency, meaning it can handle the vast amount of data more accessible than any other library. Besides, NumPy is very convenient to work with, especially for matrix multiplication and reshaping. On top of that, NumPy is fast. In fact, TensorFlow and Scikit learn to use NumPy array to compute the matrix multiplication in the back end.

How to install NumPy?

To install Pandas library, please refer our tutorial How to install TensorFlow. NumPy is installed by default. In remote case, NumPy not installed-

You can install NumPy using:

  • Anaconda: conda conda install -c anaconda numpy
  • In Jupyter Notebook :
import sys
!conda install --yes --prefix {sys.prefix} numpy

Import NumPy and Check Version

The command to import numpy is

import numpy as np			

Above code renames the Numpy namespace to np. This permits us to prefix Numpy function, methods, and attributes with " np " instead of typing " numpy." It is the standard shortcut you will find in the numpy literature

To check your installed version of Numpy use the command

print (np.__version__)

Output

1.14.0

Create a NumPy Array

Simplest way to create an array in Numpy is to use Python List

myPythonList = [1,9,8,3]

To convert python list to a numpy array by using the object np.array.

numpy_array_from_list = np.array(myPythonList)

To display the contents of the list

numpy_array_from_list

Output

array([1, 9, 8, 3])

In practice, there is no need to declare a Python List. The operation can be combined.

a  = np.array([1,9,8,3])				

NOTE: Numpy documentation states use of np.ndarray to create an array. However, this the recommended method

You can also create a numpy array from a Tuple

Mathematical Operations on an Array

You could perform mathematical operations like additions, subtraction, division and multiplication on an array. The syntax is the array name followed by the operation (+.-,*,/) followed by the operand

Example:

numpy_array_from_list + 10

Output:

array([11, 19, 18, 13])

This operation adds 10 to each element of the numpy array.

Shape of Array

You can check the shape of the array with the object shape preceded by the name of the array. In the same way, you can check the type with dtypes.

import numpy as np
a  = np.array([1,2,3])
print(a.shape)
print(a.dtype)

(3,)
int64

An integer is a value without decimal. If you create an array with decimal, then the type will change to float.

#### Different type
b  = np.array([1.1,2.0,3.2])
print(b.dtype)

float64

2 Dimension Array

You can add a dimension with a ","coma

Note that it has to be within the bracket []

### 2 dimension
c = np.array([(1,2,3),
              (4,5,6)])
print(c.shape)
(2, 3)

3 Dimension Array

Higher dimension can be constructed as follow:

### 3 dimension
d = np.array([
    [[1, 2,3],
        [4, 5, 6]],
    [[7, 8,9],
        [10, 11, 12]]
])
print(d.shape)
(2, 2, 3)

np.zeros and np.ones

You can create matrix full of zeroes or ones. It can be used when you initialized the weights during the first iteration in TensorFlow.

The syntax is

numpy.zeros(shape, dtype=float, order='C')
numpy.ones(shape, dtype=float, order='C')

Here,

Shape: is the shape of the array

Dtype: is the datatype. It is optional. The default value is float64

Order: Default is C which is an essential row style.

Example:

np.zeros((2,2))

Output:

array([[0., 0.],
          [0., 0.]])
np.zeros((2,2), dtype=np.int16)

Output:

array([[0, 0],
         [0, 0]], dtype=int16)
## Create 1
np.ones((1,2,3), dtype=np.int16)			
array([[[1, 1, 1],        
       [1, 1, 1]]], dtype=int16)			

Reshape and Flatten Data

In some occasion, you need to reshape the data from wide to long.

e  = np.array([(1,2,3), (4,5,6)])
print(e)
e.reshape(3,2)

Output:

[[1 2 3] 
 [4 5 6]]			
array([[1, 2],       
	[3, 4],       
	[5, 6]])			

When you deal with some neural network like convnet, you need to flatten the array. You can use flatten()

e.flatten()			
array([1, 2, 3, 4, 5, 6])			

hstack and vstack

Numpy library has also two convenient function to horizontally or vertically append the data. Lets study them with an example:

## Stack
f = np.array([1,2,3])
g = np.array([4,5,6])

print('Horizontal Append:', np.hstack((f, g)))
print('Vertical Append:', np.vstack((f, g)))

Horizontal Append: [1 2 3 4 5 6]
Vertical Append: [[1 2 3]
 [4 5 6]]

Generate Random Numbers

To generate random numbers for Gaussian distribution use

numpy .random.normal(loc, scale, size)

Here

  • Loc: the mean. The center of distribution
  • scale: standard deviation.
  • Size: number of returns
## Generate random nmber from normal distribution
normal_array = np.random.normal(5, 0.5, 10)
print(normal_array)			
[5.56171852 4.84233558 4.65392767 4.946659   4.85165567 5.61211317 4.46704244 5.22675736 4.49888936 4.68731125]			

If plotted the distribution will be similar to following plot

Asarray

Consider the following 2-D matrix with four rows and four columns filled by 1

A = np.matrix(np.ones((4,4)))			

If you want to change the value of the matrix, you cannot. The reason is, it is not possible to change a copy.

np.array(A)[2]=2
print(A)			
[[1. 1. 1. 1.]
 [1. 1. 1. 1.] 
 [1. 1. 1. 1.] 
 [1. 1. 1. 1.]]

Matrix is immutable. You can use asarray if you want to add modification in the original array. let's see if any change occurs when you want to change the value of the third rows with the value 2

np.asarray(A)[2]=2
print(A)

Code Explanation:

np.asarray(A): converts the matrix A to an array

[2]: select the third rows

[[1. 1. 1. 1.] 
      [1. 1. 1. 1.] 
      [2. 2. 2. 2.] # new value 
      [1. 1. 1. 1.]]

Arrange

In some occasion, you want to create value evenly spaced within a given interval. For instance, you want to create values from 1 to 10; you can use arrange

Syntax:

numpy.arange(start, stop,step) 
  • Start: Start of interval
  • Stop: End of interval
  • Step: Spacing between values. Default step is 1

Example:

np.arange(1, 11)			
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])			

If you want to change the step, you can add a third number in the parenthesis. It will change the step.

np.arange(1, 14, 4)			
array([ 1,  5,  9, 13])			

Linspace

Linspace gives evenly spaced samples.

Syntax:

numpy.linspace(start, stop, num, endpoint)

Here,

  • Start: Starting value of the sequence
  • Stop: End value of the sequence
  • Num: Number of samples to generate. Default is 50
  • Endpoint: If True (default), stop is the last value. If False, stop value is not included.

For instance, it can be used to create 10 values from 1 to 5 evenly spaced.

np.linspace(1.0, 5.0, num=10)			

array([1.        , 1.44444444, 1.88888889, 2.33333333, 2.77777778,       3.22222222, 3.66666667, 4.11111111, 4.55555556, 5.        ])			

If you do not want to include the last digit in the interval, you can set endpoint to false

np.linspace(1.0, 5.0, num=5, endpoint=False)			
array([1. , 1.8, 2.6, 3.4, 4.2])

LogSpace

LogSpace returns even spaced numbers on a log scale. Logspace has the same parameters as np.linspace.

np.logspace(3.0, 4.0, num=4)			

array([ 1000. ,  2154.43469003,  4641.58883361, 10000.        ])			

Finaly, if you want to check the size of an array, you can use itemsize

x = np.array([1,2,3], dtype=np.complex128)
x.itemsize			

16

The x element has 16 bytes.

Indexing and slicing

Slicing data is trivial with numpy. We will slice the matrice e. Note that, in Python, you need to use the brackets to return the rows or columns

## Slice
e  = np.array([(1,2,3), (4,5,6)])
print(e)
[[1 2 3]
 [4 5 6]]

Remember with numpy the first array/column starts at 0.

## First column
print('First row:', e[0])

## Second col
print('Second row:', e[1])
First row: [1 2 3]
Second row: [4 5 6]

In Python, like many other languages,

  • The values before the comma stand for the rows
  • The value on the rights stands for the columns.
  • If you want to select a column, you need to add : before the column index.
  • : means you want all the rows from the selected column.
print('Second column:', e[:,1])			
Second column: [2 5]			

To return the first two values of the second row. You use : to select all columns up to the second

## 
  print(e[1, :2])			
  [4 5]			

Statistical function

Numpy is equipped with the robust statistical function as listed below

Function

Numpy

Min

np.min()

Max

np.max()

Mean

np.mean()

Median

np.median()

Standard deviation

np.stdt()

## Statistical function
### Min 
print(np.min(normal_array))

### Max 
print(np.max(normal_array))
### Mean 
print(np.mean(normal_array))
### Median
print(np.median(normal_array))
### Sd
print(np.std(normal_array))
4.467042435266913
5.612113171990201
4.934841002270593
4.846995625786663
0.3875019367395316

Dot Product

Numpy is powerful library for matrices computation. For instance, you can compute the dot product with np.dot

## Linear algebra
### Dot product: product of two arrays
f = np.array([1,2])
g = np.array([4,5])
### 1*4+2*5
np.dot(f, g)

14

Matrix Multiplication

In the same way, you can compute matrices multiplication with np.matmul

### Matmul: matruc product of two arrays
h = [[1,2],[3,4]] 
i = [[5,6],[7,8]] 
### 1*5+2*7 = 19
np.matmul(h, i)
array([[19, 22],       
            [43, 50]])			

Determinant

Last but not least, if you need to compute the determinant, you can use np.linalg.det(). Note that numpy takes care of the dimension.

## Determinant 2*2 matrix
### 5*8-7*6np.linalg.det(i)			
-2.000000000000005			

Summary

Below, a summary of the essential functions used with NumPy

Objective

Code

Create array

array([1,2,3])

print the shape

array([.]).shape

reshape

reshape

flat an array

flatten

append vertically

vstack

append horizontally

hstack

create a matrix

matrix

create space

arrange

Create a linear space

linspace

Create a log space

logspace

Below is a summary of basic statistical and arithmetical function

Objective

Code

min

min()

max

max()

mean

mean()

median

median()

standard deviation

std()

Here is the complete code:

import numpy as np 

##Create array
### list
myPythonList = [1,9,8,3]
numpy_array_from_list = np.array(myPythonList)
### Directly in numpy
np.array([1,9,8,3])

### Shape
a  = np.array([1,2,3])
print(a.shape)

### Type
print(a.dtype)

### 2D array
c = np.array([(1,2,3),
(4,5,6)])
print("2d Array",c)

### 3D array
d = np.array([    
[[1, 2,3],
[4, 5, 6]],
[[7, 8,9],
[10, 11, 12]]
])
print("3d Array",d)
### Reshape
e  = np.array([(1,2,3), (4,5,6)])
print(e)
e.reshape(3,2)
print("After Reshape",e)
### Flatten
e.flatten()

print("After Flatten",e)

### hstack & vstack
f = np.array([1,2,3])
g = np.array([4,5,6])

print('Horizontal Append:', np.hstack((f, g)))
print('Vertical Append:', np.vstack((f, g)))



### random number
normal_array = np.random.normal(5, 0.5, 10)
print("Random Number",normal_array)

### asarray
A = np.matrix(np.ones((4,4)))
np.asarray(A)
print("Asrray",A)
### Arrange


print("Arrange",np.arange(1, 11))

### linspace

lin = np.linspace(1.0, 5.0, num=10)
print("Linspace",lin)

### logspace
log1 = np.logspace(3.0, 4.0, num=4)
print("Logspace",log1)

### Slicing
#### rows
e  = np.array([(1,2,3), (4,5,6)])
print(e[0])

#### columns
print(e[:,1])

#### rows and columns
print(e[1, :2])

 

YOU MIGHT LIKE: