Mathematics for Data Science – DatabaseTown https://databasetown.com Data Science for Beginners Tue, 14 Mar 2023 14:50:51 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.2 https://databasetown.com/wp-content/uploads/2020/02/dbtown11-150x150.png Mathematics for Data Science – DatabaseTown https://databasetown.com 32 32 165548442 Transpose of a Matrix in Python https://databasetown.com/transpose-of-a-matrix-in-python/ https://databasetown.com/transpose-of-a-matrix-in-python/#respond Thu, 13 Feb 2020 15:41:41 +0000 https://databasetown.com/?p=3103 A transpose of a matrix is obtained by interchanging all its rows into columns or columns into rows. It is denoted by \(\displaystyle {{A}^{t}}\) or \(\displaystyle {{A}^{‘}}\). For example,

If  \(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 3 & 5 \\ 2 & 6 & 2 \end{array}} \right]\,\) then  \(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 4 & 2 \\ 2 & 3 & 6 \\ 3 & 5 & 2 \end{array}} \right]\)

In transpose of a matrix, the values of matrix are not changing, only their positions are changing. When we take the transpose of a same vector two times, we again obtain the initial vector. Further, A m x n matrix transposed will be a n x m matrix as all the rows of a matrix turn into columns and vice versa.

Let’s start a practical example of taking a transpose of a matrix in python…

First, we import the relevant libraries in Jupyter Notebook. We use NumPy, a library for the python programming that allows us to work with multidimensional arrays and matrices along with a large collection of high-level mathematical functions to operate on these arrays as shown below,

Transpose of a Matrix

Transpose of a Matrix in Python

Let’s see more examples of transpose of a matrix …

Transpose of a Matrix in Python
Transpose of a Matrix in Python

In above examples, you can see that, only rows of two matrices B and C are changed into columns and columns into rows.

Let’s see an example of taking transpose of a scalar and a vector.

transpose of a scalar and vector

It transpires from above examples that when we take a transpose of a scalar or vector, we obtain the same result because in python one dimensional array do not get transposed.

However, if we intend to get the transpose of a vector then we will reshape it into 4 x 1 matrix or 2-dimensional array, let’s do it practically,

transpose of a vector

Further reading

]]>
https://databasetown.com/transpose-of-a-matrix-in-python/feed/ 0 3103
Dot Product of Two Matrices in Python https://databasetown.com/dot-product-of-two-matrices-in-python/ https://databasetown.com/dot-product-of-two-matrices-in-python/#respond Wed, 05 Feb 2020 17:13:22 +0000 https://databasetown.com/?p=3039 The product of two matrices A and B will be possible if the number of columns of a Matrix A is equal to the number of rows of another Matrix B. A mathematical example of dot product of two matrices A & B is given below.

If

\(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right]\)

and

\(\displaystyle B=\left[ {\begin{array}{*{20}{c}} 3 & 2 \\ 1 & 4 \end{array}} \right]\)

Then,

\(\displaystyle AB=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right] \left[ {\begin{array}{*{20}{c}} 3 & 2 \\ 1 & 4 \end{array}} \right]\)

\(\displaystyle AB=\left[ {\begin{array}{*{20}{c}} {1\times 3+2\times 1} & {1\times 2+2\times 4} \\ {3\times 3+4\times 1} & {3\times 2+4\times 4} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {3+2} & {2+8} \\ {9+4} & {6+16} \end{array}} \right]\)

\(\displaystyle AB=\left[ {\begin{array}{*{20}{c}} 5 & {10} \\ {13} & {22} \end{array}} \right]\)

Let’s start a practical example of dot product of two matrices A & B in python. First, we import the relevant libraries in Jupyter Notebook.

Dot Product of two Matrices

Let’s see another example of Dot product of two matrices C and D having different values.

If all the diagonal elements of a diagonal matrix are same, then it is called a Scalar Matrix. We can also take the dot product of two scalars which result will also a scalar, like this

Linear Algebra is mostly concerned with operations on vectors and matrices. Let’s take an example of dot product of one scalar and one vector…

