[파이썬] scipy 커브 피팅

In data analysis and scientific computing, curve fitting is a commonly used technique to model and approximate data points with a mathematical function. The Scipy library in Python provides a powerful set of functions for curve fitting, allowing you to find the best-fit parameters and assess the quality of the fit. In this blog post, we will explore how to perform curve fitting using Scipy in Python.

Prerequisites

To follow along with the examples in this blog post, make sure you have Scipy installed. If you don’t have it installed, you can install it using the following command:

pip install scipy

Importing the necessary libraries

To get started, we need to import the required libraries. We will be using the curve_fit function from the Scipy library for curve fitting.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

Generating sample data

Let’s start by generating some sample data that we will use for curve fitting. We will generate data points from a known mathematical function and add some random noise to it to make it more realistic.

# Define the mathematical function
def func(x, a, b, c):
    return a * np.exp(-b * x) + c

# Create the x-axis values
x = np.linspace(0, 10, 100)

# Generate y values from the function with some random noise
y = func(x, 2.5, 1.3, 0.5)
y_noise = 0.2 * np.random.normal(size=x.size)
y_with_noise = y + y_noise

Performing the curve fitting

Now that we have our sample data, we can proceed with the curve fitting. We will use the curve_fit function from Scipy to fit our data to the defined mathematical function.

# Perform the curve fitting
params, cov = curve_fit(func, x, y_with_noise)
a_fit, b_fit, c_fit = params

The curve_fit function takes three arguments: the function we want to fit (func in this case), the x values, and the y values with noise. It returns the optimized parameters (params) and the covariance matrix (cov).

Visualizing the fit

To visualize the fit, we can plot the original data points along with the fitted curve.

# Plot the original data points
plt.scatter(x, y_with_noise, label='Original Data')

# Plot the fitted curve
plt.plot(x, func(x, a_fit, b_fit, c_fit), 'r-', label='Fitted Curve')

# Set labels and legends
plt.xlabel('x')
plt.ylabel('y')
plt.legend()

# Display the plot
plt.show()

Assessing the quality of the fit

In addition to visualizing the fit, it is also important to assess the quality of the fit. Scipy provides the cov output from the curve_fit function, which can be used to calculate the standard errors and confidence intervals of the fitted parameters. This information gives an estimate of the uncertainty in the fitted values.

Moreover, you can also evaluate the goodness of fit by calculating the residuals and a measure of the fit’s quality, such as the chi-squared statistic or the coefficient of determination (R-squared).

Conclusion

Curve fitting is a useful technique in scientific computing and data analysis. In this blog post, we explored how to perform curve fitting using the Scipy library in Python. We covered the steps involved, from generating sample data to performing the curve fitting using the curve_fit function. We also visualized the fit and discussed how to assess the quality of the fit.

Scipy provides a wide range of optimization functions and statistical tools for curve fitting, allowing you to handle various types of data and functions. Experiment with different functions and datasets to gain a better understanding of curve fitting and its applications.

Happy curve fitting with Scipy in Python!