簡體   English   中英

python excel中的曲線擬合

[英]Curve fitting in python excel

我堅持從 p0 開始的初始猜測來設置擬合並生成 a、b 和 c 的猜測。 我需要為 excel 的低 RMSD 的給定數據集創建最佳曲線擬合。 我不確定我是否需要創建一個新的 p0,即使它在一開始就已經給了我一個。 這是我到目前為止所擁有的:

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

# USAGE:  fitting.py input a b c
# Example : fitting.py mono.csv 1 -1 1
# input is a comma-separated csv file
# a, b, c are the initial guesses for fitting variables a, b, and c

infile = sys.argv[1]
p0 = [float(sys.argv[2]),float(sys.argv[3]),float(sys.argv[4])]

def readDATA(file):
    f = open(file)
    data = f.readlines()
    f.close()
    x,y = [], []
    for i in data:
        x.append(int(i.split(',')[0]))
        y.append(float(i.split(',')[1]))
    return np.array(x), np.array(y)


def monoEXP(x, a, b, c):
    return a * np.exp(b*x) + c

# Returns the RMSD           
# y1 and y2 are two arrays of the same length         
def calcRMSD(y1,y2):
    return (  np.sqrt(np.sum((y1 - y2)**2) / len(y1)) )
    

p0 = [1,1,1]
popt =p0
rmsd = 100000000000000000
for i in range(1000):
  list2 = [random.gauss(popt[0],0.1*popt[0]), random.gauss(popt[1],0.1*popt[1]), random.gauss(popt[2],0.1*popt[2])]
  #print(list2)
  
  
  if calcRMSD(np.array(x), np.array(y)) < rmsd:
  #rmsd = calcRMSD(np.array(list1), np.array(list2))
    #print(rmsd)
    rmsd = calcRMSD(np.array(x), np.array(y))    #overwrite the original rmsd with the new rmsd
    #print(popt)
    popt = list2    #stores the best results of the list with the rmsd
    
print(popt)
if rmsd < 0.01:
      #print(list1)
      #print(list2)
  print(f"RMSD: {np.round(rmsd,3)}")
# This function does the fitting
# Start by using the initial guesses from p0 to set up the fitting
# use a for or while loop to iterate:
#    - Generate guesses for a, b, and c.
#    - use the RMSD to evaluate if this current guess is better than the previous one
#    - if the current guess is the best so far, 'save' the guess, otherwise discard the guess
#    - make sure your RMSD is getting smaller with each SAVED guess
#    - the parameter steps is the MAX number of steps performed. Find a way to break out of the loop when the RMSD is "stable"
#    - this function returns two variables, popt and rmsd. popt is an array of the fitting result in the order a, b, anc c. rmsd is the RMSD that was achieved in the end**
def fitDATA(x,y,p0,steps):        #important
    return

# readDATA returns the x and y values as arrays
x_data, y_data = readDATA(infile)

# determine the fitted parameters
# fitDATA has 4 parameters: x_data, y_data, initial guess p0, and maximum number of steps
popt, rmsd = fitDATA(x_data, y_data, p0, 1_000_000)    #popt needs to be array


# Output of fitting result
print(f'Fitting Results:\n   a = {np.round(popt[0],2)}\n   b = {np.round(popt[1],2)}\n   c = {np.round(popt[2],2)}\n   RMSD: {np.round(rmsd,3)}  ')

# Plotting of data and fit
plt.plot(x_data, y_data, 'bo', label='data')
plt.plot(x_data, monoEXP(x_data, *popt), 'r-', label=f'fit: a={np.round(popt[0],2)}, b={np.round(popt[1],2)}, c={np.round(popt[2],2)}   ')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

無需猜測的擬合:

在此處輸入圖像描述

您可以使用上面的演算來為您的初始猜測獲得好的值。

上述方法的理論在: https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM