昨天周末晚上没有出去,码了一小段,先留着kangkang。
昨天周末晚上没有出去,码了一小段,先留着kangkang。
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from tqdm import tqdm
# wrapper class for an interval
# readability is more important than efficiency, so I won't use many tricks
class Interval:
# [@left, @right)
def __init__(self, left, right):
self.left = left
self.right = right
# whether a point is in this interval
def contain(self, x):
return self.left <= x < self.right
# length of this interval
def size(self):
return self.right - self.left
# domain of the square wave, [0, 2)
DOMAIN = Interval(0.0, 2.0)
# square wave function
def square_wave(x):
if 0.5 < x < 1.5:
return 1
return 0
# get @n samples randomly from the square wave
def sample(n):
samples = []
for i in range(0, n):
x = np.random.uniform(DOMAIN.left, DOMAIN.right)
y = square_wave(x)
samples.append([x, y])
return samples
# wrapper class for value function
class ValueFunction:
# @domain: domain of this function, an instance of Interval
# @alpha: basic step size for one update
def __init__(self, feature_width, domain=DOMAIN, alpha=0.2, num_of_features=50):
self.feature_width = feature_width
self.num_of_featrues = num_of_features
self.features = []
self.alpha = alpha
self.domain = domain
# there are many ways to place those feature windows,
# following is just one possible way
step = (domain.size() - feature_width) / (num_of_features - 1)
left = domain.left
for i in range(0, num_of_features - 1):
self.features.append(Interval(left, left + feature_width))
left += step
self.features.append(Interval(left, domain.right))
# initialize weight for each feature
self.weights = np.zeros(num_of_features)
# for point @x, return the indices of corresponding feature windows
def get_active_features(self, x):
active_features = []
for i in range(0, len(self.features)):
if self.features[i].contain(x):
active_features.append(i)
return active_features
# estimate the value for point @x
def value(self, x):
active_features = self.get_active_features(x)
return np.sum(self.weights[active_features])
# update weights given sample of point @x
# @delta: y - x
def update(self, delta, x):
active_features = self.get_active_features(x)
delta *= self.alpha / len(active_features)
for index in active_features:
self.weights[index] += delta
# train @value_function with a set of samples @samples
def approximate(samples, value_function):
for x, y in samples:
delta = y - value_function.value(x)
value_function.update(delta, x)
# Figure 9.8
def figure_9_8():
num_of_samples = [10, 40, 160, 640, 2560, 10240]
feature_widths = [0.2, 0.4, 1.0]
plt.figure(figsize=(30, 20))
axis_x = np.arange(DOMAIN.left, DOMAIN.right, 0.02)
for index, num_of_sample in enumerate(num_of_samples):
print(num_of_sample, 'samples')
samples = sample(num_of_sample)
value_functions = [ValueFunction(feature_width) for feature_width in feature_widths]
plt.subplot(2, 3, index + 1)
plt.title('%d samples' % (num_of_sample))
for value_function in value_functions:
approximate(samples, value_function)
values = [value_function.value(x) for x in axis_x]
plt.plot(axis_x, values, label='feature width %.01f' % (value_function.feature_width))
plt.legend()
plt.savefig('../images/figure_9_8.png')
plt.close()
if __name__ == '__main__':
figure_9_8()
有更好的想法,再编辑完善一下,嘿嘿
最新文章
- jQuery中,$(&#39;#main&#39;) 与 document.getElementById(&#39;main&#39;)是什么样的关系-转
- PHP表单数据验证
- Android ArrayAdapter使用
- WinAPI【远程注入】三种注入方案【转】
- Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路
- having count(*) >; 1
- CROSS APPLY和 OUTER APPLY 区别
- umask设置导致程序权限不足的问题
- Oracle replace函数使用
- DEDECMS批量修改默认文章和列表命名规则的方法
- Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站
- 【VB/.NET】Converting VB6 to VB.NET 【Part II】【之四】
- [河南省ACM省赛-第三届] 聪明的kk (nyoj 171)
- spring 里面的StringUtils,先放这儿,有时间研究吧
- gulp基于seaJs模块化项目打包实践【原创】
- Orcle导入导出dmp文件
- 掌握一门语言Go
- python2.7源码编译安装
- A*搜索详解(2)——再战觐天宝匣
- python 编码规范 (http://www.runoob.com/w3cnote/google-python-styleguide.html)
热门文章
- HUE Oozie : error=2, No such file or directory采坑记录
- Hyperledger Fabric:最简单的方式测试你的链码
- 导入Jar报错An internal error occurred during: ";Building workspace";. zip END header not found
- JVM基础回顾记录(一):JVM的内存模型
- Django生命周期
- JeeSite | 保存信息修改记录封装
- PHP ThinkPHP 非常好用的增删改查方法
- django.db.utils.OperationalError: (1093, ";You can&#39;t specify target table &#39;xxx&#39; for update in FROM clause";)
- maven修改运行环境配置
- Git - Git分支管理策略