NumPy is a key Python library that every data scientist should be familiar with. This thorough NumPy tutorial walks you through the basics of NumPy, from basic mathematical operations to how Numpy interacts with picture data.

Before we start the Numpy course, it’s important to note that when individuals first start working with NLP (Natural Language Processing), they utilise default Python lists

They eventually switch to Numpy, though. This is due to the fact that larger experiments with a lot of data aren’t necessarily compatible with standard Python lists. Numpy comes in handy when Python lists use too much memory.

**Implementing Numpy in PyCharm**

import numpy as np

np is the de facto abbreviation for NumPy used by the data science community.

#### NumPy Array

A Numpy Array, also known as a Numpy matrix, is a two-dimensional array with rows and columns. A NumPy array with four columns and three rows is shown below.

#### NumPy Arrays vs. Python Lists â€” What Is the Distinction?

If you know Python, you might be questioning why we need NumPy arrays since we already have Python lists. After all, these Python lists function as an array that may store numerous sorts of items. This is an excellent question, and the solution lies in the way Python keeps objects in memory.

A Python object is essentially a reference to a memory region that contains all of the object’s characteristics, such as bytes and value. Although this extra information is what makes Python a dynamically typed language, it comes at a cost, which is evident when keeping a big collection of objects, such as in an array.

Python lists are just an array of pointers, each referring to a different item in the list.

#### Creating a NumPy Array

Given the complexity of the issues they tackle, NumPy arrays are remarkably simple to generate. The np.array() technique is used to generate a very simple ndarray. All you have to do is pass the array’s values as a list:

np.array([1,2,3,4],dtype=np.float32)

Output:

Â

array([1., 2., 3., 4.], dtype=float32)

Since NumPy arrays can contain only homogeneous datatypes, values will be upcast if the types do not match:

np.array([1,2.0,3,4])

Output:

array([1., 2., 3., 4.])

Here, NumPy has upcast integer values to float values.

NumPy arrays can be multi-dimensional too.

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

array([[1, 2, 3, 4],

Â Â Â Â [5, 6, 7, 8]])

(i) Numpy Tutorial: How to create a NumPy matrix in the same shape but as a different array. This uses the function NumPy.empty_like():

# Creating ndarray from list

c = np.array([[1., 2.,],[1., 2.]])

# Creating new array in the shape of c, filled with 0

d = np.empty_like(c)

(ii) Then you cast the NumPy array from the Python list using the function NumPy.asarray() :

import NumPy as np

list = [1, 2, 3]

c = np.asarray(list)

(iii) You can also create a customized ndarray in the required size. You can fill it with random values, ones, or zeroes.

# Array items as ndarray

c = np.array([1, 2, 3])

# A 2Ã—2 2d array shape for the arrays in the format (rows, columns)

shape = (2, 2)

# Random values

c = np.empty(shape)

d = np.ones(shape)

e = np.zeros(shape)

#### Array of zeros

The np.zeros() function in NumPy allows you to build an array of all zeros. All you have to do is supply the required array’s shape:

np.zeros(5)

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

The one above is a 1-D array while the one below is a 2-D array:

np.zeros((2,3))

array([[0., 0., 0.],

[0., 0., 0.]])

Array of ones

You could also use the np.ones() function to construct an array of all 1s:

#### How to Merge Arrays with Numpy Python

Rather than merging arrays, you should build an array of the desired size and fill it. This is due to the fact that merging arrays only results in the construction of a large array and the copying of the contents into it.

Use these routines instead if you need to combine arrays.

Concatenate

1d arrays:

Â

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

b = np.array([5, 6])

print np.concatenate([a, b, b])

# >> [1 2 3 5 6 5 6]

Â

2d arrays:

a2 = np.array([[1, 2], [3, 4]])

# axis=0 â€“ concatenate along rows

print np.concatenate((a2, b), axis=0)

# >> [[1 2]

# [3 4]

# [5 6]]

# axis=1 â€“ concatenate along columns, but first b needs to be transposed:

b.T

#>> [[5]

# [6]]

np.concatenate((a2, b.T), axis=1)

#>> [[1 2 5]

# [3 4 6]]

#### Append

1d arrays:

# 1d arrays

print np.append(a, a2)

# >> [1 2 3 1 2 3 4]

print np.append(a, a)

# >> [1 2 3 1 2 3]

2d arrays.

pend(a2, b, axis=0)

# >> [[1 2]

# [3 4]

# [5 6]]

print np.append(a2, b.T, axis=1)

# >> [[1 2 5]

# [3 4 6]]

#### Python NumPy Operations

**ndim:Â **

You can determine the array’s dimension, whether it’s a two-dimensional or single-dimensional array. So, let’s look at how we may find the dimensions in practise. I can determine whether the array is single or multidimensional using the ndim function in the code below.

1 | import numpy as np |

2 | a = np.array([(1,2,3),(4,5,6)]) |

3 | print(a.ndim) |

Output â€“ 2

It’sÂ aÂ two-dimension arrayÂ becauseÂ theÂ outputÂ isÂ 2.Â (multiÂ dimension).

Â

**itemsize:**

Each element’s byte size can be calculated. I’ve defined a single-dimensional array in the code below, and we can discover the size of each element using the ‘itemsize’ function.

1 | import numpy as np |

2 | a = np.array([(1,2,3)]) |

3 | print(a.itemsize) |

Output â€“ 4

In the numpy array above, each element takes up 4 bytes.

Â

#### Hstack (stack horizontally) and vstack (stack vertically)

1d arrays:

print np.hstack([a, b])

# >> [1 2 3 5 6]

print np.vstack([a, a])

# >> [[1 2 3]

# [1 2 3]]

2d arrays:

print np.hstack([a2,a2]) # arrays must match shape

# >> [[1 2 1 2]

# [3 4 3 4]]

print np.vstack([a2, b])

# >> [[1 2]

# [3 4]

# [5 6]]

#### linspace

Linspace delivers numbers that are evenly spaced throughout a given interval. For example:

1 | import numpy as np |

2 | a=np.linspace(1,3,10) |

3 | print(a) |

Output â€“ [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111 2.33333333 2.55555556 2.77777778 3

#### Square Root and Standard Deviation in Numpy

The square root function returns the square root of each and every output element. You may also get the standard deviation. Let’s see what happens.

1 |
import numpy as np |

2 |
a=np.array([(1,2,3),(3,4,5,)]) |

3 |
print(np.sqrt(a)) |

4 |
print(np.std(a)) |

#### Max/Min Numpy Tutorial

This function is useful for determining the minimum and maximum values of a NumPy array.

1 | import numpy as np |

2 | a= np.array([1,2,3]) |

3 | print(a.min()) |

4 | print(a.max()) |

5 | print(a.sum()) |

Â

#### Numpy Tutorial: Python Numpy Special Functions

Mathematical functions like sine, tan, cos, log, etc can also be used you can use in NumPy. We can plot the sine, cos, tan function by importing Matplotlib. Hereâ€™s what it looks like for the sine function:

1 | import numpy as np |

2 | import matplotlib.pyplot as plt |

3 | x= np.arange(0,3*np.pi,0.1) |

4 | y=np.sin(x) |

5 | plt.plot(x,y) |

6 | plt.show() |

Â

### Final thought

NumPy is an excellent tool for emerging data scientists who want to execute more complex operations with big volumes of data than the basic Python lists.

There are many more operations that may be performed with these Python Tools that are not covered in this Numpy lesson. You can progress to more sophisticated operations once you’ve mastered the NumPy basics.

Â

Are the possibilities presented by Python inspiring you as well? You can also enrol in a Data Science Course for more profitable career opportunities in Python Data Science.