It is clear from above snap that, the result obtained after taking dot product of a scalar and a vector is also a vector because a scalar value i.e. 2 is multiplied with each value of a vector i.e. 1, 2, 3 & 4 and we obtained a vector having values 2, 4, 6 & 8.

]]>
https://databasetown.com/dot-product-of-two-matrices-in-python/feed/ 0 3039
Matrix Addition and Subtraction in Python https://databasetown.com/matrix-addition-and-subtraction-in-python/ https://databasetown.com/matrix-addition-and-subtraction-in-python/#respond Tue, 28 Jan 2020 17:45:04 +0000 https://databasetown.com/?p=3003 Matrix Addition and Subtraction in Python programming language is performed like the normal algebraic operations.

Before discussing these operations, it is necessary to introduce a bit about Algebra which has been taken from the Arabic word Al-Jabar, afterward, this word turned into Algebra.

Algebra is a branch of Mathematics that provides an easy solution to many complex mathematical problems especially when quantity is represented by a sign without any arithmetical value.

Addition of Matrices

Generally, the addition of two matrices A and B is possible if they have the same orders. The addition of two matrices A and B is denoted by A + B. Mathematical example of addition of two matrices is given here.

If \(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \end{array}} \right]\) and \(\displaystyle B=\left[ {\begin{array}{*{20}{c}} 2 & 3 & 4 \\ 5 & 2 & 4 \end{array}} \right]\)

Then,

\(\displaystyle A+B=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} 2 & 3 & 4 \\ 5 & 2 & 4 \end{array}} \right]\) \(\displaystyle =\left[ {\begin{array}{*{20}{c}} {1+2} & {2+3} & {3+4} \\ {4+5} & {5+2} & {6+4} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 3 & 5 & 7 \\ 9 & 7 & {10} \end{array}} \right]\)

Let’s start a practical example of addition of two matrices in python…

First, we import the relevant libraries in Jupyter Notebook as shown below,

import numpy as np

We use NumPy, a library for the python programming that allows us to work with multidimensional arrays and matrices along with a large collection of high-level mathematical functions to operate on these arrays.

Now we perform the addition of two matrices A and B like this,

addition of matrix in python

Subtraction of Matrices

The subtraction of one matrix from another matrix will be possible if they have the same orders and subtraction of two matrices A and B is denoted by A – B. Mathematical example of subtraction to two matrices is given below

If

\(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 5 & 6 & 7 \end{array}} \right]\) and \(\displaystyle B=\left[ {\begin{array}{*{20}{c}} 2 & 5 & 3 \\ 4 & 1 & 8 \end{array}} \right]\)

Then,

\(\displaystyle A-B=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 5 & 6 & 7 \end{array}} \right]-\left[ {\begin{array}{*{20}{c}} 2 & 5 & 3 \\ 4 & 1 & 8 \end{array}} \right]\)

\(\displaystyle =\left[ {\begin{array}{*{20}{c}} {1-2} & {2-5} & {3-3} \\ {5-4} & {6-1} & {7-8} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {-1} & {-3} & 0 \\ 1 & 5 & {-1} \end{array}} \right]\)

Let’s start a practical example of subtraction of two matrices in python. We have already imported the relevant libraries in Jupyter Notebook as mentioned above, therefore, now we perform the subtraction of two matrices A and B like this,

subtraction of matrix in python

Let’s see an example of addition of two vectors but we must care about the length of these two vectors that should be same.

addition of two vectors in python

An example of subtraction of two vectors having same length is given below,

subtraction of two vectors in python

Related post: Linear Algebra for Data Science

]]>
https://databasetown.com/matrix-addition-and-subtraction-in-python/feed/ 0 3003
Linear Algebra in TensorFlow (Scalars, Vectors & Matrices) https://databasetown.com/linear-algebra-in-tensorflow/ https://databasetown.com/linear-algebra-in-tensorflow/#respond Wed, 22 Jan 2020 14:58:07 +0000 https://databasetown.com/?p=2987 Linear Algebra in TensorFlow: TensorFlow is open source software under Apache Open Source license for dataflow which is frequently being used for machine learning applications like deep-neural-network to improve the performance of search engines, such as, Google, image captioning, recommendation and translation.

