Regression Analysis with SciPy
Last Updated : 23 Jun, 2025
Regression analysis is a statistical technique used to examine the relationship between one or more independent variables and a dependent variable. It helps in modelling and analyzing data where the focus is on the relationship between variables. In Python SciPy is a popular library for statistical analysis which offers simple and efficient tools for performing regression using its stats module.
Regression AnalysisRegression Analysis
Regression analysis is a statistical technique used to test the relationship between a dependent variable and one or more independent variables. It helps in understanding how the dependent variable changes when any one of the independent variables is varied while the others are held fixed. Regression analysis is commonly used for prediction, forecasting and determining causal relationships between variables.
1. Using SciPy for Linear Regression
Linear Regression models a straight line relationship between one independent and one dependent variable:
y = mx+c
- This code performs Linear regression using SciPy’s linregress() on sample x and y data.
- It calculates the slope, intercept and R squared value to describe the line of best fit.
- Finally, it visualizes the original data points and the fitted regression line using Matplotlib.
Python import numpy as np from scipy import stats import matplotlib.pyplot as plt # Sample data x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) # Perform linear regression slope, intercept, r_value, p_value, std_err = stats.linregress(x, y) print(f"Slope: {slope}") print(f"Intercept: {intercept}") print(f"R-squared: {r_value**2}") plt.scatter(x, y, color='blue', label='Data') plt.plot(x, slope * x + intercept, color='red', label='Regression Line') plt.legend() plt.xlabel("X") plt.ylabel("Y") plt.title("Linear Regression with SciPy") plt.show()
Output:
Output for Linear Regression2. Using SciPy for Exponential Regression
Exponential Regression is used when data shows exponential growth or decay modelled as:
y = a \cdot e^{bx}
- This code performs Exponential regression using curve_fit from SciPy.
- It fits the data to the model and finds the best values for a and b. The result is plotted by showing both the original data points and the fitted exponential curve.
Python import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt #Sample data x = np.array([0, 1, 2, 3, 4]) y = np.array([1, 2.7, 7.4, 20.1, 54.6]) # Define the exponential function def exp_func(x, a, b): return a * np.exp(b * x) # Fit curve params, _ = curve_fit(exp_func, x, y) a, b = params print(f"a = {a}, b = {b}") # Plot x_line = np.linspace(min(x), max(x), 100) plt.scatter(x, y) plt.plot(x_line, exp_func(x_line, *params), color='green') plt.show()
Output:
Output for Exponential Regression
3. Using SciPy for Logarithmic Regression
Logarithmic Regression models relationships where the dependent variable changes proportionally to the logarithm of the independent variable:
y = a + b \ln(x)
- This code fits a Logarithmic regression model to the data using curve_fit. It estimates the parameters a and b that best describe the relationship between x and y.
- The plot visualizes the original data points alongside the fitted logarithmic curve.
Python import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt x = np.array([1, 2, 3, 4, 5]) y = np.array([2.3, 3.1, 3.8, 4.4, 5.0]) def log_func(x, a, b): return a + b * np.log(x) params, _ = curve_fit(log_func, x, y) print(f"a = {params[0]}, b = {params[1]}") # Plot x_line = np.linspace(min(x), max(x), 100) plt.scatter(x, y) plt.plot(x_line, log_func(x_line, *params), color='purple') plt.show()
Output:
Output for Logarithmic Regression
4. Using SciPy for Logistic Regression
Logistic Regression models S shaped curves often used for classification or growth processes:
y = \frac{L}{1 + e^{-k(x - x_0)}}
- This code fits a Logistic regression curve to the data, modelling an S shaped growth using the logistic function.
- It estimates parameters for curve height (L), growth rate (k) and midpoint (x0) using curve_fit. The plot shows the noisy data points and the smooth fitted logistic curve.
Python import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt x = np.linspace(0, 10, 100) y = 1 / (1 + np.exp(-1.5*(x - 5))) + 0.05 * np.random.normal(size=len(x)) def logistic(x, L, k, x0): return L / (1 + np.exp(-k*(x - x0))) params, _ = curve_fit(logistic, x, y, p0=[1, 1, 1]) print(f"L = {params[0]}, k = {params[1]}, x0 = {params[2]}") plt.scatter(x, y, s=10) plt.plot(x, logistic(x, *params), color='red') plt.title("Logistic Regression") plt.show()
Output:
Output for Logistic Regression
Similar Reads
What is Regression Analysis? In this article, we discuss about regression analysis, types of regression analysis, its applications, advantages, and disadvantages.What is regression?Regression Analysis is a supervised learning analysis where supervised learning is the analyzing or predicting the data based on the previously avai
15+ min read
Multiple Linear Regression With scikit-learn In this article, let's learn about multiple linear regression using scikit-learn in the Python programming language. Regression is a statistical method for determining the relationship between features and an outcome variable or result. Machine learning, it's utilized as a method for predictive mode
11 min read
Orthogonal distance regression using SciPy Regression basically involves determining the relationship between a dependent variable and one or more independent variables. It generally involves finding the best fit line that minimizes the sum of squared errors for each point. Based on the implementation procedures, regression algorithms are cl
4 min read
K-Nearest Neighbors (KNN) Regression with Scikit-Learn K-Nearest Neighbors (KNN) is one of the simplest and most intuitive machine learning algorithms. While it is commonly associated with classification tasks, KNN can also be used for regression. This article will delve into the fundamentals of KNN regression, how it works, and how to implement it usin
7 min read
Data Analysis with Python Data Analysis is the technique of collecting, transforming and organizing data to make future predictions and informed data-driven decisions. It also helps to find possible solutions for a business problem. In this article, we will discuss how to do data analysis with Python i.e. analyzing numerical
15+ min read