机器学习学习笔记--HMM从雅虎拿股票数据分析

本文通过使用高斯隐马尔可夫模型(Gaussian Hidden Markov Model)对Intel公司的历史股价和交易量数据进行分析,展示了如何利用Python及其相关库进行数据获取、模型训练及结果可视化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网的例子:

http://hmmlearn.readthedocs.io/en/latest/auto_examples/plot_hmm_stock_analysis.html#sphx-glr-auto-examples-plot-hmm-stock-analysis-py

但是已经过期了 这个代码无法用

我改动之后

# coding: utf-8

# In[1]:


from __future__ import print_function
import datetime

import numpy as np
from matplotlib import cm, pyplot as plt
from matplotlib.dates import YearLocator, MonthLocator
try:
    from matplotlib.finance import quotes_historical_yahoo_ochl
except ImportError:
    # For Matplotlib prior to 1.5.
    from matplotlib.finance import (
        quotes_historical_yahoo as quotes_historical_yahoo_ochl
    )

from hmmlearn.hmm import GaussianHMM


print(__doc__)

import fix_yahoo_finance as yf


# In[2]:


quotes = yf.download("INTC", datetime.date(1995, 1, 1), datetime.date(2012, 1, 6))
quotes_matrix = quotes.reset_index().as_matrix()


# In[12]:


print(quotes)
quotes = np.array(quotes)
for q in quotes:
    open = np.array(q[0])
    close = np.array(q[3])
    volume=np.array(q[5])
    print(open,close,volume)
    #获取到开盘价 收盘价 和 交易体量


# In[19]:


open = np.array(quotes[:,0])
close = np.array(quotes[:,3])
volume=np.array(quotes[:,5])
print(open)


# In[22]:


x=np.column_stack([close,volume])
print(x)


# In[25]:


#以上是用numpy获取数据 ,以下是用HMM训练 运行高斯HMM
print("fitting to HMM and decoding ...", end="")
#创建一个HMM实例并执行fit 
model = GaussianHMM(n_components=4,covariance_type="diag",n_iter=1000).fit(x)


# In[26]:


#预测内部隐藏状态的最佳顺序
hidden_states=model.predict(x)


# In[27]:


#以下是画图
print("Transition matrix")
print(model.transmat_)



# In[28]:


print("Means and vars of each hidden state")
for i in range(model.n_components):
    print("{0}th hidden state".format(i))
    print("mean = ",model.means_[i])
    print("val = ",np.diag(model.covars_[i]))
    print()


# In[30]:


fig , axs = plt.subplots(model.n_components,sharex=True,sharey=True)
colours = cm.rainbow(np.linspace(0,1,model.n_components))
#plt.subplots  有s和没有s  有差别的


# In[34]:


for i ,(ax,colour) in enumerate(zip(axs,colours)):
    #使用花哨索引来绘制每个状态的数据
    mask = hidden_states == i
    ax.plot_date(open[mask],close[mask],".-", c=colour)
    ax.set_title("{0}th hidden state".format(i))
    # Format the ticks.
    ax.xaxis.set_major_locator(YearLocator())
    ax.xaxis.set_minor_locator(MonthLocator())

    ax.grid(True)
plt.show()

输出

Transition matrix

[[  9.80610945e-001  5.01334224e-003  7.89925823e-233  1.43757129e-002]

[  3.00453847e-003  9.94724313e-001  5.06277757e-287  2.27114819e-003]

[  1.22706578e-240  1.69469602e-106  9.97700148e-001  2.29985206e-003]

[  1.86991637e-002  6.75484942e-029  1.17468735e-108  9.81300836e-001]]

Means and vars of each hidden state

0th hidden state

mean =  [  2.23179993e+01  6.28104280e+07]

val =  [  2.26268607e+00  4.27252350e+14]

1th hidden state

mean =  [  3.53577227e+01  5.32016661e+07]

val =  [  1.42426609e+02  4.34011309e+14]

2th hidden state

mean =  [  7.55640975e+00  8.17939427e+07]

val =  [  2.51862808e+00  2.20319259e+15]

3th hidden state

mean =  [  1.78359459e+01  7.62517437e+07]

val =  [  4.62819936e+00  1.25113880e+15]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值