# Python NumPy Tutorial for Beginners: Learn with Examples

## What is NumPy in Python?

NumPy is an open source library available in Python, which helps in mathematical, scientific, engineering, and data science programming. It is a very useful library to perform mathematical and statistical operations in Python. It works perfectly for multi-dimensional arrays and matrix multiplication. It is easy to integrate with C/C++ and Fortran.

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.

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.’

## 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.

In this Python NumPy Tutorial, we will learn:

## How to Install NumPy

To install NumPy library, please refer our tutorial How to install TensorFlow. NumPy is installed by default with Anaconda.

In remote case, NumPy not installed-

You can install NumPy using Anaconda:

`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 below command:

`print (np.__version__)`

Output:

`1.18.0`

## What is Python NumPy Array?

NumPy arrays are a bit like Python lists, but still very much different at the same time. For those of you who are new to the topic, let’s clarify what it exactly is and what it’s good for.

As the name kind of gives away, a NumPy array is a central data structure of the numpy library. The library’s name is actually short for “Numeric Python” or “Numerical Python”.

## Creating 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)```
Objective Code
Create array array([1,2,3])
print the shape array([.]).shape

## What is numpy.zeros()?

numpy.zeros() or np.zeros Python function is used to create a matrix full of zeroes. numpy.zeros() in Python can be used when you initialize the weights during the first iteration in TensorFlow and other statistic tasks.

numpy.zeros() function Syntax

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

Python numpy.zeros() Parameters

Here,

• Shape: is the shape of the numpy zero array
• Dtype: is the datatype in numpy zeros. It is optional. The default value is float64
• Order: Default is C which is an essential row style for numpy.zeros() in Python.

Python numpy.zeros() Example

```import numpy as np
np.zeros((2,2))```

Output:

```array([[0., 0.],
[0., 0.]])```

Example of numpy zero with Datatype

```import numpy as np
np.zeros((2,2), dtype=np.int16)```

Output:

```array([[0, 0],
[0, 0]], dtype=int16)```

## What is numpy.ones()?

np.ones() function is used to create a matrix full of ones. numpy.ones() in Python can be used when you initialize the weights during the first iteration in TensorFlow and other statistic tasks.

Python numpy.ones() Syntax

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

Python numpy.ones() Parameters

Here,

• Shape: is the shape of the np.ones Python Array
• Dtype: is the datatype in numpy ones. It is optional. The default value is float64
• Order: Default is C which is an essential row style.

Python numpy.ones() 2D Array with Datatype Example

```import numpy as np
np.ones((1,2,3), dtype=np.int16)```

Output:

```array([[[1, 1, 1],
[1, 1, 1]]], dtype=int16)```

## numpy.reshape() function in Python

Python NumPy Reshape function is used to shape an array without changing its data. In some occasions, you may need to reshape the data from wide to long. You can use the np.reshape function for this.

Syntax of np.reshape()

`numpy.reshape(a, newShape, order='C')`

Here,

a: Array that you want to reshape

newShape: The new desires shape

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

Example of NumPy Reshape

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

Output:

``` // Before reshape
[[1 2 3]
[4 5 6]]```
```//After Reshape
array([[1, 2],
[3, 4],
[5, 6]])```

## numpy.flatten() in Python

Python NumPy Flatten function is used to return a copy of the array in one-dimension. When you deal with some neural network like convnet, you need to flatten the array. You can use the np.flatten() functions for this.

Syntax of np.flatten()

`numpy.flatten(order='C')`

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

Example of NumPy Flatten

`e.flatten()`

Output:

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

## What is numpy.hstack() in Python?

Numpy.hstack is a function in Python that is used to horizontally stack sequences of input arrays in order to make a single array. With hstack() function, you can append data horizontally. It is a very convenient function in NumPy.

Lets study hstack in Python with an example:

Example:

```## Horitzontal Stack
import numpy as np
f = np.array([1,2,3])
g = np.array([4,5,6])
print('Horizontal Append:', np.hstack((f, g)))```

Output:

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

## What is numpy.vstack() in Python?

Numpy.vstack is a function in Python which is used to vertically stack sequences of input arrays in order to make a single array. With vstack() function, you can append data vertically.

Lets study it with an example:

Example:

```## Vertical Stack
import numpy as np
f = np.array([1,2,3])
g = np.array([4,5,6])
print('Vertical Append:', np.vstack((f, g)))```

Output:

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

After studying NumPy vstack and hstack, let’s learn an example to generate random numbers in NumPy.

## Generate Random Numbers using NumPy

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

Example:

