Questions tagged [curve-fitting]

Fitting 1-D curve to data points, minimizing pre-defined error/loss function.

From wiki:

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points.

3428 questions
230
votes
7 answers

How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting

I have a set of data and I want to compare which line describes it best (polynomials of different orders, exponential or logarithmic). I use Python and Numpy and for polynomial fitting there is a function polyfit(). But I found no such functions for…
Tomas Novotny
  • 7,547
  • 9
  • 26
  • 23
138
votes
12 answers

How do I calculate r-squared using Python and Numpy?

I'm using Python and Numpy to calculate a best fit polynomial of arbitrary degree. I pass a list of x values, y values, and the degree of the polynomial I want to fit (linear, quadratic, etc.). This much works, but I also want to calculate r…
Travis Beale
  • 5,534
  • 7
  • 34
  • 34
113
votes
8 answers

Linear regression with matplotlib / numpy

I'm trying to generate a linear regression on a scatter plot I have generated, however my data is in list format, and all of the examples I can find of using polyfit require using arange. arange doesn't accept lists though. I have searched high and…
user771224
103
votes
7 answers

Fitting a density curve to a histogram in R

Is there a function in R that fits a curve to a histogram? Let's say you had the following histogram hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))) It looks normal, but it's skewed. I want to fit a normal curve…
boo-urns
  • 10,136
  • 26
  • 71
  • 107
100
votes
9 answers

How to fit a smooth curve to my data in R?

I'm trying to draw a smooth curve in R. I have the following simple toy data: > x [1] 1 2 3 4 5 6 7 8 9 10 > y [1] 2 4 6 8 7 12 14 16 18 20 Now when I plot it with a standard command it looks bumpy and edgy, of course: > plot(x,y,…
Frank
  • 64,140
  • 93
  • 237
  • 324
93
votes
5 answers

Fitting polynomial model to data in R

I've read the answers to this question and they are quite helpful, but I need help. I have an example data set in R as follows: x <- c(32,64,96,118,126,144,152.5,158) y <- c(99.5,104.8,108.5,100,86,64,35.3,15) I want to fit a model to these data…
Mehper C. Palavuzlar
  • 10,089
  • 23
  • 56
  • 69
86
votes
12 answers

How to apply piecewise linear fit in Python?

I am trying to fit piecewise linear fit as shown in fig.1 for a data set This figure was obtained by setting on the lines. I attempted to apply a piecewise linear fit using the code: from scipy import optimize import matplotlib.pyplot as plt import…
Tom Kurushingal
  • 6,086
  • 20
  • 54
  • 86
72
votes
11 answers

Android How to draw a smooth line following your finger

http://marakana.com/tutorials/android/2d-graphics-example.html I am using this example below. But when I move my fingers too fast across the screen the line turns to individual dots. I am not sure whether I can speed up the drawing. Or I should…
Somk
  • 11,869
  • 32
  • 97
  • 143
70
votes
3 answers

python numpy/scipy curve fitting

I have some points and I am trying to fit curve for this points. I know that there exist scipy.optimize.curve_fit function, but I do not understand the documentation, i.e. how to use this function. My points: np.array([(1, 1), (2, 4), (3, 1), (9,…
Bob
  • 10,427
  • 24
  • 63
  • 71
69
votes
5 answers

Python curve_fit with multiple independent variables

Python's curve_fit calculates the best-fit parameters for a function with a single independent variable, but is there a way, using curve_fit or something else, to fit for a function with multiple independent variables? For example: def func(x, y, a,…
ylangylang
  • 3,294
  • 11
  • 30
  • 34
61
votes
5 answers

Fitting a histogram with python

I have a histogram H=hist(my_data,bins=my_bin,histtype='step',color='r') I can see that the shape is almost gaussian but I would like to fit this histogram with a gaussian function and print the value of the mean and sigma I get. Can you help me?
Brian
  • 13,996
  • 19
  • 70
  • 94
59
votes
3 answers

fitting data with numpy

I have the following data: >>> x array([ 3.08, 3.1 , 3.12, 3.14, 3.16, 3.18, 3.2 , 3.22, 3.24, 3.26, 3.28, 3.3 , 3.32, 3.34, 3.36, 3.38, 3.4 , 3.42, 3.44, 3.46, 3.48, 3.5 , 3.52, 3.54, 3.56, 3.58, 3.6 , 3.62, …
ezitoc
  • 729
  • 1
  • 6
  • 9
50
votes
6 answers

How do I fit a sine curve to my data with pylab and numpy?

I am trying to show that economies follow a relatively sinusoidal growth pattern. I am building a python simulation to show that even when we let some degree of randomness take hold, we can still produce something relatively sinusoidal. I am happy…
ChapmIndustries
  • 1,401
  • 4
  • 17
  • 19
50
votes
4 answers

Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000

I want to make an logharitmic fit. But I keep getting the a runtime error: Optimal parameters not found: Number of calls to function has reached maxfev = 1000 I use the following script. Can anyone tell me where I go wrong? I use Spyder an am…
Tjitze
  • 501
  • 1
  • 4
  • 3
44
votes
7 answers

Fitting only one parameter of a function with many parameters in python

In python I have a function which has many parameters. I want to fit this function to a data set, but using only one parameter, the rest of the parameters I want to supply on on my own. Here is an example: def func(x,a,b): return a*x*x + b for b…
lovespeed
  • 4,835
  • 15
  • 41
  • 54
1
2 3
99 100