# Using curve_fit for Non-Linear, Multi-Variate Models [Python] [closed]

Warning: ML Noob.

I have a 3D dataset (data at the bottom) with 2 feature variables and 1 target variable. Polynomial Regression produced unsatisfactory results and it seems that the relationship of my data is more logarithmic in nature.

My goal is to use the curve_fit() function, or similar, to understand how different combinations of my features relate to the target variable. I'm running into a few issues

Main Questions:

• Given my example data below: Is scipy.optimize.curve_fit() the correct tool for this job? If not, what are some non-linear, multivariate alternatives that may work for my dataset?
• Why does the following code produce an array of inf covariance terms? I'm getting this error: 'Covariance of the parameters could not be estimated'
• Why do the popt values match my predictions exactly? This is an optional term according to the scipy reference docs, but removing them produces an array of ones. Would love to know what I'm doing wrong

Code

def func(X, a, b, c):
x,y = X
return a + b*np.log(x) + c*np.log(y)

x = mdot_true  # Feature 1
y = temp_true  # Feature 2
X = x,y
z = vout_true  # Target

p0 = 1.5, 0.5, 0.1  # Initial guesses for a, b, c
print(curve_fit(func, (x, y), z))


The Dataset

Primarily focusing on the left-hand chart. This is a limited dataset, but each of the curves correspond with the "slices" from the righthand chart.

Data

x = [-0.61108422 -0.642349   -0.67787715 -0.56134481 -0.66224476 -0.5116054
-0.81998976 -0.71766868  2.49833953  2.48981277  2.48981277  2.50118178
2.50970853  2.52534092  5.05636634  5.0648931   5.05778747  5.05636634
5.07199873  5.05636634  7.60444528  7.61723541  7.62576217  7.62007767
7.61297204  7.62434104 10.20936926 10.17526223 10.17099885 10.19657912
10.195158   10.2008425  12.72049892 12.70628766 12.71907779 12.74039468
12.72334117 12.69918203 15.29700037 15.30126375 15.31263276 15.2984215
15.29415812 15.31263276 17.89623984 17.86071169 17.85218494 17.89623984
17.86071169 17.88060746 20.42300189 20.42726527 20.44858216 20.42300189
20.42868639 20.41447513 23.0080301  23.02792587 22.99666109 22.99097659
22.9753442  22.98671321 25.54189778 25.55610904 25.54900341 25.54900341
25.54758228 25.54758228 28.1112936  28.09566122 28.11271473 28.09281897
28.10134572 28.10845135 30.68068943 30.66363592 30.68068943 30.69774294
30.66363592 30.68495281 33.23303174 33.21739936 33.23445287 33.21455711
33.24440075 33.20603035 35.76405717 35.77826843 35.78537406 35.79816419
35.78821631 35.78821631 38.344822   38.34055862 38.34055862 38.33771637
38.31497836 38.34197975 40.92700796 40.89432206 40.90426995 40.89574319
40.90853332 40.91137558 43.46798127 43.46087564 43.45945451 43.45945451
43.42250524 43.47650802 46.03595597 46.02885034 46.05158835 46.03453484
46.00327007 46.02032358 48.60677292 48.5840349  48.58687716 48.6167208
48.59824616 48.6167208  51.1463251  51.15058848 51.17332649 51.16906312
51.13353497 51.11079695 53.7199843  53.72282656 53.70293079 53.72708993
53.71714205 53.6816139  56.23395622 56.25669423 56.26806324 56.27516887
56.27090549 56.26806324 58.8559337  58.8559337  58.84030132 58.82040555
58.83888019 58.8175633  61.42532953 61.35427323 61.39264363 61.40117039
61.44522529 61.40827602 63.94924932 63.95777608 63.92366906 63.95493383
63.96061833 63.93645919 66.50585502 66.54422542 66.53427754 66.50443389
66.46037898 66.48738038 69.02124805 69.05393395 69.10651561 69.03688044
69.09088323 69.06530296 71.67022694 71.63043541 71.65033117 71.66312131
71.67164806 71.59490726 74.21120024 74.20125236 74.23109601 74.16146083
74.17993547 74.21262137 76.77064819 76.7877017  76.72659328 76.74648904
76.76354256 76.79907071]