For example, when a user types a keyword in Google’s search bar, it provides a recommendation which could be helpful for users or researcher. The stable version of TensorFlow appeared in 2017 which was developed by the Google Brain Team to improve the services of Gmail and Google search engine.

Its architecture performs in three parts such as data preprocessing, model building, model training and model estimation.

TensorFlow acquires input as multi dimensional array and its library fit in various API to make at scale deep learning architecture such as CNN or RNN.

TensorFlow runs on GPU and CPU. It is based on graph calculation which permits the developer to visualize the construction of the Neural Network with TensorBoard as it runs on GPU and CPU.  

These are the algorithms supported by TensorFlow.

  • Classificationtf.estimator.LinearClassifier
  • Deep Learning Classificationtf.estimator.DNNClassifier
  • Deep Learning wipe and deeptf.estimator.DNNLinearCombinedClassifier
  • Boosted Tree Classificationtf.estimator.BoostedTreesClassifier
  • Linear Regressiontf.estimator.LinearRegressor
  • Boosted Tree Regressiontf.estimator.BoostedTreesRegressor

You can see more details here.

Before to start a practical example of TensorFlow, it is essential to recall the concepts of scalar, vector, and matrix.

A scalar is always one by one so, it has the lowest dimensionality, whereas, each element of a vector is a scalar and dimension of a vector is (m x 1) or (1 x m) matrix and a matrix is a collection of vectors (m x n) or a collection of scalars.

A few instances of scalar, vector, and matrix are given below.

Examples of 1 x 1 Scalar:

  • [2]
  • [4]

Examples of m x 1 Vector:

  • $latex \displaystyle \left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right]$
  • $latex \displaystyle \left[ {\begin{array}{*{20}{c}} 4 \\ 6 \\ 2 \end{array}} \right]$

Examples of m x n Matrices:

  • $latex \displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 4 & 1 \\ 2 & 5 & 3 \\ 3 & 6 & 2 \end{array}} \right]$
  • $latex \displaystyle \left[ {\begin{array}{*{20}{c}} 3 & 4 & 7 \\ 1 & 3 & 0 \\ 8 & 2 & 5 \end{array}} \right]$

Let’s start a practical example of TensorFlow…

Practical Example of TensorFlow

Before to create a Tensor, it is essential, first to import the relevant library in Jupyter Notbook as shown in below snap.

Import the relevant library:

import numpy as np

Creating a Tensor and checking its shape: Now we are going to create and Tensor and check its shape. Tensor can be stored in an array like this,

creating tensor

In this example, firs,t we take two matrices t1 and t2 and create an array with two elements t1 and t2 and the result obtained in the form of an array which contains these two matrices.

Now we check this shape like this,

tensor.shape

The above result depicts that this array contains two matrices, each of which is 2 by 3.

Manually creating a Tensor:

We can also create a tensor manually, but in fact, it is a bit difficult as various brackets are involved.

This is an example of Linear Algebra in TensorFlow

Linear Algebra in TensorFlow
Linear Algebra in TensorFlow

Read related article: Linear Algebra for Data Science

]]>
https://databasetown.com/linear-algebra-in-tensorflow/feed/ 0 2987
Scalars, Vector and Matrices in Python (Using Arrays) https://databasetown.com/scalars-vectors-and-matrices-in-python/ https://databasetown.com/scalars-vectors-and-matrices-in-python/#respond Thu, 16 Jan 2020 16:07:35 +0000 https://databasetown.com/?p=2951 Arrays in python, are frequently used to work with scalars, vectors and matrices, a topic of today’s post. This post is continuation of linear algebra for data science.

We use NumPy, a library for the python programming which allows us to work with multidimensional arrays and matrices along with a large collection of high-level mathematical functions to operate on these arrays. let’s start a practical example,

import numpy as np
import numpy as np

Declaring Scalar, Vectors and Matrices

declaring scalars

As you have already known that scalar has no dimension and the above example showed how to declare a scalar quantity in python. Now in the next example, we are going to declare a new variable i.e. vector which is equal to an array of 2, 4 and 6 which are enclosed by a bracket like this,

