Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/ML Regression Analysis/5.1 Exponential equation .ipynb
3074 views
Kernel: Python 3 (ipykernel)

Non-Linear Regression with Python

Non-linear regression is used when data shows a curved or non-linear relationship between input features and output. Unlike linear regression which fits straight lines, non-linear regression can model more complex patterns.


What is Non-Linear Regression?

A regression technique used to model the relationship between a dependent variable and one or more independent variables when the model equation is non-linear in the parameters.


Exponential Regression

Equation:

%7B97810339-0DA1-40CC-B48A-E5F0EE4D5A8D%7D.png

Where:

  • ( a ) = initial value

  • ( b ) = growth or decay rate

  • ( e ) = Euler’s number (~2.718)

Common Use Cases:

  • Population growth

  • Compound interest

  • Radioactive decay

import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit from sklearn.metrics import r2_score # Simulated exponential data np.random.seed(0) X = np.linspace(0, 4, 50) y = 2 * np.exp(1.5 * X) + np.random.normal(0, 25, size=X.shape) # Define exponential function def exp_func(x, a, b): return a * np.exp(b * x) # Fit model params, _ = curve_fit(exp_func, X, y, p0=(1, 1)) # p0: initial guess a, b = params y_pred = exp_func(X, a, b) # R² Score r2 = r2_score(y, y_pred) print(f"Fitted: a = {a:.2f}, b = {b:.2f}, R² = {r2:.4f}") # Plot plt.scatter(X, y, label='Observed') plt.plot(X, y_pred, color='red', label='Exponential Fit') plt.legend() plt.title("Exponential Regression") plt.grid(True) plt.show()
Fitted: a = 2.12, b = 1.48, R² = 0.9795
Image in a Jupyter notebook