计算 Pandas 中一系列趋势线的斜率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38385162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:35:32  来源:igfitidea点击:

calculating slope for a series trendline in Pandas

pythonnumpypandas

提问by Dmitry B.

Is there an idiomatic way of getting the slope for linear trend line fitting values in a DataFramecolumn? The data is indexed with DateTimeindex.

是否有一种惯用的方法来获取列中线性趋势线拟合值的斜率DataFrame?数据使用索引进行DateTime索引。

采纳答案by piRSquared

This should do it:

这应该这样做:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(100, 5), pd.date_range('2012-01-01', periods=100))

def trend(df):
    df = df.copy().sort_index()
    dates = df.index.to_julian_date().values[:, None]
    x = np.concatenate([np.ones_like(dates), dates], axis=1)
    y = df.values
    return pd.DataFrame(np.linalg.pinv(x.T.dot(x)).dot(x.T).dot(y).T,
                        df.columns, ['Constant', 'Trend'])


trend(df)

enter image description here

在此处输入图片说明

Using the same dfabove for its index:

使用df上面相同的索引:

df_sample = pd.DataFrame((df.index.to_julian_date() * 10 + 2) + np.random.rand(100) * 1e3, df.index)

df_sample = pd.DataFrame((df.index.to_julian_date() * 10 + 2) + np.random.rand(100) * 1e3, df.index)

coef = trend(df_sample)
df_sample['trend'] = (coef.iloc[0, 1] * df_sample.index.to_julian_date() + coef.iloc[0, 0])
df_sample.plot(style=['.', '-'])

enter image description here

在此处输入图片说明