declaring vectors

Now we are going to declare a Matrix having two rows and three columns. The elements of each row are enclosed by a bracket and the two brackets are separated by a comma, whereas, these two rows are enclosed by a whole bracket, result of which is given below,

declaring matrix

Data Types

If we intend to get the data type of any variable like previously declared as scalar, vector and matrix then we simply write type and variable name which enclosed by a bracket as shown in given picture,

data types

In above example, vector is a one-dimensional array and matrix is a two-dimensional array. It is pertinent to mention here that within numpy, integers, floats and arrays with one element perform in the same way as regards linear algebraic operations.

Data Shapes

Shape can also apply to our objects to confirm their dimensionality or shape like this,

It is cleared from above snap that the first matrix has three rows and one column i.e. (3,), whereas, the second matrix has two rows and three columns i.e. (2, 3).

Create a column vector

Here, we use a reshape method which provides an array with a new shape without altering its data like this,

create a column vector in python

You can see that the resultant matrix contains three rows and one column which was previously contained only one row and three columns.

Read also: Introduction to Data Science (A Beginner’s Guide)

]]>
https://databasetown.com/scalars-vectors-and-matrices-in-python/feed/ 0 2951
Linear Algebra for Data Science https://databasetown.com/linear-algebra-for-data-science/ https://databasetown.com/linear-algebra-for-data-science/#respond Thu, 09 Jan 2020 18:34:10 +0000 https://databasetown.com/?p=2914 Linear Algebra for Data Science and machine learning is very essential as the concepts of linear algebra are used to understand the working of algorithms. In this post, we are going to discuss the basic concepts of linear algebra.

Why Linear Algebra?

Enormous datasets mostly contain hundreds to a large number of individual data objects.

It is simpler to work with this information and operate on it when it is characterized in the form of matrices and vectors.

Linear Algebra is a branch of mathematics that manages vectors and tasks on vectors. Linear Algebra is significantly essential for Artificial Intelligence and information handling calculations.

Data Scientists must have basic knowledge of mathematics to solve complex data problems in an efficient way to boost the company revenue.

Linear Algebra is mostly concerned with operations on vectors and matrices, so let’s start learning about matrices and vectors.

What is the Matrix?

Usually, it corresponds to a collection of information stored in an arranged manner. Mathematically, it states to a set of numbers, variables or functions arranged in rows and columns.

Matrices are generally represented by the capital English alphabets like A, B, C……, etc. For example,

\(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 3 & 4 & 5 \\ 6 & 7 & 8 \\ 9 & 2 & 3 \end{array}} \right] \)

The order of a matrix is defined by the number of rows and columns in a matrix.

Order of a matrix = number of rows × number of columns

\(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 2 & 5 & 8 \\ 8 & 2 & 3 \end{array}} \right]\)

In above example, number of rows is 3 and number of columns is also 3, therefore,

Order of matrix A is 3 × 3.

There are following types of matrices:

  • Row Matrix
  • Column Matrix
  • Unit or Identity Matrix
  • Null or Zero matrix
  • Square Matrix
  • Rectangular Matrix
  • Diagonal Matrix
  • Scalar Matrix
  • Negative of a Matrix
  • Transpose of a Matrix
  • Symmetric Matrix
  • Skew-Symmetric Matrix.

The basic operations on matrices are:

  • Addition of Matrices
  • Subtraction of Matrices
  • Product of Matrices

What is a Vector?

Vectors can be considered as an array of numbers having two independent properties that is magnitude and direction where the order of the numbers also matters.

A vector is the simplest linear algebraic object. Vectors are usually represented by a lowercase bold letter like x, y. For example,

\(\displaystyle x=\left[ {\begin{array}{*{20}{c}} {{{x}_{1}}} \\ {{{x}_{2}}} \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ {{{x}_{n}}} \end{array}} \right]\)

Usually, there are two types of vectors i.e. row vectors and column vectors, examples of which are given below:-

Row Vector

\(\displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right] \)

The length of this vector is 3.

Column Vector

\(\displaystyle \left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right] \)

The length of this vector is 4.

