Slope of Line, Python, Data Science

In our previous section we have seen how we can find the slope of a line in python. But how can we relate this to data science?

In addition, can we explain the slope of the line in terms of data science?

Yes, we can.

A slope of line stands for a line segment that refers to how steep it is. For example, think of a mountain. You’re climbing down.

The more steep it is, the harder it will be to climb up or down. Right?

Suppose we have records of all the known mountains in the world. As a result, we can plot the slope of the line with the help of the Matplotlib package in Python. 

For instance consider the code as follows.

import pandas as panda
import matplotlib.pyplot as plt

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

axis = data_frame_of_numbers['axis']

value = data_frame_of_numbers['value']

plt.plot(axis, value)
plt.show()

As we run the code, what do we find?

Let’s run the code.

Slope of line, python, data science
Slope of line, python, data science

It gives us the exact value. However, it does not make any sense.

Now consider the following code.

import pandas as panda
import matplotlib.pyplot as plt

data_frame_one = panda.DataFrame({'axis': ['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y'], 
                      'value': [2, 4, 4, 6, 6, 8, 8, 10]})

axis = data_frame_one['axis']

value = data_frame_one['value']

plt.plot(axis, value)
plt.show()

As a result, we see the following output.

Plotting different Slope of lines with Matplotlib
Plotting different Slope of lines with Matplotlib

On the one hand the value of X is clear. And on the other hand, the value of Y is also clear.

There is no ambiguity. Isn’t it?

In addition, the Matplotlib package in python shows that there is a common pattern in these slopes of lines.

That’s true.

Because each slope of the line represents the same steepness. Right? 

Relationship between slope of line, python and data science

However, although it looks good, and finds a common pattern which is the hallmark of data science, still it doesn’t make any sense.

Certainly we can try to make it look sensible with the next code.

This code is much simpler than before.

import matplotlib.pyplot as plt

def find_slope_of_line(x1, y1, x2, y2):
    slope_of_line = (y2 - y1) /(x2 - x1)
    print(f'Slope of line is: {slope_of_line}')
    
find_slope_of_line(0, 3, 4, 23)

# Slope of line is: 5.0

plt.plot((0, 3), (4, 23))

plt.show()

Firstly, in the above code, we have defined a function.

What does this function return?

A simple procedure.

The formula to find the slope of the line.

What is the formula?

When the values of X coordinate and Y coordinate change,  the slope of the line changes. 

How does it happen?

Simple. 

The difference in Y coordinate divided by the difference in X coordinate gives us the value of the slope of the line.

At any rate, the formula is as follows.  

slope_of_line = (y2 - y1) /(x2 - x1)

In summary, the changed value of Y coordinate by the changed value of X coordinate gives us the slope of the line.

Similarly we have supplied the first value of X coordinate as 0, and the first value of the Y coordinate as 4.

As a result when we run the code, the Matplotlib package plots the graph exactly as we wanted.

Slope of line, python and data science
Slope of line, python and data science

As we place our cursor on the starting point of the slope of the line it shows the first value on the top right hand corner.

Incidentally we can think of X as the representation of Fuel consumption. And Y as the speed of the car.

In that case, when speed is slow, and fuel consumption is 0, the slope of the line represents a pattern.

Isn’t it?

And we have already learned that commonness in pattern plays an important role in data science.

As we progress, we will talk more about it. Of course we’ll learn more data science examples.

So stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

One response to “Slope of Line, Python, Data Science”

  1. […] first and foremost reason is ease of automation. It becomes really helpful when we can automate a few tasks which we can only accomplish with the […]

Leave a Reply