有没有什么方法可以使用 python 的梯度下降来获得时间序列的斜率?
Is there any ways to get a slope in time series using gradient descent from python?
我是 python 和机器学习领域的新手。我正在尝试使用梯度下降法(可能是线性回归)从 python 2.7 中的温度和时间序列图获得斜率,如下所示
我从 OpenTSDB 获取温度和时间值,时间值最初显示为 unix 时间,但我使用如下所示将其更改为字符串
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);
有什么方法可以使用梯度下降法得到上面图的斜率吗?
我试过本教程 https://anaconda.org/benawad/gradient-descent/notebook,但没有用,而且给了我一个尴尬的斜率答案。
编辑
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')
我用了上面的函数做梯度下降,但是效果不佳
timenum_list
是unix时间的列表。
temperature_list
是温度列表。
梯度下降是一种用于查找函数极值(最小值或最大值)的算法,问题是您没有函数。您所拥有的只是一系列点。现在您可能会尝试将多项式拟合到您的点并计算该函数的导数,但这可能不太准确,因为您的数据是 'bumpy',否则您将不得不使用高阶多项式。
第二个选项是线性插值:简单地说,取两个点并在它们之间拟合一条线并计算该线的斜率。
真正的问题是:您首先需要完成什么?
我是 python 和机器学习领域的新手。我正在尝试使用梯度下降法(可能是线性回归)从 python 2.7 中的温度和时间序列图获得斜率,如下所示
我从 OpenTSDB 获取温度和时间值,时间值最初显示为 unix 时间,但我使用如下所示将其更改为字符串
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);
有什么方法可以使用梯度下降法得到上面图的斜率吗?
我试过本教程 https://anaconda.org/benawad/gradient-descent/notebook,但没有用,而且给了我一个尴尬的斜率答案。
编辑
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')
我用了上面的函数做梯度下降,但是效果不佳
timenum_list
是unix时间的列表。
temperature_list
是温度列表。
梯度下降是一种用于查找函数极值(最小值或最大值)的算法,问题是您没有函数。您所拥有的只是一系列点。现在您可能会尝试将多项式拟合到您的点并计算该函数的导数,但这可能不太准确,因为您的数据是 'bumpy',否则您将不得不使用高阶多项式。
第二个选项是线性插值:简单地说,取两个点并在它们之间拟合一条线并计算该线的斜率。
真正的问题是:您首先需要完成什么?