# 机器学习股票价格预测从爬虫到预测-预测与调参

```rate_of_return.plot(kind='hist',bins=150,alpha=0.3,color='g',normed=1)
rate_of_return.plot(kind='kde',xlim=[-0.1,0.1],style='r',grid=True,figsize=(15,10))
plt.show()```

OK，现在我们按照机器学习股票价格预测初级实战这篇文章的方法对黄金价格进行预测，主要思路就是，通过对预测当天前两天的涨跌情况作为特征，而标签为预测当天的涨跌情况。

```# 窗口期对预测数量的影响
win_ratio = []
window_list = [x for x in range(22) if x != 0]
i = 1
for window in window_list:

X = deque()
y = deque()
clf = tree.DecisionTreeClassifier()
prediction = 0
test_num = 0
win_num = 0

current_index = window

for current_index in range(current_index, len(up_and_down)-1, 1):
fact = up_and_down[current_index+1]

X.append(list(up_and_down[(current_index-window): current_index]))
y.append(up_and_down[current_index])
if len(y) > 100:
test_num += 1
clf.fit(X, y)

prediction = clf.predict([list(up_and_down[(current_index-window+1): current_index+1])])

if prediction[0] == fact:
win_num += 1
ratio = win_num/test_num
print("已完成预测",i,'次')
i += 1
win_ratio.append(ratio)

fig = plt.figure(figsize=(12,10))
plt.plot(window_list,win_ratio,'ro--')
plt.show()```

```# 样本数量对预测率的影响
window = 2
win_ratio = []
samples_list = [x*5 for x in range(60) if x != 0]

for samples in samples_list:

X = deque()
y = deque()
clf = tree.DecisionTreeClassifier()
prediction = 0
test_num = 0
win_num = 0

current_index = 2

for current_index in range(current_index, len(up_and_down)-1, 1):
fact = up_and_down[current_index+1]

X.append(list(up_and_down[(current_index-window): current_index]))
y.append(up_and_down[current_index])
if len(y) > samples:
test_num += 1
clf.fit(X, y)

prediction = clf.predict([list(up_and_down[(current_index-window+1): current_index+1])])

if prediction[0] == fact:
win_num += 1
ratio = win_num/test_num
win_ratio.append(ratio)
print("预测完毕")

fig = plt.figure(figsize=(12,10))
plt.plot(samples_list,win_ratio,'ro--')
plt.show()```

31 篇文章38 人订阅

0 条评论

## 相关文章

### 普通程序员，几个月如何成功转型AI？

IT派 - {技术青年圈} 持续关注互联网、大数据、人工智能领域 动辄50万的毕业生年薪，动辄100万起步价的海归AI高级人才，普通员到底应不应该转型AI工程...

5056

2293

3103

### 最新综述文章推荐：自然语言生成、深度学习算法、多媒体大数据分析

【导读】专知内容组整理了最近人工智能领域相关期刊的5篇最新综述文章，为大家进行介绍，欢迎查看! 1 ▌自然语言生成综述：任务，应用，评价 ---- ---- ...

6487

3218

9622

### 【1996~2016】盘点 20 年 AAAI 人工智能最佳论文

【新智元导读】新智元汇集计算机科学领域1996年到2016年人工智能领域最佳论文，附上题目及摘要的中文翻译，并对这些作品进行系统考察，从中可以看出人工智能这门学...

4499

4356

36810

1922