Published in

Voice of Code

# Easy guide with NumPy Data-Structures

Deep learning and Machine Learning,are the two most hot topics in field of technology,but it involves a lot of matrix math, and it’s important for us to understand the basics before diving into building our own neural networks.

NumPy is a Python Library, to work easily and efficiently with matrices in Python.It is one such library: that provides fast alternatives to math operations in Python and is designed to work efficiently with groups of numbers — like matrices.

# Using NumPy in Python:

One can install numpy in Python, using pip. Type the command:pip install numpy,to include numpy library.

To include it in the code, add the following to the code.The following command imports numpy as np,to be used in the code.

`import numpy as np`

# The data-types of numpy:

The most common way to work with numbers in NumPy is through `ndarray` objects. They are similar to Python lists, but can have any number of dimensions. Also, `ndarray` supports fast math operations.

Since it can store any number of dimensions, one can use `ndarray`s to represent any of the data types : scalars, vectors, matrices, or tensors.

## 1.Scalar

Single numbers,are called Scalars in Numpy, and they have a dimension of 0.NumPy lets us specify signed and unsigned types, as well as different sizes. So instead of Python’s `int`, you have access to types like `uint8`, `int8`, `uint16`, `int16`, and so on…

To create a NumPy array that holds a scalar, we can do so by passing the value to NumPy’s `array` function.

`s = np.array(5)`

We can see the shape of your arrays by checking their `shape` attribute.

`s.shape`

It gives an output, of (), empty paranthesis, showing it has 0 dimension.Even though scalars are inside arrays, we still use them like a normal scalar.

`x = s + 10`

and x would now equal 15. If you were to check the type of x, you'd find it is probably `numpy.int64`, because its working with NumPy types, not Python types.

## 2.Vector

Vectors are list of numbers,having dimension 1.To create a vector, we have to pass a Python list to the `array` function, like this:

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

If we check vector’s `shape` attribute, it will return a single number representing the vector's one-dimensional length. In the above example, `v.shape` would return `(3,)`

Just like lists,we can access an element within the vector using indices, like this:

`x = v[1]`

Now `x` equals `2`.

NumPy also supports advanced indexing techniques. For example, to access the items from the second element onward, you would say:

`v[1:]`

and it would return an array of `[2, 3]`. NumPy slicing is quite powerful, allowing you to access any combination of items in an `ndarray`.

## 3.Matrices

Matrices are basically 2D vectors,or a list of lists represented in Numpy. We create matrices using NumPy’s `array` function, just like for vectors. However, instead of just passing in a list, one needs to supply a list of lists, where each list represents a row. So to create a 3x3 matrix containing the numbers one through nine, do this:

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

Checking its `shape` attribute would return the tuple `(3, 3)` to indicate it has two dimensions, each length 3.

We can access elements of matrices just like vectors, but using additional index values. So to find the number `6` in the above matrix, we would access `m[1][2]`.

## 4.Tensors

Tensors are n dimensional `ndarray.`They are just like vectors and matrices, but they can have more dimensions. For example, to create a 3x3x2x1 tensor

`t = np.array([[[[1],[2]],[[3],[4]],[[5],[6]]],[[[7],[8]],\    [[9],[10]],[[11],[12]]],[[[13],[14]],[[15],[16]],[[17],[17]]]])`

And `t.shape` would return `(3, 3, 2, 1)`.

We can access items just like with matrices, but with more indices. So `t[2][1][1][0]` will return `16`.

# Changing Shapes and Dimensions:

Sometimes one needs to change the shape of the data without actually changing its contents. For example, we may have a vector, which is one-dimensional, but need a matrix, which is two-dimensional.

Let’s say we have the following vector:

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

Calling `v.shape` would return `(4,)`. But what if we want a 1x4 matrix? We can accomplish that with the `reshape` function, like so:

`x = v.reshape(1,4)`

Calling `x.shape` would return `(1,4)`. Now if we want a 4x1 matrix, we can do this:

`x = v.reshape(4,1)`

The `reshape` function works for more than just adding a dimension of size 1.

# Element-wise operations:

For adding,multiplying or doing any mathematical operation,on a list,we needed to run a loop,and do the calculations we want to perform,using

In NumPy, we could do the following:

`values = [1,2,3,4,5]values = np.array(values) + 5# now values is an ndarray that holds [6,7,8,9,10]`

We should point out, NumPy actually has functions for things like adding, multiplying, etc. But it also supports using the standard math operators. So the following two lines are equivalent:

`x = np.multiply(some_array, 5)x = some_array * 5`

The following code,multiplies each element of the array with 5.

The same functions and operators that work with scalars and matrices also work with other dimensions. We just need to make sure that the items we perform the operation on have compatible shapes.

# NumPy Matrix Multiplication:

For two matrices,to be multiplied:

• The number of columns in the left matrix must equal the number of rows in the right matrix.
• The answer matrix always has the same number of rows as the left matrix and the same number of columns as the right matrix.
• Order matters. Multiplying A•B is not the same as multiplying B•A.
• Data in the left matrix should be arranged as rows., while data in the right matrix should be arranged as columns

Now if the requirements are already fulfilled for a given matrix,we can multiply them using,

To find the matrix product, you use NumPy’s `matmul` function.

`a = np.array([[1,2,3,4],[5,6,7,8]])a.shapeb = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])c = np.matmul(a, b)`

Here,shape of a, is (2,4) and shape of b,is (4,3).So multiplication is possible and the resultant matrix c,has shape (2,3).

However if the above condition,is not fulfilled,then we need to alter the matrices using Transpose.

# Matrix Transpose:

Getting the transpose of a matrix is really easy in NumPy. Simply access its `T` attribute. There is also a `transpose()` function which returns the same thing, but we rarely see that used anywhere.

For example:

`m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])m=m.T`

NumPy does this without actually moving any data in memory — it simply changes the way it indexes the original matrix — so it’s quite efficient.

However, that also means we need to be careful with how we modify objects, because they are sharing the same data. For example, with the same matrix `m` from above, let's make a new variable `m_t` that stores `m`'s transpose. Then look what happens if we modify a value in `m_t`:

`m_t = m.Tm_t[3][1] = 200m_t# displays the following result:# array([[ 1,   5, 9],#        [ 2,   6, 10],#        [ 3,   7, 11],#        [ 4, 200, 12]])m# displays the following result:# array([[ 1,  2,  3,   4],#        [ 5,  6,  7, 200],#        [ 9, 10, 11,  12]])`

Notice how it modified both the transpose and the original matrix.That’s because they are sharing the same copy of data. So remember to consider the transpose just as a different view of the matrix, rather than a different matrix entirely.

--

--

## More from Voice of Code

Voice of Code is a global technical blogging community that plans to encourage people and makes sure that the voice of bloggers reaches beyond borders. We learn better by sharing our knowledge and experience