Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 支持 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dice_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
c = np.array([i])
x_next[i] = dice_hmm.predict(X, i)

print "state: ", Z
print "logprob: ", logprob
print "prob of x_next: ", x_next
print("state: ", Z)
print("logprob: ", logprob)
print("prob of x_next: ", x_next)

15 changes: 7 additions & 8 deletions Stock_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
import datetime

import numpy as np
import pandas as pd
from matplotlib import cm, pyplot as plt
from matplotlib.dates import YearLocator, MonthLocator
from matplotlib.finance import quotes_historical_yahoo_ochl

from hmm import GaussianHMM
from sklearn.preprocessing import scale


###############################################################################
# 导入Yahoo金融数据
quotes = quotes_historical_yahoo_ochl(
"INTC", datetime.date(1995, 1, 1), datetime.date(2012, 1, 6))

dates = np.array([q[0] for q in quotes], dtype=int) # 日期列
close_v = np.array([q[2] for q in quotes]) # 收盘价
volume = np.array([q[5] for q in quotes])[1:] # 交易数
quotes = pd.read_csv('data/yahoofinance-INTC-19950101-20040412.csv')

dates = quotes.index.values
close_v = quotes[["Close"]].values.flatten()
volume = quotes[["Volume"]].values.flatten()
# diff:out[n] = a[n+1] - a[n] 得到价格变化
diff = np.diff(close_v)
dates = dates[1:]
close_v = close_v[1:]
volume = volume[1:]

# scale归一化处理:均值为0和方差为1
# 将价格和交易数组成输入数据
Expand All @@ -39,7 +38,7 @@
hidden_states = model.decode(X)

# 打印参数
print "Transition matrix: ", model.transmat_prob
print("Transition matrix: ", model.transmat_prob)
print("Means and vars of each hidden state")
for i in range(model.n_state):
print("{0}th hidden state".format(i))
Expand Down
22 changes: 11 additions & 11 deletions Wordseg_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def getList(input_str):

# 预处理词典:RenMinData.txt_utf8
def precess_data():
ifp = file("RenMinData.txt_utf8")
ifp = open("RenMinData.txt_utf8")
line_num = 0
word_dic = {}
word_ind = 0
Expand All @@ -38,18 +38,18 @@ def precess_data():
for line in ifp:
line_num += 1
if line_num % 10000 == 0:
print line_num
print(line_num)

line = line.strip()
if not line:continue
line = line.decode("utf-8","ignore")
# line = line.decode("utf-8","ignore")

word_list = []
for i in range(len(line)):
if line[i] == " ":continue
word_list.append(line[i])
# 建立单词表
if not word_dic.has_key(line[i]):
if not word_dic.__contains__(line[i]):
word_dic[line[i]] = word_ind
word_ind += 1
line_seq.append(word_list)
Expand All @@ -71,7 +71,7 @@ def precess_data():
def word_trans(wordline, word_dic):
word_inc = []
line = wordline.strip()
line = line.decode("utf-8", "ignore")
# line = line.decode("utf-8", "ignore")
for n in range(len(line)):
word_inc.append([word_dic[line[n]]])

Expand All @@ -81,8 +81,8 @@ def word_trans(wordline, word_dic):
wordseg_hmm = hmm.DiscreteHMM(4,len(word_dic),5)
wordseg_hmm.train_batch(X,Z)

print "startprob_prior: ", wordseg_hmm.start_prob
print "transmit: ", wordseg_hmm.transmat_prob
print("startprob_prior: ", wordseg_hmm.start_prob)
print("transmit: ", wordseg_hmm.transmat_prob)

sentence_1 = "我要回家吃饭"
sentence_2 = "中国人民从此站起来了"
Expand All @@ -94,8 +94,8 @@ def word_trans(wordline, word_dic):
Z_3 = wordseg_hmm.decode(word_trans(sentence_3,word_dic))
Z_4 = wordseg_hmm.decode(word_trans(sentence_4,word_dic))

print u"我要回家吃饭: ", Z_1
print u"中国人民从此站起来了: ", Z_2
print u"经党中央研究决定: ", Z_3
print u"江主席发表重要讲话: ", Z_4
print(u"我要回家吃饭: ", Z_1)
print(u"中国人民从此站起来了: ", Z_2)
print(u"经党中央研究决定: ", Z_3)
print(u"江主席发表重要讲话: ", Z_4)

Loading