0
$\begingroup$

I'm trying to fit a dataset to a curve for while, but I'm not managing. The goal is to obtain a curve with equation that fits the data so I can get the parameter x to any value of y. The blue dataset is the measurement of the soil parameter at the field, and the orange dataset is the measurement of the parameter at the lab. enter image description here

I already have limited the min and max value for values that make sense in real life, and I have applied the outlier cleaner for the quartile of 0.25/0.75

def remove_outlier(df_in, col_name):
    q1 = df_in[col_name].quantile(0.25)
    q3 = df_in[col_name].quantile(0.75)
    iqr = q3-q1 #Interquartile range
    fence_low  = q1-1.5*iqr
    fence_high = q3+1.5*iqr
    df_out = df_in.loc[(df_in[col_name] > fence_low) & (df_in[col_name] < fence_high)]
    return df_out

and when I try to fit the data with scipy:

def fit_ocr(x,a,b):
    return a/x + b
param, param_cov = curve_fit(fit_ocr, x, y)
a = param[0]
b = param[1]
test_fit = fit_ocr(x,a,b)

That's what I get:

enter image description here

$\endgroup$
7
  • 2
    $\begingroup$ The usual least squares method for fitting a curve requires to define one variable as predictor and one as response. This does not work with the red "trendline", because whatever variable you choose as predictor, there is no unique response value for a given predictor. Are you instead looking for surface fitting, e.g. moving least squares? $\endgroup$ Commented Jan 24, 2022 at 9:49
  • $\begingroup$ Yes! probably this is what I need, could you give me some more information, please? $\endgroup$ Commented Jan 24, 2022 at 10:01
  • $\begingroup$ This is just a restatement of your original question. Please clarify the original by editing it rather than reposting a new version of your question. $\endgroup$ Commented Jan 24, 2022 at 15:00
  • $\begingroup$ Jennifer wrote: "could you give me some more information". As the question is already closed, here some suggesions instead of a proper answer. Most work on surface reconstruction is for 3D data. For 2D data, there is an implementation in CGAL doc.cgal.org/latest/Advancing_front_surface_reconstruction/…. This will result in a polygon of the surface which you then can further process, e.g. by splie fitting or local orthogonal least squares through the points close to the surface. $\endgroup$ Commented Jan 27, 2022 at 12:58
  • $\begingroup$ @cdalitz thank you for the suggestion, I have done some research, and is not what I need, I have handled it manually. Thank you $\endgroup$ Commented Jan 28, 2022 at 11:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.