Welcome to the lesson on regression analysis, which plays a crucial role in statistical modeling and prediction. Regression analysis helps us understand relationships between variables by fitting a mathematical model to the data. In this lesson, you'll learn how to perform simple linear regression using SciPy. This lesson builds on your previous understanding of descriptive statistics, probability distributions, and correlation and will prepare you for further exploration of statistical methods.
To explore simple linear regression, we need datasets x and y, representing the independent and dependent variables, respectively. Here's the sample data we'll work with:
In this example, x represents the independent variable whose values are [1, 2, 3, 4, 5], and y is the dependent variable with values [2, 1, 4, 3, 5]. The aim is to model the relationship between x and y.
Let's visualize the regression line along with the data points using Matplotlib:
Here's what happens in this code:
- The
scatterfunction plots the data points. - The
plotfunction draws the regression line obtained from the linear regression analysis. - Labels and title are added for clarity.
Here is the result:

Now, let's use the famous Iris dataset to perform simple linear regression with real data:
In this example:
- We take the sepal length as the independent variable (
x) and the petal length as the dependent variable (y). - We apply the same process of calculating the regression using
scipy.stats.linregress.

The resulting plot shows the regression line.
In this lesson, you've learned how to perform simple linear regression using SciPy. You explored practical steps to compute the regression line and visualize the relationship between two variables. Understanding regression analysis is essential for data-driven decision-making and predictive modeling.
As you move on to the practice exercises, apply these concepts to solve real-world problems where regression can provide insights. Keep practicing to strengthen your understanding, and look forward to more advanced topics in your learning journey.
