gpt4 book ai didi

python - 是否有 pandas 自相关图方法的 Bokeh 版本?

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:42 24 4
gpt4 key购买 nike

假设我们有一个名为"series" 的时间序列对象。我知道使用 autocorrelation_plot() 方法绘制 series 对象的滞后和自相关维度非常容易。

代码如下:

from matplotlib import pyplot
from pandas.tools.plotting import autocorrelation_plot
autocorrelation_plot(series)
pyplot.show()

这是 Pandas 图:

enter image description here

有没有办法使用 Bokeh 服务器获得相同的情节?

最佳答案

是的,有。我编写的代码可以为您提供与 pandas autocorrelation_plot() 方法相同的结果。

代码如下:

from bokeh.layouts import column
from bokeh.plotting import figure, curdoc
import timeseries_model_creator # to get data
import numpy as np

TimeSeriesModelCreator = timeseries_model_creator.TimeSeriesModelCreator()
series = TimeSeriesModelCreator.read_csv() # time series object

def get_autocorrelation_plot_params(series):
n = len(series)
data = np.asarray(series)

mean = np.mean(data)
c0 = np.sum((data - mean) ** 2) / float(n)

def r(h):
return ((data[:n - h] - mean) *
(data[h:] - mean)).sum() / float(n) / c0
x = np.arange(n) + 1
y = map(r, x)
print "x : ", x, " y : ", y
z95 = 1.959963984540054
z99 = 2.5758293035489004
return n, x, y, z95, z99

n, x, y, z95, z99 = get_autocorrelation_plot_params(series)

auto_correlation_plot2 = figure(title='Time Series Auto-Correlation', plot_width=1000,
plot_height=500, x_axis_label="Lag", y_axis_label="Autocorrelation")

auto_correlation_plot2.line(x, y=z99 / np.sqrt(n), line_dash='dashed', line_color='grey')
auto_correlation_plot2.line(x, y=z95 / np.sqrt(n), line_color='grey')
auto_correlation_plot2.line(x, y=0.0, line_color='black')
auto_correlation_plot2.line(x, y=-z95 / np.sqrt(n), line_color='grey')
auto_correlation_plot2.line(x, y=-z99 / np.sqrt(n), line_dash='dashed', line_color='grey')

auto_correlation_plot2.line(x, y, line_width=2)
auto_correlation_plot2.circle(x, y, fill_color="white", size=8) # optional

curdoc().add_root(column(auto_correlation_plot2))

这是 Bokeh 图:

enter image description here

关于python - 是否有 pandas 自相关图方法的 Bokeh 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43344406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com