```## 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

## NumPy Asarray Function

The asarray()function is used when you want to convert an input to an array. The input could be a lists, tuple, ndarray, etc.

Syntax:

`numpy.asarray(data, dtype=None, order=None)[source]`

Here,

data: Data that you want to convert to an array

dtype: This is an optional argument. If not specified, the data type is inferred from the input data

Order: Default is C which is an essential row style. Other option is F (Fortan-style)

Example:

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

```import numpy as np
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
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
print(A)```

Code Explanation:

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

: select the third rows

Output:

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

## What is numpy.arange()?

numpy.arange() is an inbuilt numpy function that returns an ndarray object containing evenly spaced values within a defined interval. For instance, you want to create values from 1 to 10; you can use np.arange() in Python function.

Syntax:

`numpy.arange(start, stop, step, dtype)`

Python NumPy arange Parameters:

• Start: Start of interval for np.arange in Python function.
• Stop: End of interval.
• Step: Spacing between values. Default step is 1.
• Dtype: Is a type of array output for NumPy arange in Python.

Example:

```import numpy np
np.arange(1, 11)```

Output:

`array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])`

Example:

If you want to change the step in this NumPy arange function in Python example, you can add a third number in the parenthesis. It will change the step.

```import numpy np
np.arange(1, 14, 4)```

Output:

`array([ 1,  5,  9, 13])`

## NumPy Linspace Function

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.

Example:

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

```import numpy as np
np.linspace(1.0, 5.0, num=10)```

Output:

`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)`

Output:

`array([1. , 1.8, 2.6, 3.4, 4.2])`

## LogSpace NumPy Function in Python

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

Syntax:

`numpy.logspace(start, stop, num, endpoint)`

Example:

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

Output:

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

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

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

Output:

`16`

Each element takes 16 bytes.

## Indexing and Slicing in Python

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
Example:

```## Slice
import numpy as np
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)

## Second col
print('Second row:', e)```

Output:

```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

```## Second Row, two values
print(e[1, :2])
[4 5]```

## Statistical Functions in Python

NumPy has quite a few useful statistical functions for finding minimum, maximum, percentile standard deviation and variance, etc from the given elements in the array. The functions are explained as follows −

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.std()

Consider the following Array:

Example:

```import numpy as np
normal_array = np.random.normal(5, 0.5, 10)
print(normal_array)```

Output:

`[5.56171852 4.84233558 4.65392767 4.946659   4.85165567 5.61211317 4.46704244 5.22675736 4.49888936 4.68731125]`

Example of NumPy 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))```

Output:

```4.467042435266913
5.612113171990201
4.934841002270593
4.846995625786663
0.3875019367395316```

## What is numpy dot product?

Numpy.dot product is a powerful library for matrix computation. For instance, you can compute the dot product with np.dot. Numpy.dot product is the dot product of a and b. numpy.dot() in Python handles the 2D arrays and perform matrix multiplications.

Syntax:

`numpy.dot(x, y, out=None)`

Parameters

Here,

x,y: Input arrays. x and y both should be 1-D or 2-D for the np.dot() function to work

out: This is the output argument for 1-D array scalar to be returned. Otherwise ndarray should be returned.

Returns

The function numpy.dot() in Python returns a Dot product of two arrays x and y. The dot() function returns a scalar if both x and y are 1-D; otherwise, it returns an array. If ‘out’ is given then it is returned.

Raises

Dot product in Python raises a ValueError exception if the last dimension of x does not have the same size as the second last dimension of y.

Example:

```## 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)```

Output:

`14`

## Matrix Multiplication in Python

The Numpy matmul() function is used to return the matrix product of 2 arrays. Here is how it works

1) 2-D arrays, it returns normal product

2) Dimensions > 2, the product is treated as a stack of matrix

3) 1-D array is first promoted to a matrix, and then the product is calculated

Syntax:

`numpy.matmul(x, y, out=None)`

Here,

x,y: Input arrays. scalars not allowed

out: This is optional parameter. Usually output is stored in ndarray

Example:

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)```

Output:

```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.

Example:

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

Output:

`-2.000000000000005`

## Summary

• NumPy is an open source library available in Python, which helps in mathematical, scientific, engineering, and data science programming.
• numpy.zeros() or np.zeros Python function is used to create a matrix full of zeroes.
• numpy.ones() in Python can be used when you initialize the weights during the first iteration in TensorFlow and other statistic tasks.
• Python NumPy Reshape function is used to shape an array without changing its data.
• Python NumPy Flatten function is used to return a copy of the array in one-dimension.
• Numpy.hstack is a function in Python that is used to horizontally stack sequences of input arrays in order to make a single array.
• Numpy.vstack is a function in Python which is used to vertically stack sequences of input arrays in order to make a single array.
• numpy.arange() is an inbuilt numpy function that returns an ndarray object containing evenly spaced values within a defined interval.
• Numpy.dot product is a powerful library for matrix computation.
• The Numpy matmul() function is used to return the matrix product of 2 arrays.