时间序列 python_Python-时间序列

本文介绍了Python中的时间序列,包括创建时间序列和处理样例数据。示例涉及将股票价格数据组织成DataFrame,并设置日期作为索引。
摘要由CSDN通过智能技术生成
时间序列 python

时间序列 python

Python-时间序列 (Python - Time Series)

Time series is a series of data points in which each data point is associated with a timestamp. A simple example is the price of a stock in the stock market at different points of time on a given day. Another example is the amount of rainfall in a region at different months of the year.

时间序列是一系列数据点,其中每个数据点都与时间戳关联。 一个简单的例子是在给定的一天中,股票在不同时间点的价格。 另一个例子是该地区一年中不同月份的降雨量。

In the below example we take the value of stock prices every day for a quarter for a particular stock symbol. We capture these values as a csv file and then organize them to a dataframe using pandas library. We then set the date field as index of the dataframe by recreating the additional Valuedate column as index and deleting the old valuedate column.

在下面的示例中,我们以特定股票代码每天四分之一的股价价格为例。 我们将这些值捕获为一个csv文件,然后使用pandas库将它们组织到一个数据框中。 然后,通过将其他Valuedate列重新创建为索引并删除旧的valuedate列,将date字段设置为数据框的索引。

样本数据 (Sample Data)

Below is the sample data for the price of the stock on different days of a given quarter. The data is saved in a file named as stock.csv

以下是给定季度不同日期的股票价格示例数据。 数据保存在名为stock.csv的文件中


ValueDate	Price
01-01-2018,	1042.05
02-01-2018,	1033.55
03-01-2018,	1029.7
04-01-2018,	1021.3
05-01-2018,	1015.4
...
...
...
...
23-03-2018,	1161.3
26-03-2018,	1167.6
27-03-2018,	1155.25
28-03-2018,	1154


创建时间序列 (Creating Time Series)


from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('path_to_file/stock.csv')
df = pd.DataFrame(data, columns = ['ValueDate', 'Price'])

# Set the Date as Index
df['ValueDate'] = pd.to_datetime(df['ValueDate'])
df.index = df['ValueDate']
del df['ValueDate']


df.plot(figsize=(15, 6))
plt.show()

Its output is as follows −

输出如下-

timeseries.png

翻译自: https://www.tutorialspoint.com/python_data_science/python_time_series.htm

时间序列 python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Statsmodels是Python中用于统计建模和计量经济学的库,它提供了各种统计模型,包括线性回归、时间序列分析等。在时间序列分析中,ARIMA模型是一种常用的模型。 ARIMA模型是自回归移动平均模型的缩写,它是一种广义的线性模型,常用于描述时间序列数据的自相关结构和随机性。ARIMA模型可以分为AR(自回归)、MA(移动平均)和差分(I)三部分,其中AR是指用当前值的前几个值来预测当前值,MA是指用当前误差的前几个值来预测当前误差,差分是指对时间序列进行差分处理,使其变得平稳。 在Python中,使用Statsmodels中的ARIMA模型进行时间序列分析可以分为以下几个步骤: 1. 导入相关库 ```python import pandas as pd import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt ``` 2. 读取数据 ```python data = pd.read_csv("data.csv", index_col=0, parse_dates=True) ``` 3. 绘制时间序列图 ```python plt.plot(data) plt.show() ``` 4. 确定模型阶数 可以使用ACF和PACF图来确定ARIMA模型的阶数。ACF图展示了时间序列与其滞后版本之间的自相关性,PACF图展示了当前时间序列与其滞后版本之间的部分自相关性。根据ACF和PACF图的信息,可以确定ARIMA模型的p、d和q参数。 ```python fig, ax = plt.subplots(2,1) sm.graphics.tsa.plot_acf(data, lags=30, ax=ax[0]) sm.graphics.tsa.plot_pacf(data, lags=30, ax=ax[1]) plt.show() ``` 5. 拟合模型 根据确定的ARIMA模型阶数,使用ARIMA()函数拟合时间序列数据。 ```python model = sm.tsa.ARIMA(data, order=(p,d,q)) results = model.fit() ``` 6. 模型诊断 使用plot_diagnostics()函数进行模型诊断,检查残差是否符合白噪声假设。 ```python results.plot_diagnostics(figsize=(15, 12)) plt.show() ``` 7. 预测 使用forecast()函数进行预测。 ```python forecast = results.forecast(steps=10) ``` 以上就是使用Python中Statsmodels包进行时间序列分析ARIMA模型的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值