I'm a newbie in python and machine learning. I'm trying to use gradient descent method(linear regression maybe) to get a slope from temperature and time series of graph in python 2.7 like below
I'm getting temperature and time values from OpenTSDB, and time value is originally appeared as unix time but I changed it to string with using like below
TIME = time.localtime(float(_time));
stime = '%4d/%02d/%02d %02d:%02d:%02d' % (TIME.tm_year, TIME.tm_mon, TIME.tm_mday, TIME.tm_hour, TIME.tm_min, TIME.tm_sec);
Is there any ways to get a slope of graph above using gradient descent?
I tried with this tutorial https://anaconda.org/benawad/gradient-descent/notebook, but doesn't work and gives me an awkward answer of slope.
EDIT
def plot_line(y, data_points):
x_values = [i for i in range(int(min(data_points))-1, int(max(data_points))+2)]
y_values = [y(x) for x in x_values]
plt.plot(x_values, y_values, 'b')
def summation(y, x_points, y_points):
total1 = 0
total2 = 0
for i in range(1, len(x_points)):
total1 += y(x_points[i]) - y_points[i]
total2 += (y(x_points[i]) - y_points[i]) * x_points[i]
return total1 / len(x_points), total2 / len(x_points)
m = 0
b = 0
y = lambda x : m*x + b;
learn = 0.1;
for i in range(5):
s1, s2 = summation(y, timenum_list, temperature_list)
m = m - learn * s2
b = b - learn * s1
print m;
print b;
plt.plot(timenum_list, temperature_list, 'bo')
title('Time vs Temperature')
xlabel('Time')
ylabel('Temperature')
I used the above function for gradient descent, but didn't work well.
timenum_list
is the list of unix time.
temperature_list
is the list of temperature.
Gradient descent is an alogrithm to find extremes (minimum or maximum) of a function and the problem is, you do not have a function. All you have is a sequence of points. Now you might try to fit a polynomial to your points and compute the derivative of that function, but that probably would not be too accurate, given your data is 'bumpy', or you would have to use a high degree polynomial.
The second option is linear interpolation: In plain words, take two points and fit a line between them and calculate the slope of that line.
The real question is: what do you need to accomplish in the first place?