y = [14.91958618 20.0328064  20.05026245 20.1026001  14.93704224 14.91958618
14.97192383 20.0328064  14.91958618 20.08514404 20.06768799 14.93704224
19.99789429 14.90213013 20.05026245 14.93704224 20.13751221 20.01535034
14.93704224 14.97192383 20.08514404 14.95449829 14.93704224 19.99789429
14.93704224 20.15496826 20.15496826 19.98043823 19.99789429 14.93704224
14.91958618 14.93704224 20.0328064  20.05026245 14.93704224 14.95449829
14.91958618 20.12005615 20.13751221 14.93704224 14.93704224 20.01535034
19.99789429 14.95449829 20.05026245 20.15496826 14.97192383 14.93704224
20.01535034 14.86721802 14.88467407 20.01535034 14.93704224 14.93704224
19.98043823 20.12005615 14.97192383 14.93704224 20.0328064  14.93704224
19.98043823 20.12005615 14.93704224 20.01535034 20.05026245 20.13751221
14.90213013 14.95449829 20.13751221 19.99789429 14.88467407 20.01535034
14.93704224 14.95449829 14.91958618 20.01535034 20.0328064  20.15496826
14.90213013 14.95449829 14.90213013 14.93704224 20.0328064  19.96298218
14.88467407 20.13751221 14.91958618 20.15496826 19.99789429 14.91958618
19.98043823 14.90213013 14.93704224 20.15496826 19.99789429 14.90213013
14.91958618 20.0328064  14.90213013 20.01535034 14.93704224 20.13751221
14.93704224 19.98043823 14.88467407 14.91958618 20.1026001  20.0328064
20.01535034 14.91958618 20.01535034 14.93704224 20.1026001  14.88467407
14.88467407 19.99789429 14.91958618 20.01535034 20.01535034 20.08514404
14.90213013 14.93704224 19.99789429 20.1026001  14.84979248 14.90213013
14.91958618 20.01535034 14.93704224 14.90213013 14.93704224 19.98043823
20.1026001  19.99789429 19.98043823 14.93704224 14.91958618 20.01535034
20.06768799 14.90213013 19.98043823 14.90213013 20.01535034 14.93704224
14.93704224 20.1026001  19.98043823 19.99789429 14.93704224 14.91958618
14.88467407 20.1026001  14.90213013 19.92810059 19.96298218 14.95449829
14.93704224 20.05026245 19.98043823 14.93704224 14.97192383 14.93704224
19.98043823 20.06768799 19.99789429 19.99789429 14.93704224 20.01535034
14.93704224 14.97192383 19.99789429 20.01535034 14.97192383 14.88467407
14.95449829 20.05026245 14.97192383 14.95449829 14.97192383 20.06768799
19.99789429 19.99789429 20.01535034 14.91958618 20.05026245 20.01535034
14.97192383 14.98937988]

z = [2.15999084 2.13611048 2.14076448 2.16021973 2.14366369 2.16670481
2.12268254 2.13717861 2.37712673 2.37300679 2.37056535 2.37506676
2.37041276 2.38567178 2.53635462 2.54726482 2.53597314 2.53231098
2.5460441  2.54741741 2.65957122 2.67742428 2.67566949 2.6598764
2.67612726 2.66086824 2.76012818 2.75692378 2.75791562 2.77866789
2.77950713 2.77920195 2.83329519 2.83489738 2.85984588 2.85938811
2.8564126  2.83489738 2.90180819 2.92675669 2.9282826  2.90188449
2.90402075 2.92874037 2.95590143 2.95765621 2.98313878 2.98397803
2.95407034 2.98336767 3.0327306  3.00373846 3.03219654 3.03219654
3.00167849 3.0047303  3.07499809 3.07698177 3.04440375 3.07606622
3.04280156 3.04356451 3.11146715 3.07904173 3.0794995  3.08003357
3.11253529 3.11276417 3.1123064  3.11055161 3.1451133  3.1107042
3.1451133  3.14526589 3.17441062 3.13977264 3.13900969 3.140383
3.17433433 3.17357137 3.20111391 3.19859617 3.16548409 3.1638056
3.20103761 3.16632334 3.22301061 3.18898299 3.1872282  3.22499428
3.18783856 3.22423133 3.24620432 3.20889601 3.20714122 3.24612802
3.2449073  3.20798047 3.26504921 3.22735943 3.26413367 3.22659648
3.26535439 3.22690166 3.2824445  3.28183413 3.24429694 3.24437324
3.24460212 3.28320745 3.26131075 3.29831388 3.2604715  3.30052644
3.29892424 3.26001373 3.31418326 3.2745098  3.27443351 3.27557794
3.31464103 3.31395438 3.28915846 3.28969253 3.32845045 3.32936599
3.32982376 3.28915846 3.34187839 3.34203098 3.34195468 3.30128939
3.30174716 3.30182345 3.31410697 3.35347524 3.35423819 3.31311513
3.31433585 3.35461967 3.32593271 3.36751354 3.3245594  3.36583505
3.36606394 3.32509346 3.33569848 3.33646143 3.37796597 3.37819486
3.37842374 3.33653773 3.38925765 3.34653239 3.34721904 3.38773175
3.38925765 3.34851606 3.35690852 3.39841306 3.40024414 3.39993896
3.35690852 3.35835813 3.36842908 3.3666743  3.41039139 3.36804761
3.41077287 3.40985733 3.37727932 3.37743191 3.41947051 3.42183566
3.42069123 3.37827115 3.43022812 3.43091478 3.43137255 3.38742657
3.38727398 3.38704509 3.3962768  3.44068055 3.39681086 3.39711604
3.43976501 3.44197757]

• x has negative values, so you can't calculate log(x) Commented Jun 14, 2022 at 12:43
• Ah, duh. Fantastic sleuthing, thank you @gunes Commented Jun 14, 2022 at 17:05