Skip to content

Commit bcf1fb6

Browse files
committedDec 15, 2016
update
1 parent 110d227 commit bcf1fb6

File tree

14 files changed

+461
-120
lines changed

14 files changed

+461
-120
lines changed
 

‎README.md

+19-120
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
这个 github 是我做 python 机器学习视频教程的支柱. 里面都是视频中提到的代码源码. 主要做的方面是机器学习的, 包括神经网络等等.
33
自己的 research 是在神经网络和强化学习方面, 所以以后也会补上强化学习的教程, 敬请期待. 欢迎大家分享传播, 让我们都能轻松的学习~
44

5-
[<img src="https://github.com/MorvanZhou/tutorials/blob/master/%E7%89%87%E5%A4%B4.png?raw=true" height="200">](http://morvanzhou.github.io/tutorials/)
5+
[<img src="https://github.com/MorvanZhou/tutorials/blob/master/%E7%89%87%E5%A4%B4.png?raw=true" height="200">](https://morvanzhou.github.io/tutorials/)
66

77

88
## 大陆没办法翻墙, 我们还有优酷, 网易云
@@ -15,123 +15,22 @@
1515
[Youtube 频道主页](https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg)
1616

1717

18-
## 以下内容是频道内的结构咯
19-
### Python 基础
20-
对于 python 还很陌生的同学们,没问题, 这里是一个快速入门的通道. 这不一定是最好的通道, 不过是通往机器学习道路上比较好的通道.我筛选了一些没必要的内容, 只留下专门为了机器学习有关的内容.
21-
22-
[Python 基础 教程视频(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cIRP5gCi8AlYwQ1uFO2aQBw)
23-
24-
[Python 基础 教程视频(优酷)](http://list.youku.com/albumlist/show?id=27312381&ascending=1&page=1)
25-
26-
---
27-
28-
### [机器学习-简介系列](https://github.com/MorvanZhou/tutorials/blob/master/ML_intro/README.md)
29-
[<img src='https://github.com/MorvanZhou/tutorials/blob/master/ML_intro/ML%20brief%20intro.png?raw=true' height=200>](https://github.com/MorvanZhou/tutorials/blob/master/ML_intro/README.md)
30-
31-
这是对于机器学习的各种方法的一个简短介绍, 还有学好机器学习很多实用的小技巧.
32-
33-
[机器学习-简介系列 视频教程(Youtuebe)](https://www.youtube.com/playlist?list=PLXO45tsB95cIFm8Y8vMkNNPPXAtYXwKin)
34-
35-
[机器学习-简介系列 视频教程(优酷)](http://list.youku.com/albumlist/show?id=27892935&ascending=1&page=1)
36-
37-
以及<机器学习-简介系列>的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/ML_intro/README.md)
38-
39-
---
40-
41-
### [Tensorflow](https://github.com/MorvanZhou/tutorials/blob/master/tensorflowTUT/Readme.md)
42-
[<img src="https://github.com/MorvanZhou/tutorials/blob/master/tensorflowTUT/Tensorflow%20course%20cover%20page.jpg?raw=true" height='200'>](https://github.com/MorvanZhou/tutorials/blob/master/tensorflowTUT/Readme.md)
43-
44-
Tensorflow 是神经网络方面的主力军之一. 这一套教程从最基础的方面,一直简单风趣地讲解到最高级的方面. 是入门的Tensorflow 神经网络的首选.
45-
46-
[Tensorflow 神经网络 深度学习 视频教程(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cKI5AIlf5TxxFPzb-0zeVZ8)
47-
48-
[Tensorflow 神经网络 深度学习 视频教程(优酷)](http://www.youku.com/playlist_show/id_27327189.html)
49-
50-
以及 Tensorflow 的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/tensorflowTUT/Readme.md)
51-
52-
---
53-
54-
### [SciKit-Learn (sklearn)](https://github.com/MorvanZhou/tutorials/blob/master/sklearnTUT/README.md)
55-
[<img src='https://github.com/MorvanZhou/tutorials/blob/master/sklearnTUT/sklearn%20cover%20page.jpg?raw=true' height='200'>](https://github.com/MorvanZhou/tutorials/blob/master/sklearnTUT/README.md)
56-
57-
SciKit-Learn 汇集了各种各样的机器学习方法, 是一个全方面的汇总. 对于不同的项目, 我们可能会用到不同的机器学习方法. 掌握 sklearn 的通用学习形式, 我们就能打遍天下无敌是.
58-
59-
[scikit-learn 机器学习 视频教程(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cI7ZleLM5i3XXhhe9YmVrRO)
60-
61-
[scikit-learn 机器学习 视频教程(优酷)](http://www.youku.com/playlist_show/id_27469882.html)
62-
63-
以及 Sklearn 的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/sklearnTUT/README.md)
64-
65-
---
66-
67-
### [Theano](https://github.com/MorvanZhou/tutorials/blob/master/theanoTUT/README.md)
68-
[<img src='https://github.com/MorvanZhou/tutorials/blob/master/theanoTUT/theano%20cover%20page.jpg?raw=true' height='200'>](https://github.com/MorvanZhou/tutorials/blob/master/theanoTUT/README.md)
69-
70-
Theano 算得上是 Tensorflow 的前身, 和 Tensorflow 有着类似的结构形式. 目前的 Tensorflow 还不能很好的支持 Windows 系统, 所以如果想在 Windows 系统上玩转神经网络, Theano 是一个很好的替代选择.
71-
72-
[Theano 神经网络 机器学习 视频教程(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cKpDID642AjNkygrSR5X15T)
73-
74-
[Theano 神经网络 机器学习 视频教程(优酷)](http://www.youku.com/playlist_show/id_27743371.html)
75-
76-
以及 Theano 的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/theanoTUT/README.md)
77-
78-
79-
---
80-
81-
### [Matplotlib](https://github.com/MorvanZhou/tutorials/blob/master/matplotlibTUT/README.md)
82-
[<img src='https://github.com/MorvanZhou/tutorials/blob/master/matplotlibTUT/cover%20page.jpg?raw=true' height='200'>](https://github.com/MorvanZhou/tutorials/blob/master/matplotlibTUT/README.md)
83-
84-
有了机器学习的各种方法, 我很还需要很多其他的工具来完善学习过程, 提高自己的学习效率. 其中之一就是把数据,结果都可视化出来. 有了直观的可视化结果, 我们就能更好的掌握知识. Matplotlib 就是 python 的一个可视化神器.
85-
86-
[Matplotlib 图像可视化 视频教程(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cKiBRXYqNNCw8AUo6tYen3l)
87-
88-
[Matplotlib 图像可视化 视频教程(优酷)](http://www.youku.com/playlist_show/id_28097045.html)
89-
90-
以及 Matplotlib 的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/matplotlibTUT/README.md)
91-
92-
---
93-
94-
### [Numpy & Pandas](https://github.com/MorvanZhou/tutorials/blob/master/numpy%26pandas/README.md)
95-
[<img src='https://github.com/MorvanZhou/tutorials/blob/master/numpy&pandas/cover%20page.jpg?raw=true' height='200'>](https://github.com/MorvanZhou/tutorials/blob/master/numpy%26pandas/README.md)
96-
97-
科学运算是工科生必不可少的一个工具, numpy 和 pandas 就是为了我们在 python 中运用高效的科学运算所开发出来的.
98-
99-
[Numpy & Pandas 数据处理 视频教程(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cKKyC45gatc8wEc3Ue7BlI4)
100-
101-
[Numpy & Pandas 数据处理 视频教程(优酷)](http://www.youku.com/playlist_show/id_27329155.html)
102-
103-
以及 Numpy & Pandas 的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/numpy%26pandas/README.md)
104-
105-
---
106-
107-
### Python Multiprocessing
108-
这是 python 的基础教程之一, 对于了解如何高效的运用我们的计算机运算能力有很好的帮助.
109-
110-
[Multiprocessing 多进程 视频教程(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cJgYDaJbwhg629-Il5cfkhe)
111-
112-
[Multiprocessing 多进程 视频教程(优酷)](http://www.youku.com/playlist_show/id_27423283.html)
113-
114-
---
115-
116-
### Python Threading
117-
同上, 可是在机器学习的运用中, 有时并没有 multiprocessing 模块好.
118-
119-
[Threading 多线程 视频教学(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cKaHtKLn-jat8SOGndS3MEt)
120-
121-
[Threading 多线程 视频教学(优酷)](http://www.youku.com/playlist_show/id_27399497.html)
122-
123-
---
124-
125-
### [Python Tkinter](https://github.com/MorvanZhou/tutorials/blob/master/tkinterTUT/README.md)
126-
[<img src='https://github.com/MorvanZhou/tutorials/blob/master/tkinterTUT/cover%20page.jpg?raw=true' height='200'>](https://github.com/MorvanZhou/tutorials/blob/master/tkinterTUT/README.md)
127-
128-
Tkinter 是 python 自带的可视化窗口包, 可以用来做 simulation 的可视化, 而且他也是跨平台兼容.
129-
130-
[Tkinter 跨平台 GUI 视频教学(Youtube)](https://www.youtube.com/playlist?list=PLXO45tsB95cJU56K4EtkG0YNGBZCuDwAH)
131-
132-
[Tkinter 跨平台 GUI 视频教学(优酷)](http://www.youku.com/playlist_show/id_27433146.html)
133-
134-
以及 tkinter 的[学习目录](https://github.com/MorvanZhou/tutorials/blob/master/tkinterTUT/README.md)
135-
136-
---
18+
全部内容:
19+
20+
* [Python 基础](https://morvanzhou.github.io/tutorials/python-basic/)
21+
* [基础](https://morvanzhou.github.io/tutorials/python-basic/basic/)
22+
* [多线程 threading](https://morvanzhou.github.io/tutorials/python-basic/threading/)
23+
* [多进程 multiprocessing](https://morvanzhou.github.io/tutorials/python-basic/multiprocessing/)
24+
* [简单窗口 tkinter](https://morvanzhou.github.io/tutorials/python-basic/tkinter/)
25+
* [机器学习](https://morvanzhou.github.io/tutorials/machine-learning/)
26+
* [有趣的机器学习](https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/)
27+
* [Tensorflow](https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/)
28+
* [Theano](https://morvanzhou.github.io/tutorials/machine-learning/theano/)
29+
* [Keras](https://morvanzhou.github.io/tutorials/machine-learning/keras/)
30+
* [Scikit-Learn](https://morvanzhou.github.io/tutorials/machine-learning/sklearn/)
31+
* [数据处理](https://morvanzhou.github.io/tutorials/data-manipulation/)
32+
* [Numpy & Pandas](https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/)
33+
* [Matplotlib](https://morvanzhou.github.io/tutorials/data-manipulation/plt/)
34+
* [其他](https://morvanzhou.github.io/tutorials/others/)
35+
* [Git 版本管理](https://morvanzhou.github.io/tutorials/others/git/)
13736

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
A simple example for Reinforcement Learning using table lookup Q-learning method.
3+
An agent "o" is on the left of a 1 dimensional world, the treasure is on the rightmost location.
4+
Run this program and to see how the agent will improve its strategy of finding the treasure.
5+
6+
View more on 莫烦Python: https://morvanzhou.github.io/tutorials/
7+
"""
8+
9+
import numpy as np
10+
import pandas as pd
11+
import time
12+
13+
np.random.seed(2) # reproducible
14+
15+
16+
N_STATES = 6 # the length of the 1 dimensional world
17+
ACTIONS = ['left', 'right'] # available actions
18+
EPSILON = 0.9 # greedy police
19+
ALPHA = 0.1 # learning rate
20+
LAMBDA = 0.9 # discount factor
21+
MAX_EPISODES = 13 # maximum episodes
22+
FRESH_TIME = 0.3 # fresh time for one move
23+
24+
25+
def build_q_table(n_states, actions):
26+
table = pd.DataFrame(
27+
np.zeros((n_states, len(actions))), # q_table initial values
28+
columns=actions, # actions's name
29+
)
30+
# print(table) # show table
31+
return table
32+
33+
34+
def choose_action(state, q_table):
35+
# This is how to choose an action
36+
state_actions = q_table.iloc[state, :]
37+
if (np.random.rand() > EPSILON) or (state_actions.all() == 0): # act non-greedy or state-action have no value
38+
action_name = np.random.choice(ACTIONS)
39+
else: # act greedy
40+
action_name = state_actions.argmax()
41+
return action_name
42+
43+
44+
def get_env_feedback(S, A):
45+
# This is how agent will interact with the environment
46+
if A == 'right': # move right
47+
if S == N_STATES - 2: # terminate
48+
S_ = 'terminal'
49+
R = 1
50+
else:
51+
S_ = S + 1
52+
R = 0
53+
else: # move left
54+
R = 0
55+
if S == 0:
56+
S_ = S # reach the wall
57+
else:
58+
S_ = S - 1
59+
return S_, R
60+
61+
62+
def update_env(S, episode, step_counter):
63+
# This is how environment be updated
64+
env_list = ['-']*(N_STATES-1) + ['T'] # '---------T' our environment
65+
if S == 'terminal':
66+
interaction = 'Episode %s: total_steps = %s' % (episode+1, step_counter)
67+
print('\r{}'.format(interaction), end='')
68+
time.sleep(2)
69+
print('\r ', end='')
70+
else:
71+
env_list[S] = 'o'
72+
interaction = ''.join(env_list)
73+
print('\r{}'.format(interaction), end='')
74+
time.sleep(FRESH_TIME)
75+
76+
77+
def rl():
78+
# main part of RL loop
79+
q_table = build_q_table(N_STATES, ACTIONS)
80+
for episode in range(MAX_EPISODES):
81+
step_counter = 0
82+
S = 0
83+
is_terminated = False
84+
update_env(S, episode, step_counter)
85+
while not is_terminated:
86+
87+
A = choose_action(S, q_table)
88+
S_, R = get_env_feedback(S, A) # take action & get next state and reward
89+
q_predict = q_table.ix[S, A]
90+
if S_ != 'terminal':
91+
q_target = R + LAMBDA * q_table.iloc[S_, :].max() # next state is not terminal
92+
else:
93+
q_target = R # next state is terminal
94+
is_terminated = True # terminate this episode
95+
96+
q_table.ix[S, A] += ALPHA * (q_target - q_predict) # update
97+
S = S_ # move to next state
98+
99+
update_env(S, episode, step_counter+1)
100+
step_counter += 1
101+
return q_table
102+
103+
104+
if __name__ == "__main__":
105+
q_table = rl()
106+
print('\r\nQ-table:\n')
107+
print(q_table)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)