curve-fitting

SciPy Curve Fit Fails Power Law

淺唱寂寞╮ 提交于 2019-11-29 07:14:11
So, I'm trying to fit a set of data with a power law of the following kind: def f(x,N,a): # Power law fit if a >0: return N*x**(-a) else: return 10.**300 par,cov = scipy.optimize.curve_fit(f,data,time,array([10**(-7),1.2])) where the else condition is just to force a to be positive. Using scipy.optimize.curve_fit yields an awful fit (green line) , returning values of 1.2e+04 and 1.9e0-7 for N and a, respectively, with absolutely no intersection with the data. From fits I've put in manually, the values should land around 1e-07 and 1.2 for N and a, respectively, though putting those into curve

How to fit the 2D scatter data with a line with C++

╄→гoц情女王★ 提交于 2019-11-29 06:57:37
I used to work with MATLAB, and for the question I raised I can use p = polyfit(x,y,1) to estimate the best fit line for the scatter data in a plate. I was wondering which resources I can rely on to implement the line fitting algorithm with C++. I understand there are a lot of algorithms for this subject, and for me I expect the algorithm should be fast and meantime it can obtain the comparable accuracy of polyfit function in MATLAB. I would suggest coding it from scratch. It is a very simple implementation in C++. You can code up both the intercept and gradient for least-squares fit (the same

Fit a curve to the boundary of a scatterplot

*爱你&永不变心* 提交于 2019-11-29 04:19:22
I'm trying to fit a curve to the boundary of a scatterplot. See this image for reference . I have accomplished a fit already with the following (simplified) code. It slices the dataframe into little vertical strips, and then finds the minimum value in those strips of width width , ignoring nan s. (The function is monotonically decreasing.) def func(val): """ returns some function of 'val'""" return val * 2 for i in range(0, max_val, width)): _df = df[(df.val > i) & (df.val < i + width)] # vertical slice if np.isnan(np.min(func(_df.val)): # ignore nans continue xs.append(i + width) ys.append(np

Fit a curve for data made up of two distinct regimes

萝らか妹 提交于 2019-11-29 03:46:09
I'm looking for a way to plot a curve through some experimental data. The data shows a small linear regime with a shallow gradient, followed by a steep linear regime after a threshold value. My data is here: http://pastebin.com/H4NSbxqr I could fit the data with two lines relatively easily, but I'd like to fit with a continuous line ideally - which should look like two lines with a smooth curve joining them around the threshold (~5000 in the data, shown above). I attempted this using scipy.optimize curve_fit and trying a function which included the sum of a straight line and an exponential: y

Weighted trendline

旧街凉风 提交于 2019-11-29 02:04:00
Excel produces scatter diagrams for sets of pair values. It also gives the option of producing a best fit trendline and formula for the trendline. It also produces bubble diagrams which take into consideration a weight provided with each value. However, the weight has no influence on the trendline or formula. Here is an example set of values, with their mappings and weights. Value Map Weight 0 1 10 1 2 10 2 5 10 3 5 20 4 6 20 5 1 1 With Excel's trendline, the mapping for value 5 has too much influence on the formula. Is there any way to produce a formula that reflects the respective weights?

6th degree curve fitting with numpy/scipy

血红的双手。 提交于 2019-11-29 01:43:15
问题 I have a very specific requirement for interpolating nonlinear data using a 6th degree polynomial. I've seen numpy/scipy routines (scipy.interpolate.InterpolatedUnivariateSpline) that allow interpolation only up to degree 5. Even if there's no direct function to do this, is there a way to replicate Excel's LINEST linear regression algorithm in Python? LINEST allows 6th degree curve-fitting but I do NOT want to use Excel for anything as this calculation is part of a much larger Python script.

Optimize constants in differential equations in Python

别等时光非礼了梦想. 提交于 2019-11-29 00:43:59
Okay so how would i approach to writing a code to optimize the constants a and b in a differential equation, like dy/dt = a*y^2 + b, using curve_fit? I would be using odeint to solve the ODE and then curve_fit to optimize a and b. If you could please provide input on this situation i would greatly appreciate it! You might be better served by looking at ODEs with Sympy . Scipy/Numpy are fundamentally numerical packages and aren't really set up to do algebraic/symbolic operations. You definitely can do this: import numpy as np from scipy.integrate import odeint from scipy.optimize import curve

Finding a curve to match data

╄→гoц情女王★ 提交于 2019-11-28 18:58:06
I'm looking for a non-linear curve fitting routine (probably most likely to be found in R or Python, but I'm open to other languages) which would take x,y data and fit a curve to it. I should be able to specify as a string the type of expression I want to fit. Examples: "A+B*x+C*x*x" "(A+B*x+C*x*x)/(D*x+E*x*x)" "sin(A+B*x)*exp(C+D*x)+E+F*x" What I would get out of this is at least the values for the constants (A, B, C, etc.) And hopefully stats about the fitness of the match. There are commercial programs to do this, but I expected to be able to find something as common as fitting to a desired

Why does scipy.optimize.curve_fit not fit to the data?

二次信任 提交于 2019-11-28 18:21:05
I've been trying to fit an exponential to some data for a while using scipy.optimize.curve_fit but i'm having real difficulty. I really can't see any reason why this wouldn't work but it just produces a strait line, no idea why! Any help would be much appreciated from __future__ import division import numpy from scipy.optimize import curve_fit import matplotlib.pyplot as pyplot def func(x,a,b,c): return a*numpy.exp(-b*x)-c yData = numpy.load('yData.npy') xData = numpy.load('xData.npy') trialX = numpy.linspace(xData[0],xData[-1],1000) # Fit a polynomial fitted = numpy.polyfit(xData, yData, 10)[

Goodness of fit functions in R

梦想与她 提交于 2019-11-28 15:23:12
问题 What functions do you use in R to fit a curve to your data and test how well that curve fits? What results are considered good? 回答1: Just the first part of that question can fill entire books. Just some quick choices: lm() for standard linear models glm() for generalised linear models (eg for logistic regression) rlm() from package MASS for robust linear models lmrob() from package robustbase for robust linear models loess() for non-linear / non-parametric models Then there are domain