[Python Basics] Numpy — Part1

Numpy is the most fundamental Python package for scientific computing. Studying Tensorflow 2 will lead you to deal with multi-dimensional objects. So let’s just start with Numpy from the basics.

A Ydobon
3 min readJan 17, 2020

Starting with Numpy

First, we need to import the numpypackage. Conventionally, we use np to shorten the name.

The following examples are quite self-explanatory.

import numpy as npa = np.array([1, 2])
>> array([1, 2])
b = np.array([[1.0, 2], [3, 4]])
>> array([[1., 2.],
[3., 4.]])
c = np.array([[[1,2],[3,4],[5,6]], [[7,8],[9,10],[11,12]]])
>> array([[[1, 2],
[3, 4],
[5, 6]],
[[7, 8],
[9, 10],
[11, 12]]])

It will be more understandable if you think this ndarray as a multi-dimensional matrix (see image below).

Note: A frequent error consists in calling np.array with multiple arguments, rather than providing a single sequence as an argument.

Note: If you mix integers with floating-point numbers, numpy will automatically convert all items into the floating-point.

Get quick info on ndarrays

These are the useful numpy attributes when taking a quick look at the ndarrays.

  a.ndimb.shapec.dtype

“ndarray.ndim” shows you the dimension of ndarray. For example, if you want to get the dimension of ndarray “a”, you should write “a.ndim” which will return “1” as a result.

“ndarray.shape” returns the number of rows and columns of given ndarray as a tuple form. Can you guess the value Python will return when you execute “b.shape”?
Answer: “(2,2)” will be the result which means the given ndarray “b” consists of 2 rows and 2 columns.

Maybe you will understand “ndarray.dtype” intuitively. This attribute literally returns the data type of given ndarray. In the case of ndarray “b”, Python automatically detects the dtype as “float64” — this is the default dtype but you can later change “float64” to “float32” if you want. We will go though the related code very soon.

Functions for creating arrays

Numpy provides some useful functions to create ndarrays. Let’s go over them with the following examples.

np.zeros(shape, dtype=np.float64)

This returns a new ndarray of given shape, filled with zeros. You can also specify the data type of zeros — if you don’t, it will return zeros in “float64” format.

np.zeros((2,4))
>> array([0., 0., 0., 0.],
[0., 0., 0., 0.])

np.ones(shape, dtype=np.float64)

This is really similar to “np.zeros()” function, except it fills the ndarray with ones. In the example below, I tried to convert the dtype into “int32” instead of using the default value.

np.ones((1,2,3), dtype=np.int32)
>> array([[[1, 1, 1],
[1, 1, 1]]])

np.eye(n, dtype=np.float64)

This is useful when creating n*n identity matrix whose main diagonal is all 1 and other entries all zero. You can specify the size of matrix by giving an integer number.

np.eye(2)
>> array([[1., 0.],
[0., 1.]])

Creating arrays with sequences of numbers

You can also create ndarrays with uniformly spaced numbers.

np.arange(start, end, step)

np.arange(1, 3, 0.3)
>> array([1. , 1.3, 1.6, 1.9, 2.2, 2.5, 2.8])

“np.arange()” is quite similar to Python “range()” function. From the start number(inclusive) to the end number(exclusive), Numpy will return a sequence of numbers spaced by the specified step.

If only the end number is specified, Numpy interprets this as “np.arange(0,end,1)” by default.

Note that unlike Python “range()” function, “np.arange()” can create a sequence of floating-point numbers, which makes Numpy very useful when dealing with numeric data.

np.linspace(start, end, spaced_num, dtype=np.float64)

np.linspace(0, 5, 6)
>> array([0., 1., 2., 3., 4., 5.])

This generates a uniformly spaced set of numbers over the specified interval. In this case, unlike “np.arange()”, both start and end numbers are inclusive.

Until now, we’ve learned the most basic Numpy functions. If you want to see what’s more in Numpy, please take a look at our next post!

--

--

A Ydobon
A Ydobon

No responses yet