Linear Algebra and add Vector in Data Science

Linear algebra is an essential component in Data Science. As a result, we need to understand the vector which is two dimensional arrays.

We have seen before that we can plot any vector with the help of NumPy, Pandas and Matplotlib.

In addition we have also seen how we can multiply two vectors. 

Therefore we can add two vectors in the same way. 

Let’s try to draw two vectors and add them mathematically and after that we will visualize the value on a Cartesian Coordinate plane. 

Although my drawing is horrible, yet it will give an idea, I hope.

Adding two vectors and plot the graph and Linear Algebra
Adding two vectors and plot the graph and Linear Algebra

As we see in the above image, we have plotted the two single vectors separately first. After that we have tried to plot their sum which we can get by adding them just like scalar values.

As a result, we can plot them to show that when we add them by replacing the head and tail, we get a triangle.

However, we can do the same thing with NumPy and Matplotlib.

Let’s see the code first.


import numpy as np 
import matplotlib.pyplot as plt

first = np.array([5, 7])
second = np.array([2, 4])

# PLotting addition of two arrays or single vectors

sum_of_two_arrays = np.add(first, second)

print(sum_of_two_arrays)

plt.plot(first) 
plt.plot(second) 
plt.plot(sum_of_two_arrays) 
plt.plot(np.array([2, 7]))

# Add Title

plt.title("Matplotlib PLotting NumPy Arrays") 


# Display

plt.show()

The code explains itself. It has two single vector values which we have shown in our horrible drawing. 🙂

Next, we have added them.

Finally we have plotted them separately.

However, to make the triangle complete, we have replaced the head and tail of the two vectors. As a result, we have got the triangle.

Adding two vectors and plot the graph
Adding two vectors and plot the graph

We can get the same result with Pandas and Matplotlib. 

But to do that we need to tweak the above code a little bit.

Let’s see the code first.

import pandas as panda
import matplotlib.pyplot as plt

first = panda.DataFrame({'axis': ['X', 'Y'], 
                      'value': [5, 7]})

second = panda.DataFrame({'axis': ['X', 'Y'], 
                      'value': [2, 4]})

sum_of_two_vectors = panda.DataFrame({'axis': ['X', 'Y'], 
                      'value': [7, 11]})

first_and_second = panda.DataFrame({'axis': ['X', 'Y'], 
                      'value': [2, 7]})

axis_one = first['axis']
value_one = first['value']
axis_two = second['axis']
value_two = second['value']
sum_axis = sum_of_two_vectors['axis']
sum_value = sum_of_two_vectors['value']
first_second_axis = first_and_second['axis']
first_second_value = first_and_second['value']

plt.plot(axis_one, value_one)
plt.plot(axis_two, value_two)
plt.plot(sum_axis, sum_value)
plt.plot(first_second_axis, first_second_value)
plt.show()

To see the code with Google Colab image please visit the GitHub repository page.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

2 responses to “Linear Algebra and add Vector in Data Science”

  1. […] We can add or subtract two vectors just like any scalar value. To prove this, we can plot them and find that they’re adding like scalar values. […]

  2. […] As a result, in linear algebra we have seen vector operations and we have also plotted them on a Car… […]

Leave a Reply