The addition of two vectors can be performed by adding the corresponding elements of each vector. 

The dot product or scalar product of two vectors is the addition of the product of the individual components of the two vectors.

Furthermore, the two vectors ‘a’ and ‘b’ are called orthogonal to each other if their dot product is zero. However, if both the orthogonal vectors have unit norm then they are known as orthonormal vectors. 

A set of vectors (v1, v2, .., vn) is known as linearly independent if no vector of the set can be denoted as a linear combination of other vectors.

Simply, you can say that a matrix is a collection of vectors. For example,

\(\displaystyle \left[ {\begin{array}{*{20}{c}} 1 & 4 & 1 \\ 2 & 5 & 3 \\ 3 & 6 & 2 \end{array}} \right]\)

In above example, there are three vectors. First vector contains 1, 2, 3, second vector contains 4, 5, 6 and third vector contain 1, 3, 2.

Therefore, a matrix has two dimensions M by n vectors, whereas, the vector has a single dimension ‘m’ by 1 while scalar has no dimension as shown below: –

\(\displaystyle Matrix\,\,{{A}_{{m\times n}}}=\left[ {\begin{array}{*{20}{c}} {{{a}_{{11}}}} & {{{a}_{{12}}}} & {{{a}_{{13}}}} & {…..} & {{{a}_{{1n}}}} \\ {{{a}_{{21}}}} & {{{a}_{{22}}}} & {{{a}_{{23}}}} & {…..} & {{{a}_{{2n}}}} \\ {{{a}_{{31}}}} & {{{a}_{{32}}}} & {{{a}_{{33}}}} & {…..} & {{{a}_{{3n}}}} \\ . & . & . & . & . \\ . & . & . & . & . \\ . & . & . & . & . \\ . & . & . & . & . \\ . & . & . & . & . \\ . & . & . & . & . \\ {{{a}_{{m1}}}} & {{{a}_{{m2}}}} & {{{a}_{{m3}}}} & {…..} & {{{a}_{{mn}}}} \end{array}} \right]\)

\(\displaystyle Vector\,\,\,\,\,x=\left[ {\begin{array}{*{20}{c}} {{{x}_{1}}} \\ {{{x}_{2}}} \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ {{{x}_{m}}} \end{array}} \right]\)

\(\displaystyle Scalar\,\,=\,\,\,\left[ x \right]\) A scalar has no dimension and direction.

Now, let’s explore the linear algebra and geometry.

A two-dimensional space can be defined by two lines and two lines mean two vectors.

As we have already learned that a matrix is a collection of vectors in linear algebra. So, any two-dimensional space can easily be represented by a matrix.

The following two matrices can be graphically represented in two dimensions as

\(\displaystyle \,\left[ {\begin{array}{*{20}{c}} 2 \\ 4 \end{array}} \right]\)

\(\displaystyle \left[ {\begin{array}{*{20}{c}} {-2} \\ {-4} \end{array}} \right]\)

two dimensional matrix
two dimensional matrix

You can see that, the direction is always from the origin of the graph to the end point. The vector -2 and – 4 is exactly opposite of the first one. Therefore, if we take two vector.

\(\displaystyle \left[ {\begin{array}{*{20}{c}} 1 \\ 0 \end{array}} \right]\) and

\(\displaystyle \,\left[ {\begin{array}{*{20}{c}} 0 \\ 1 \end{array}} \right]\)

then by taking these two vectors together we can generate a matrix i.e. \(\displaystyle \,\left[ {\begin{array}{*{20}{c}} 1 & 0 \\ 0 & 1 \end{array}} \right]\) which is made up of small portions of two axes x and y as shown in figure.

matrix graph
matrix graph

Linear Algebra Applications in Data Science

  • Vectorized Code (also known as Array Programming)
  • Image Recognition
    • Deep Learning
    • CNNs (Convolutional Neural Networks)
  • Dimensionality Reduction
    • Eigenvalues
    • Eigenvector

Next Post: Scalars, Vector and Matrices in Python

Read also: Statistics for Data Science

]]>
https://databasetown.com/linear-algebra-for-data-science/feed/ 0 2914