# How to run a regression analysis in python on a nonlinear, strange dataset

I can't figure out what type of regression analysis or extrapolation technique to use in order to come up with an equation for the data I have plotted.

For a school project, I've been testing diodes by connecting them to a temperature switch that shuts off the circuit once the temperature reaches around 250 degrees Celsius. I plotted the data, and I wanted to extrapolate it and get an equation that I could use to predict future diode temperatures. However, I have tried all sorts of extrapolation techniques and none of them accurately fits the data. For example

extrapolation1

as well as this:

extrapolation2

I've tried extrapolation/regressions in both R Studio and python, but I simply cannot get an accurate enough equation for either. Is there anything I could improve in my code, or maybe is there another technique I could use?

This is the code I used to get the first graph:

import numpy as np
import pandas as pd
from scipy.optimize import curve_fit

df = pd.read_csv("JB WELD DIODE REG.csv")
def func(x, a, b, c,d):
return a*np.sin(b*x-c)+d
x = (df.time)/10**2
y= (df.diode)/10**2

popt, pcov = curve_fit(func, x, y)
popt

plt.plot(x, func(x, *popt), 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f, d=%5.3f' %tuple(popt)
)
plt.plot(x, y)

• If you post a link to the data file, I can run the data through my Python open source "function finder" at zunzun.com and see what candidate equations it suggests. It has hundreds of known, named equations to search through. – James Phillips Dec 26 '18 at 11:20
• What's on the X axis? Time? – HakunaMaData Dec 26 '18 at 12:55
• Why doesn't the temperature fall all the way to room temperature.. is that because you're not letting the diode cool down? Also, Why did the temperature start from close to zero in the beginning? It is important as you could model this as noise and ignore parts of the curves. – HakunaMaData Dec 26 '18 at 13:02
• Instead of treating this as a black box regression problem, it would be more informative to model the underlying physical system--resistive heating, heat transfer from the diode to the surrounding air, etc. – user20160 Oct 14 '19 at 14:38

There are clearly two different segments to each curve. I suggest fitting each separately.

While it is heating up, it looks exponential - see whether log(250-t) is linear.

While it is cooling down, it looks quadratic, so I suggest seeing whether sqrt(t-75) is linear.

• Interesting thoughts! How would you write such a model? – Dave Oct 14 '19 at 14:23
• First, implement functions is_max(t) and is_min(t). Use these to segment the time interval into periods when it is increasing, and periods when it is decreasing. Then rebase each period to start at t=0, and plot them. To the eye it looks like you could validly average all the periods of decrease to produce a consensus curve, and all the periods of increase except the first. Then the task is to model each consensus curve. – chrishmorris Oct 16 '19 at 6:44
• If this is right, I then suggest plotting dy/dt against t for each consensus curve. This drops out the additive constant, and I think will make the right model obvious. – chrishmorris Oct 16 '19 at 6:52