Data Science – Plotting Linear Functions

Different types of functions, linear, quadratic, exponential and logarithmic can model various kinds of data.

1. Why Plot Functions?

Plotting functions allows data scientists to:

  • Explore Relationships: Visualize how one variable changes in response to another.
  • Identify Patterns: Detect trends and anomalies.
  • Choose Models: Determine which type of function best fits the data, supporting accurate predictions.

2. Types of Functions and Their Uses

Common functions used in data science include:

  • Linear Functions: Represent simple, straight-line relationships.
  • Quadratic Functions: Model data that shows a parabolic curve, often used in physics or economics.
  • Exponential Functions: Useful for data with rapid growth or decay.
  • Logarithmic Functions: Suitable for data that grows quickly at first and then levels off.

3. Plotting Different Types of Functions

A. Plotting a Linear Function

Linear functions are straightforward and represented by the equation y=mx+b, where m is the slope and b is the intercept.

Example: Linear Function

Consider a simple linear function y=2x+1.

Data Table for Linear Function:

xy=2x+1
-2-3
-1-1
01
13
25

Python Code:

import numpy as np
import matplotlib.pyplot as plt

x_values = np.array([-2, -1, 0, 1, 2])
y_values = 2 * x_values + 1

plt.plot(x_values, y_values, color="blue", marker="o", label="y = 2x + 1")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Linear Function: y = 2x + 1")
plt.legend()
plt.grid()
plt.show()

B. Plotting a Quadratic Function

A quadratic function is represented by y=ax2+bx+c and forms a parabolic curve. These functions are useful for modeling relationships where the rate of change increases or decreases.

Example: Quadratic Function

Consider y=x2−3x+2.

Data Table for Quadratic Function:

xy=x2−3x+2
-212
-16
02
10
20
32
46

Python Code:

x_values = np.linspace(-2, 4, 100)
y_values = x_values**2 - 3*x_values + 2

plt.plot(x_values, y_values, color="green", label="y = x^2 - 3x + 2")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Quadratic Function: y = x^2 - 3x + 2")
plt.legend()
plt.grid()
plt.show()

C. Plotting an Exponential Function

Exponential functions grow (or decay) rapidly and are represented by y=a⋅ebx, where e is Euler’s number (approximately 2.718).

Example: Exponential Function

Consider y=2⋅e0.5x.

Data Table for Exponential Function:

xy=2⋅e0.5x
-20.74
-11.21
02.00
13.30
25.44

Python Code:

x_values = np.linspace(-2, 2, 100)
y_values = 2 * np.exp(0.5 * x_values)

plt.plot(x_values, y_values, color="red", label="y = 2 * e^(0.5 * x)")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Exponential Function: y = 2 * e^(0.5 * x)")
plt.legend()
plt.grid()
plt.show()

D. Plotting a Logarithmic Function

Logarithmic functions increase quickly at first and then level off. They’re often represented as y=a⋅log⁡(x)+b.

Example: Logarithmic Function

Consider y=2⋅log⁡(x)+3.

Data Table for Logarithmic Function:

xy=2⋅log⁡(x)+3
13
24.39
35.19
45.78
56.26

Python Code:

x_values = np.linspace(1, 5, 100)
y_values = 2 * np.log(x_values) + 3

plt.plot(x_values, y_values, color="purple", label="y = 2 * log(x) + 3")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Logarithmic Function: y = 2 * log(x) + 3")
plt.legend()
plt.grid()
plt.show()

4. Comparing Different Functions

Plotting different functions on the same graph can provide insights into their unique characteristics.

Python Code to Compare Functions:

# Generate x values
x_values = np.linspace(-2, 5, 100)

# Define the functions
y_linear = 2 * x_values + 1
y_quadratic = x_values**2 - 3*x_values + 2
y_exponential = 2 * np.exp(0.5 * x_values)
y_logarithmic = 2 * np.log(x_values + 3) # Shift x to avoid log(0)

# Plot all functions
plt.plot(x_values, y_linear, label="Linear: y = 2x + 1", color="blue")
plt.plot(x_values, y_quadratic, label="Quadratic: y = x^2 - 3x + 2", color="green")
plt.plot(x_values, y_exponential, label="Exponential: y = 2 * e^(0.5x)", color="red")
plt.plot(x_values, y_logarithmic, label="Logarithmic: y = 2 * log(x) + 3", color="purple")

# Add labels and title
plt.xlabel("x")
plt.ylabel("y")
plt.title("Comparison of Different Functions")
plt.legend()
plt.grid()
plt.show()

5. Summary of Plotting Functions

In data science, plotting functions allows data scientists to explore data visually, identify trends, and select suitable models.

Understanding how to plot and interpret linear, quadratic, exponential and logarithmic functions is essential for analyzing a wide range of data and making informed decisions based on patterns and behaviors in datasets.

Leave a Comment