from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
#power function fitting
xdata = [2,3,4,5,6,7]
ydata = [2400,5300,8000,9700,10700,11200]
plt.plot(xdata,ydata,'b-')
###define a fitting function, y = a * x^b ###
def target_func(x, a, b):
return a * (x ** b)
###using fitting functions to obtain eigenvalues ###
popt, pcov = curve_fit(target_func, xdata, ydata)
### R ^2 calculations ###
calc_ydata = [target_func(i, popt[0], popt[1]) for i in xdata]
res_ydata = np.array(ydata) - np.array(calc_ydata)
ss_res = np.sum(res_ydata ** 2)
ss_tot = np.sum((ydata - np.mean(ydata)) ** 2)
r_squared = 1 - (ss_res / ss_tot)
#fitted values
y = [target_func(i,popt[0],popt[1]) for i in xdata]
plt.plot(xdata,y,'r--')
plt.show()
###output results ###
print("a = %f b = %f R2 = %f" % (popt[0], popt[1], r_squared))
print(ydata, calc_ydata)
Related articles
- Record: easycr Error: Could not initialize NNPACK reason Unsupported hardware causing Python program to exit abnormally
- Streamlit installation error resolution
- Matplotlib ImportError: DLL load failed: Unable to find specified module
- Python: can't open file '
': [Errno 22] Invalid argument - Python dictionary application
- Cv2. error: OpenCV (4.5.5) D: a opencv Python opencv Python opencv modules imgproc src imgwarp. cpp: 66
- Python drawing based on f (x) (2D)
- The interval for drawing a histogram in Python must be a saturation integral interval
- PLT.bar Drawing Rules - Taking Drawing Histograms as an Example
- Installed numpy but reported numpy.core.multiarray failed to import
- Regarding the issue of using sqlite to create database tables in PyCharm and not displaying the tables
- After successful installation of Python, the terminal still displays the lower version
- Usage of astype(), iloc, and loc
- Installation of Python's docx library
- Seaborne has installed the correct version, but module 'seaborne' has no attribute appears
- Iloc operates on DataFrame, converting DataFrame and Series to a list, with the maximum and second largest values, and removing punctuation at the end of the string
- AttributeError: module 'pandas' has no attribute' Series'
- Installing pip and PyMySQL in Centos Python 2.7.5 offline environment
- Develop SDK using Python and package Python projects
- The projects of django and celery, nginx and uwsgi protocols, train and queue deep learning tasks through API ports
Latest articles
- Ubuntu decompresses the tar.gz format file
- Ubuntu's XDMCP login to ubuntu desktop crashes
- RunTime Error: cuda out of memory
- Ffmpeg converts a frame from yuv444 format to png format
- Notepad++Theme Change, Highlighting Selected Content
- Record: easycr Error: Could not initialize NNPACK reason Unsupported hardware causing Python program to exit abnormally
- Streamlit installation error resolution
- Resolving Warning for Ignoring Invalid Distribution
- Matplotlib ImportError: DLL load failed: Unable to find specified module
- Python: can't open file '
': [Errno 22] Invalid argument - Executable NotFound: failed to execute 'dot', make sure the Graphviz executors are on your systems'
- Python dictionary application
- Cv2. error: OpenCV (4.5.5) D: a opencv Python opencv Python opencv modules imgproc src imgwarp. cpp: 66
- Python drawing based on f (x) (2D)
- The interval for drawing a histogram in Python must be a saturation integral interval
- PLT.bar Drawing Rules - Taking Drawing Histograms as an Example
- Sort and remove duplicate numbers in C language
- Summary of pedestrian re identification paper
- Unable to boot up when memory is full (Ubuntu)
- Java Spark code error: java. lang. NoSuchMethodError: net. jpuntz. lz4. LZ4BlockInputStream
Hot tags:
java python linux javascript using error android vue.js solution mysql data vue file c++ windows installation configuration c ubuntu method implementation between spring function project spring-boot server system sql database issue c# springboot html docker oracle idea environment command version methods image process resolve uses network excel failed time display