有100个分类 最后的全连接层怎么设置

全连接层的作用是什么? - 知乎679被浏览52261分享邀请回答6添加评论分享收藏感谢收起1276人阅读
caffe框架(59)
慢慢填坑中,今天来仔细讲一下卷基层和全连接层训练参数个数如何确定的问题。我们以Mnist为例,首先贴出网络配置文件:
[python]&&
OK,在开始讲解之前我们先说明几个问题:
1、输入的图片大小是28*28,;
2、我们将分三部分讲解,因为三部分计算方式不同;
3、由于偏置量b的个数与卷积核的个数相同,因此我们讲解的主要是权重,偏置量个数加上就可以了。
1、第一个卷积conv1,之所把第一个卷积单独拿出来,是因为他和后面的卷积计算方式不同,他训练参数个数计算并不关心输入,这里的数据就是指data层中batch_size大小。也可以说第一个卷基层并不关心特征组合,只是提取特征。
在每一个卷积层中都以一个参数num_output,这个参数怎么理解呢?两种理解方式1、卷积的种类个数;2、输出特征图的个数,我么可以认为一种卷积核提取一种特征,然后输出一张特征。
由于第一个卷积层只是简单的提取特征,并没有进行特征组合,因此训练参数个数计算只是num_output*kernel_size^2.这里怎么理解呢?(由于我不会画图,需要大家一点想象力)假设我们的输入有5张图,num_output=3,kernel_size=5。没有进行特征组合,只是简单提取特征,指的是一种卷积核对5张图的同一区域使用相同的权重进行卷积计算,这样每幅图使用相同的卷积核就能提取到相同的特征,然后相同的特征组成一张特征图。
2、第二个卷积至全连接层之间的卷积,这些卷积层的训练参数个数和输入特征图的数量有关,因为这些卷积层需要进行特征组合。举个例子:conv1的num_output=20,说明卷积1层输出了20个特征图,那么卷积2层的输入就是20。conv2的num_output=50,kernel_size=5,那么计算公式是20*50*5*5.
为什么这些卷积层的训练个数和输入的特征图的数量有关呢?重点还是在特征组合。输入的20个特征图,每个特征图代表一种特征,如果我们给每种特征不同的权重那是不是就进行了特征组合呢?conv2的卷积核是5*5,对20个特征图进行卷积,那就会有20组(5*5)个连接(每张特征图是一组),如果这20组卷积核的权重相同,那就回到了第一个卷积层的情况,没有对20个特征进行组合,因为权重相同嘛!只能看成简单的相加,如果20组权重不同,是不是就进行了线性相加了呢?所以对于一个卷积核(5*5)我们要学习的参数不是25个,而是25*20个。说到这里我相信你应该已经明白了吧!
3、全连接层,全连接层就是普通的神经网络,全连接层的num_output和卷积层中num_output的理解不同,全连接层的num_output应该看成神经元的个数。
3.1、这里要细分一下,先说IP1也就是第一个全连接层。先讲一下ip1的输入,比如最后一个卷积层的num_output=50,那么IP1的输入是50吗?注意这里不是,要理解这个问题,我们只需将全连接层看成是一些列的普通神经网络就可以。比如IP1的num_output=500,也就是有500个神经元,每个神经元都和输入的每一个像素相连,最后一个卷积层输出了50个特征图,每个特征图大小是4*4(输入图像是28*28)那么每个神经元连接的个数就是50*16=800个,也就有800个参数需要学习。总共有500个神经元,因此对于IP1层共需要学习800*500=400,000个参数。
3.2、对于iP2层,iP2的输入就是IP1的输出了,因为IP1输出的不是图像了(或矩阵)而是500个数字。比如ip2的num_output=10,也就是输出数据500维,输出10维的普通神经网络,那么需要学习的参数就是500*10=5000个。
以上,只是我的个人见解,如果有错误,欢迎大家指正!
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:250911次
积分:3925
积分:3925
排名:第7464名
原创:83篇
转载:287篇
评论:136条
研二在读,关注:深度学习,计算机视觉,机器学习,图像处理。博文主要是自己的一些总结。转载文章供资料学习(转载一些最新文章),如有侵权望告知。注:有些评论没有回复,最近比较忙不好意思。
(25)(7)(14)(43)(57)(22)(16)(46)(24)(43)(42)(40)2613人阅读
caffe(35)
CV & ML(35)
original url:
http://blog.csdn.net/tina_ttl/article/details/
在caffe中,网络的结构由prototxt文件中给出,由一些列的Layer(层)组成,常用的层如:数据加载层、卷积操作层、pooling层、非线性变换层、内积运算层、归一化层、损失计算层等;本篇主要介绍全连接层
该层是对元素进行wise to wise的运算
1. 全连接层总述
下面首先给出全连接层的结构设置的一个小例子(定义在.prototxt文件中)&
name: &fc6&
type: &InnerProduct&
bottom: &pool5&
top: &fc6&
lr_mult: 1
decay_mult: 1
lr_mult: 2
decay_mult: 0
inner_product_param {
num_output: 4096
weight_filler {
type: &gaussian&
std: 0.005
bias_filler {
type: &constant&
value: 0.1
2. 全连接层相关参数&
接下来,分别对全连接层的相关参数进行说明
(根据全连接层层的定义,它的学习参数应该为权值和bias,其他的相关参数都为hyper-paramers,在定义模型时是要给出的)
注:全链接层其实也是一种卷积层,只不过卷积核大小与输入图像大小一致
lr_mult:学习率系数
放置在param{}中
该系数用来控制学习率,在进行训练过程中,该层参数以该系数乘solver.prototxt配置文件中的base_lr的值为学习率
即学习率=lr_mult*base_lr
如果该层在结构配置文件中有两个lr_mult,则第一个表示权值学习率系数,第二个表示偏执项的学习率系数(一般情况下,偏执项的学习率系数是权值学习率系数的两倍)
inner_product_param:内积层的其他参数
放置在inner_product_param{}中
该部分对内积层的其他参数进行设置,有些参数为必须设置,有些参数为可选(因为可以直接使用默认值)
必须设置的参数
num_output:filter个数
其他可选的设置参数
weight_filter:权值初始化方法,使用方法如下
weight_filter{
& & & type:&xavier& &//这里的xavier是一冲初始化算法,也可以是“gaussian”;默认值为“constant”,即全部为0
bias_filter:偏执项初始化方法
bias_filter{
& & & type:&xavier& &//这里的xavier是一冲初始化算法,也可以是“gaussian”;默认值为“constant”,即全部为0
}bias_term:是否使用偏执项,默认值为Ture
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:795382次
积分:8295
积分:8295
排名:第2302名
原创:155篇
转载:274篇
评论:25条
(3)(3)(9)(7)(3)(6)(10)(14)(15)(12)(7)(22)(7)(6)(3)(1)(5)(3)(9)(14)(3)(11)(15)(2)(1)(4)(2)(1)(3)(4)(2)(1)(3)(6)(4)(4)(3)(9)(16)(1)(5)(3)(2)(1)(10)(18)(22)(7)(23)(15)(8)(35)(19)(7)全连接层的作用是什么? - 知乎679被浏览52261分享邀请回答91 条评论分享收藏感谢收起&img src=&/v2-dcde9cf8af526_b.jpg& data-rawwidth=&1144& data-rawheight=&249& class=&origin_image zh-lightbox-thumb& width=&1144& data-original=&/v2-dcde9cf8af526_r.jpg&&&p&一直苦于没有系统学习seanborn的教程,似乎市面上也还没有完整的官方文档的学习资料。终于下决心用几天的时间通读下官方文档,并把记录下来。&/p&&p&基于&a href=&/?target=http%3A//seaborn.pydata.org/tutorial.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&官方0.71版本&i class=&icon-external&&&/i&&/a&,所有代码和图片皆已验证,与官方结论不符的地方会进行标注。如果有翻译失当或理解有误的地方,敬请随意指正!&/p&&h2&第一章 艺术化的图表控制&/h2&&p&一个引人入胜的图表非常重要,赏心悦目的图形不但能让数据探索中一些重要的细节更容易被挖掘,也能更有利于在与观众交流分析结果的过程中吸引观众的注意力并使观众们更容易记住结论。&/p&&p&Matplotlib无疑是高度可定制的,但快速实施出吸引人的细节就变得有些复杂。Seaborn作为一个带着定制主题和高级界面控制的Matplotlib扩展包,能让绘图变得更轻松,本部分主要介绍seaborn是如何对matplotlib输出的外观进行控制的。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&%matplotlib inline
#jupyter notebook 中的魔法函数,如果不是使用该软件请使用plt.show()用于显示图像
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(sum(map(ord,&aesthetics&)))
# 定义种子
&/code&&/pre&&/div&&p&定义一个含偏移的正弦图像,来比较传统的matplotlib和seaborn的不同:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&def sinplot(flip=1):
x = np.linspace(0,14,100)
for i in range(1,7):
plt.plot(x,np.sin(x+i*.5)*(7-i)*flip)
&/code&&/pre&&/div&&p&使用matplotlib默认设置的图形效果:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sinplot()
&/code&&/pre&&/div&&img src=&/v2-96e264d6bdb_b.jpg& data-rawwidth=&374& data-rawheight=&252& class=&content_image& width=&374&&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&import seaborn as sns
&/code&&/pre&&/div&&img src=&/v2-d0e5a4d24a0bcbe01d9c2fd995ae6099_b.jpg& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-d0e5a4d24a0bcbe01d9c2fd995ae6099_r.jpg&&&p&&br&&/p&&p&seaborn默认的灰色网格底色灵感来源于matplotlib却更加柔和。大多数情况下,图应优于表。seaborn的默认灰色网格底色避免了刺目的干扰,对于多个方面的图形尤其有用,是一些更复杂的工具的核心。&/p&&p&Seaborn将matplotlib参数分成两个独立的组。第一组设定了美学风格,第二组则是不同的度量元素,这样就可以很容易地添加到代码当中了。&/p&&p&操作这些参数的接口是两对函数。为了控制样式,使用axesstyle()和setstyle()函数。为了扩展绘图,请使用plotting_context()和set_context()函数。在这两种情况下,第一个函数返回一个参数字典,第二个函数则设置matplotlib默认属性。&/p&&h2&样式控制:axes_style() and set_style()&/h2&&p&有5个seaborn的主题,适用于不同的应用和人群偏好:&/p&&ul&&li&darkgrid 黑色网格(默认)&/li&&li&whitegrid 白色网格&/li&&li&dark 黑色背景&/li&&li&white 白色背景&/li&&li&ticks 应该是四周都有刻度线的白背景?&/li&&/ul&&p&网格能够帮助我们查找图表中的定量信息,而灰色网格主题中的白线能避免影响数据的表现,白色网格主题则类似的,当然更适合表达“重数据元素”(heavy data elements不理解)&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&whitegrid&)
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data);
&/code&&/pre&&/div&&img src=&/v2-436b148ab0e2f0cd127e_b.jpg& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-436b148ab0e2f0cd127e_r.jpg&&&p&&br&&/p&&p&对于许多场景,(特别是对于像对话这样的设置,您主要想使用图形来提供数据模式的印象),网格就不那么必要了&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&dark&)
&/code&&/pre&&/div&&img src=&/v2-c180b44ec928b083715a_b.png& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-c180b44ec928b083715a_r.png&&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&white&)
&/code&&/pre&&/div&&img src=&/v2-df3f22bde655_b.jpg& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-df3f22bde655_r.jpg&&&p&&br&&/p&&p&有时你可能想要给情节增加一点额外的结构,这就是ticks参数的用途:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&ticks&)
# 官方的例子在上方/右方也拥有刻度线,而验证时却没有(是jupyter notebook的原因?)
&/code&&/pre&&/div&&img src=&/v2-050c042daedce34c1d01cba_b.jpg& data-rawwidth=&491& data-rawheight=&341& class=&origin_image zh-lightbox-thumb& width=&491& data-original=&/v2-050c042daedce34c1d01cba_r.jpg&&&p&&br&&/p&&blockquote&这里是官方运行的结果,供参考:&/blockquote&&img src=&/v2-6ff1efc7e3fc9e_b.jpg& data-rawwidth=&494& data-rawheight=&349& class=&origin_image zh-lightbox-thumb& width=&494& data-original=&/v2-6ff1efc7e3fc9e_r.jpg&&&p&&br&&/p&&p&特别的可以通过sns.axes_style(style=None, rc=None) 返回一个sns.set_style()可传的参数的字典&/p&&p&通过类似sns.set_style(&ticks&, {&xtick.major.size&: 8, &ytick.major.size&: 8})的方式写入更具体的配置样式。&/p&&p&关于sns.axes_style()下面会有说明和运行结果&/p&&h2&用despine()进行边框控制&/h2&&p&white和ticks参数的样式,都可以删除上方和右方坐标轴上不需要的边框,这在matplotlib中是无法通过参数实现的,却可以在seaborn中通过despine()函数轻松移除他们。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&white&)
sinplot() # 默认无参数状态,就是删除上方和右方的边框
sns.despine()
&/code&&/pre&&/div&&img src=&/v2-e3b1dd4ffa5b12b2550d16_b.png& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-e3b1dd4ffa5b12b2550d16_r.png&&&p&&br&&/p&&p&一些图的边框可以通过数据移位,当然调用despine()也能做同样的事。当边框没有覆盖整个数据轴的范围的时候,trim参数会限制留存的边框范围。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10, trim=True); # offset 两坐标轴离开距离;
&/code&&/pre&&/div&&img src=&/v2-11a06ec6f0fbb3e18f6db83f7a288c63_b.jpg& data-rawwidth=&489& data-rawheight=&339& class=&origin_image zh-lightbox-thumb& width=&489& data-original=&/v2-11a06ec6f0fbb3e18f6db83f7a288c63_r.jpg&&&p&&br&&/p&&p&你也可以通过往despine()中添加参数去控制边框&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&whitegrid&)
sns.boxplot(data=data, palette=&deep&)
sns.despine(left=True) # 删除左边边框
st = sns.axes_style(&darkgrid&)
&/code&&/pre&&/div&&img src=&/v2-a36d19c549a4ea5a6bca0743ef7fec8c_b.jpg& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-a36d19c549a4ea5a6bca0743ef7fec8c_r.jpg&&&p&&br&&/p&&p&despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)&/p&&p&从plot()函数中移除顶部或右边的边框&/p&&h2&临时设定图形样式&/h2&&p&虽然来回切换非常容易,但sns也允许用with语句中套用axes_style()达到临时设置参数的效果(仅对with块内的绘图函数起作用)。这也允许创建不同风格的坐标轴。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&with sns.axes_style(&darkgrid&):
plt.subplot(211)
plt.subplot(212)
sinplot(-1)
&/code&&/pre&&/div&&img src=&/v2-a2a586a7c89b70bdde41f0e_b.jpg& data-rawwidth=&487& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&487& data-original=&/v2-a2a586a7c89b70bdde41f0e_r.jpg&&&p&&br&&/p&&h2&seaborn样式中最重要的元素&/h2&&p&如果您想要定制seanborn的样式,可以将参数字典传递给axes_style()和set_style()的rc参数。注意,只能通过该方法覆盖样式定义的一部分参数。(然而,更高层次的set()函数接受任何matplotlib参数的字典)。&/p&&p&如果您想要查看包含哪些参数,您可以只调用该函数而不带参数,这将返回当前设置的字典:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.axes_style()
{'axes.axisbelow': True,
'axes.edgecolor': 'white',
'axes.facecolor': '#EAEAF2',
'axes.grid': True,
'axes.labelcolor': '.15',
'axes.linewidth': 0.0,
'figure.facecolor': 'white',
'font.family': ['sans-serif'],
'font.sans-serif': ['Arial',
'Liberation Sans',
'Bitstream Vera Sans',
'sans-serif'],
'grid.color': 'white',
'grid.linestyle': '-',
'image.cmap': 'Greys',
'legend.frameon': False,
'legend.numpoints': 1,
'legend.scatterpoints': 1,
'lines.solid_capstyle': 'round',
'text.color': '.15',
'xtick.color': '.15',
'xtick.direction': 'out',
'xtick.major.size': 0.0,
'xtick.minor.size': 0.0,
'ytick.color': '.15',
'ytick.direction': 'out',
'ytick.major.size': 0.0,
'ytick.minor.size': 0.0}
&/code&&/pre&&/div&&p&或许,你可以试试不同种类的参数效果&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_style(&darkgrid&, {&axes.facecolor&: &.9&})
&/code&&/pre&&/div&&img src=&/v2-657ac986efca_b.jpg& data-rawwidth=&479& data-rawheight=&329& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-657ac986efca_r.jpg&&&p&&br&&/p&&h2&通过 plotting_context() 和 set_context() 调整绘图元素&/h2&&p&另一组参数控制绘图元素的规模,这应该让您使用相同的代码来制作适合在较大或较小的情节适当的场景中使用的情节。&/p&&p&首先,可以通过sns.set()重置参数。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set()
&/code&&/pre&&/div&&p&四种预设,按相对尺寸的顺序(线条越来越粗),分别是paper,notebook, talk, and poster。notebook的样式是默认的,上面的绘图都是使用默认的notebook预设。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_context(&paper&)
plt.figure(figsize=(8,6))
&/code&&/pre&&/div&&img src=&/v2-f9d617e2bdddbe2ba7098f_b.jpg& data-rawwidth=&475& data-rawheight=&352& class=&origin_image zh-lightbox-thumb& width=&475& data-original=&/v2-f9d617e2bdddbe2ba7098f_r.jpg&&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# default 默认设置
sns.set_context(&notebook&)
plt.figure(figsize=(8,6))
&/code&&/pre&&/div&&img src=&/v2-89da973ee04d8216af1d_b.jpg& data-rawwidth=&479& data-rawheight=&356& class=&origin_image zh-lightbox-thumb& width=&479& data-original=&/v2-89da973ee04d8216af1d_r.jpg&&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_context(&talk&)
plt.figure(figsize=(8,6))
&/code&&/pre&&/div&&img src=&/v2-1ffc7dd72c54d11e8a27e_b.jpg& data-rawwidth=&484& data-rawheight=&361& class=&origin_image zh-lightbox-thumb& width=&484& data-original=&/v2-1ffc7dd72c54d11e8a27e_r.jpg&&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_context(&poster&)
plt.figure(figsize=(8,6))
&/code&&/pre&&/div&&img src=&/v2-dd92bbbdba278_b.png& data-rawwidth=&490& data-rawheight=&366& class=&origin_image zh-lightbox-thumb& width=&490& data-original=&/v2-dd92bbbdba278_r.png&&&p&&br&&/p&&p&通过观察各种样式的结果,你应当可以了解context函数&/p&&p&类似的,还可以使用其中一个名称来调用set_context()来设置参数,您可以通过提供参数值的字典来覆盖参数。&/p&&p&通过更改context还可以独立地扩展字体元素的大小。(这个选项也可以通过顶级set()函数获得)。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&sns.set_context(&notebook&, font_scale=1.5, rc={&lines.linewidth&: 2.5})
&/code&&/pre&&/div&&img src=&/v2-a3a968fdb8ab6f33d5b1b9e4_b.jpg& data-rawwidth=&484& data-rawheight=&334& class=&origin_image zh-lightbox-thumb& width=&484& data-original=&/v2-a3a968fdb8ab6f33d5b1b9e4_r.jpg&&&p&&br&&/p&&p&类似地(尽管它可能用处不大),也可以使用with嵌套语句进行临时的设置。&/p&&p&样式和上下文都可以用set()函数快速地进行配置。这个函数还设置了默认的颜色选项,在下一节将详细介绍这一功能。&/p&&p&&br&&/p&&p&【第一章 完】&/p&&p&&br&&/p&&p&【未禾原创,所有章节已经完结,其他章节请点击跳转】&/p&&p&&a href=&/p/& class=&internal&&Seaborn(sns)官方文档学习笔记(第一章 艺术化的图表控制) - 知乎专栏&/a&&/p&&p&&a href=&/p/& class=&internal&&Seaborn(sns)官方文档学习笔记(第二章 斑驳陆离的调色板) - 知乎专栏&/a&&/p&&p&&a href=&/p/& class=&internal&&Seaborn(sns)官方文档学习笔记(第三章 分布数据集的可视化) - 知乎专栏&/a&&/p&&p&&a href=&/p/& class=&internal&&Seaborn(sns)官方文档学习笔记(第四章 线性关系的可视化) - 知乎专栏&/a&&/p&&p&&a href=&/p/& class=&internal&&Seaborn(sns)官方文档学习笔记(第五章 分类数据的绘制) - 知乎专栏&/a&&/p&&p&&a href=&/p/& class=&internal&&Seaborn(sns)官方文档学习笔记(第六章 绘制数据网格) - 知乎专栏&/a&&/p&
一直苦于没有系统学习seanborn的教程,似乎市面上也还没有完整的官方文档的学习资料。终于下决心用几天的时间通读下官方文档,并把记录下来。基于,所有代码和图片皆已验证,与官方结论不符的地方会进行标注。如果有翻译失当或理解有误的地方,…
iOS UI开发指定是用Mac啊 其他语言的话用win系统就好了
iOS UI开发指定是用Mac啊 其他语言的话用win系统就好了
&p&我是word2016,在公式的最后键入#和标号,然后回车就有了标号&/p&
我是word2016,在公式的最后键入#和标号,然后回车就有了标号
&p&【海量信息】&/p&&p&We can have an easy access to a huge amount of information on the internet. The internet is a vast ocean of knowledge in both natural sciences and social sciences. Search engine like google can lead us to the information we need in no time and considerably broaden our horizon&/p&&p&【提升健康】&/p&&p&It can improve health condition and enhance our ability to respond quickly. Besides, as long as we keep doing it frequently, we can effectively lose weight and keep ourselves in good shape. In addition, it can better prepare us for any physical challenge in the future&/p&&p&【宽容】&/p&&p&He should tolerate relatively small mistakes committed by others. Making mistakes is somehow inevitable, as long as it’s unintentional, he is supposed to forgive it. The tolerance will breed gratitude from others.&/p&&p&【人际交往】&/p&&p&During the process of communication, my interpersonal skills will improve considerably. This can help me cooperate with others more effectively, and render me more opportunities to make new acquaintance. With more friends, I could live happily and more likely to succeed.&/p&&p&【增加幽默感】&/p&&p&The humorous style could also develop my sense of humor. This will make others more willing to listen to my stories and bring fun to my life and the lives of others. Consequently, it can improve my interpersonal skills and make me more popular&/p&&p&【促进就业】&/p&&p&This will contribute to the increase of employment rate. I mean, it can provide the local people with a prodigious amount of jobs. As a result, it would in turn bring benefits to the social stability and economic development&/p&&p&【环保】&/p&&p&Another point worth mentioning is that, it is environment friendly. With the help of this, we can better protect our planet and prevent our environment from deterioration. No one can deny that it is significant nowadays when we’re facing ecological crisis&/p&&p&【了解文化】&/p&&p&This can definitely help me improve the understanding of the culture, custom, history and many interesting stuff about the country. The deeper insight into culture can not only broaden my horizon but also help me comprehend the living style and thinking mode of the local people&/p&&p&【更好地学习】&/p&&p&With the help of this, students can concentrate on study, and learn all kinds of useful knowledge more effectively and efficiently. Thus, it can put them in a better position in the future job market&/p&&p&【与家人共享天伦】&/p&&p&More importantly, I can go home from time to time and visit my parents and grandparents. My mom will prepare me the food I like the most, my father will talk to me and help me figure out a way to solve the problem I encounter at work. My feeling can be best described by an old saying, “East, West, home is the best”&/p&&p&【隐私】&/p&&p&This can help me protect my privacy. Even though I am not secluded and literally gregarious instead, I still need my privacy. Cause being attended sometimes makes me feel uncomfortable, sometimes I just need some private time and deal with my personal stuff like calling my friends&/p&&p&【有营养】&/p&&p&They are nutritious which have a lot of body-building nutrition and vitamin we really need.&/p&&p&【享受美食】&/p&&p&I can have some tasty food. It serves the amazing dumpling of all kinds of flavors, pork flavor, chicken flavor and so on. One thing I like the most is the caraway there, it really help enrich the taste of dumplings. Anyway, it never fails to make my mouth water.&/p&&p&【生活便利】&/p&&p&Living in the city makes life easier.I can buy all stuff I need anytime and I have an easy access to theaters and cinemas where I can relax myself. Moreover, I can find the hospital nearby whenever I don’t feel well. To put all into a nutshell, it’s convenient.&/p&&p&【安全】&/p&&p&The most important point is that it’s much safer. The possibility of being stole or robbed is relatively small. With a safe environment, I can better concentrate on my own business without worrying about possible jeopardy.&/p&&p&【便于携带】&/p&&p&It is not heavy and big like the others, it’s portable so I can take it anywhere. On the bus, in the dining hall,&/p&&p&you see, the convenience is undeniable.&/p&&p&【有更多知识】&/p&&p&As the saying goes, “Knowledge is power”, sufficient knowledge can carve out a way to success. With more knowledge, we would have a broader horizon and&/p&&p&can figure out some possible solutions when we encounter a problem.&/p&&p&&b&资料推荐:&/b&&/p&&a href=&/?target=https%3A//mp./s%3F__biz%3DMzIwMjcyNjM0Ng%3D%3D%26mid%3D%26idx%3D1%26sn%3D3cd838de0b95ec06f6b2%26chksm%3D96db0ce6a1ac85f0a2ae356aaa1c012ac4bbeebd3eb4225b6bae32a1e%23rd& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&如何破28分?《十天搞定托福作文》,诚意满满助你杀托!&i class=&icon-external&&&/i&&/a&&br&&a href=&/?target=https%3A//mp./s%3F__biz%3DMzIwMjcyNjM0Ng%3D%3D%26mid%3D%26idx%3D1%26sn%3D2dfec7f0ade%26chksm%3D96db0cb7a1ac85a123baa181e33d1ca673e83cd55910d%23rd& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&精品资料《14天挑战toefl ibt听力满分》配套音频分享!&i class=&icon-external&&&/i&&/a&&br&&a href=&/?target=https%3A//mp./s%3F__biz%3DMzIwMjcyNjM0Ng%3D%3D%26mid%3D%26idx%3D1%26sn%3Dc971e132aae87achksm%3D96db0f95a1ac8f36140eacf81c%23rd& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&【2017年最新最全TPO资料】1-49完整版全套资料下载订阅!100%靠谱!&i class=&icon-external&&&/i&&/a&&br&&p&&b&获取资料:&/b&&/p&&p&&b&加普斯留学小助手微信:pusiedu01,&/b&&/p&&p&&b&备注“知乎获取托福资料”即可!&/b&&/p&
【海量信息】We can have an easy access to a huge amount of information on the internet. The internet is a vast ocean of knowledge in both natural sciences and social sciences. Search engine like google can lead us to the information we ne…
&a class=& wrap external& href=&///?target=http%3A//www.cs.cmu.edu/%7E./213/schedule.html& target=&_blank& rel=&nofollow noreferrer&&15-213/18-218/15-513: Introduction to Computer Systems / Schedule Fall 2016&i class=&icon-external&&&/i&&/a&&br&CMU15-213的课程主页,有ppt,代码还有录像,主讲人就是这本书的作者。&br&&br&下图显示了213这门课在整个课程体系中的重要性,它是众多课程的先修要求。至于15213的先修课程则是122使用C0语言教学(C语言的一个子集)&a class=& wrap external& href=&///?target=http%3A//www.cs.cmu.edu/%7Erjsimmon/15122-s16/schedule.html& target=&_blank& rel=&nofollow noreferrer&&15-122 S16。 &i class=&icon-external&&&/i&&/a&如果没有编程基础则可先修15112,使用Python教学(&a class=& wrap external& href=&///?target=http%3A//www.cs.cmu.edu/%7E112/schedule.html& target=&_blank& rel=&nofollow noreferrer&&15-112 Schedule (Fall 16)&i class=&icon-external&&&/i&&/a&)。&br&&img data-rawheight=&960& data-rawwidth=&1280& src=&/9d6b531ff92ce_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1280& data-original=&/9d6b531ff92ce_r.jpg&&&br&下面是两位作者讲课时的截图&img data-rawheight=&440& data-rawwidth=&362& src=&/cb60d12b8a154c1a1cada5_b.png& class=&content_image& width=&362&&&br&&img data-rawheight=&382& data-rawwidth=&500& src=&/57eeb216a1d4acf6b764e0_b.png& class=&origin_image zh-lightbox-thumb& width=&500& data-original=&/57eeb216a1d4acf6b764e0_r.png&&
CMU15-213的课程主页,有ppt,代码还有录像,主讲人就是这本书的作者。 下图显示了213这门课在整个课程体系中的重要性,它是众多课程的先修要求。至于15213的先修课程则是122…
同推荐《程序员的自我修养—链接、装载与库》, 可以看看我写的这篇文章:&br&&a href=&///?target=http%3A//yonghaowu.github.io//Linker_And_Loaders/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&C/C++编译链接与装载深入浅出&i class=&icon-external&&&/i&&/a&&br&&br&是我看完上面这本书, 还有&Linux C一站式编程&, &深入理解计算机系统&这三本书的编译链接部分,综合写的, 总的来说比较适合科普, 也尝试把知识有条理的串联起来.
同推荐《程序员的自我修养—链接、装载与库》, 可以看看我写的这篇文章:
是我看完上面这本书, 还有&Linux C一站式编程&, &深入理解计算机系统&这三本书的编译链接部分,综合写的, 总的来说比较适合科普, 也尝试把知识有条理的串…
又找了找图,发现能找到的还是很多的,但是系统原因一张张贴太麻烦,把我的壁纸软件告诉大家吧。&br&授人以鱼不如授人以渔不是么?*。 (ˊωˋ*) ?*。&br&强行安利一波,爱壁纸,我的壁纸基本都来自于这个软件,分辨率够高,资源也还算全&br&&img src=&/182bfbc09d00b0a69b96f7_b.png& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/182bfbc09d00b0a69b96f7_r.png&&&img src=&/ab_b.png& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/ab_r.png&&&br&——————————————————————————&br&作为知乎小透明估计也没人看&br&多图慎入&br&最近硬盘跪了,以前存的壁纸基本全没了,勉强找到这些,一部分来自电影和游戏&br&&img src=&/f2d4ac2f5a20fbe25323e00_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/f2d4ac2f5a20fbe25323e00_r.jpg&&&img src=&/6fa1c0d95f2f6bdfa527_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/6fa1c0d95f2f6bdfa527_r.jpg&&&img src=&/7fe72221bfbafbb43f872b_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/7fe72221bfbafbb43f872b_r.jpg&&&img src=&/154a75157eff611ce5f11_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/154a75157eff611ce5f11_r.jpg&&&img src=&/3d87ffbaef0_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/3d87ffbaef0_r.jpg&&&img src=&/ba4ee63d01cd452e69a578f6c695a206_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/ba4ee63d01cd452e69a578f6c695a206_r.jpg&&&img src=&/e1e53811ddfa041d656c90_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/e1e53811ddfa041d656c90_r.jpg&&&img src=&/2a6e960fba910e26a3de5a1ddc798fc6_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/2a6e960fba910e26a3de5a1ddc798fc6_r.jpg&&&img src=&/bc2d5c28f7da5_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/bc2d5c28f7da5_r.jpg&&&img src=&/db92a59cb41be3ec0c357_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/db92a59cb41be3ec0c357_r.jpg&&&img src=&/0fd35a82edc2f45ea4ee899_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/0fd35a82edc2f45ea4ee899_r.jpg&&&img src=&/233c650cbeefbdafac755ef95d189c19_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/233c650cbeefbdafac755ef95d189c19_r.jpg&&&img src=&/d6e253c5ae49b_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/d6e253c5ae49b_r.jpg&&&img src=&/acb501eb7e75_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/acb501eb7e75_r.jpg&&&img src=&/cc0fceb8ae5a_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/cc0fceb8ae5a_r.jpg&&再来点末世题材的&br&&img src=&/9fc15cc9bde_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/9fc15cc9bde_r.jpg&&&img src=&/636a183fe091dd2f822b2f5dad4cc1d5_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/636a183fe091dd2f822b2f5dad4cc1d5_r.jpg&&&img src=&/38e7e42fce63e32c357d_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/38e7e42fce63e32c357d_r.jpg&&&img src=&/4bac85a0adb2_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/4bac85a0adb2_r.jpg&&&img src=&/dd468c5e0fa67c_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/dd468c5e0fa67c_r.jpg&&&img src=&/b855fb39eca_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/b855fb39eca_r.jpg&&上一张我自己现在用的壁纸,图片出自秒速五厘米&img src=&/10e6a801caaa929a73acc5_b.jpg& data-rawwidth=&2880& data-rawheight=&1800& class=&origin_image zh-lightbox-thumb& width=&2880& data-original=&/10e6a801caaa929a73acc5_r.jpg&&
又找了找图,发现能找到的还是很多的,但是系统原因一张张贴太麻烦,把我的壁纸软件告诉大家吧。 授人以鱼不如授人以渔不是么?*。 (ˊωˋ*) ?*。 强行安利一波,爱壁纸,我的壁纸基本都来自于这个软件,分辨率够高,资源也还算全 —————————…
&p&看了一圈没有人提到的,但是之前在网上不知道什么地方看过,现在又搜了一下,把它贴出来。本篇文章转来转去已经找不到原文链接了,放一下摘抄的链接:&a href=&///?target=http%3A///article/17-python-tips.html& class=&internal&&17 大 Python 奇技淫巧&/a&&/p&&br&&p&1、显示有限的接口到外部当发布python第三方package时,并不希望代码中所有的函数或者class可以被外部import,在__init__.py中添加
__all__属性,该list中填写可以import的类或者函数名, 可以起到限制的import的作用, 防止外部import其他函数或者类。&/p&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
from base import APIBase
from client import Client
from decorator import interface, export, stream
from server import Server
from storage import Storage
from util import (LogFormatter, disable_logging_to_stderr,
enable_logging_to_kids, info)
__all__ = ['APIBase', 'Client', 'LogFormatter', 'Server',
'Storage', 'disable_logging_to_stderr', 'enable_logging_to_kids',
'export', 'info', 'interface', 'stream']
&/code&&/pre&&/div&&br&&p&2、with的魔力&/p&&p&with语句需要支持&b&上下文管理协议的对象,&/b& 上下文管理协议包含__enter__和__exit__两个方法。 with语句建立运行时上下文需要通过这两个方法执行&b&进入和退出&/b&操作。&/p&&p&其中&b&上下文表达式&/b&是跟在with之后的表达式, 该表达式返回一个上下文管理对象。&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&# 常见with使用场景
with open(&test.txt&, &r&) as my_file:
# 注意, 是__enter__()方法的返回值赋值给了my_file,
for line in my_file:
print line
&/code&&/pre&&/div&&p&详细原理可以查看这篇文章,&a href=&///?target=https%3A///developerworks/cn/opensource/os-cn-pythonwith/& class=&internal&&浅谈 Python 的 with 语句&/a&&/p&&p&知道具体原理,我们可以自定义支持上下文管理协议的类,类中实现__enter__和__exit__方法。&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
class MyWith(object):
def __init__(self):
print &__init__ method&
def __enter__(self):
print &__enter__ method&
return self
# 返回对象给as后的变量
def __exit__(self, exc_type, exc_value, exc_traceback):
print &__exit__ method&
if exc_traceback is None:
print &Exited without Exception&
return True
print &Exited with Exception&
return False
def test_with():
with MyWith() as my_with:
print &running my_with&
print &------分割线-----&
with MyWith() as my_with:
print &running before Exception&
raise Exception
print &running after Exception&
if __name__ == '__main__':
test_with()
&/code&&/pre&&/div&&p&执行结果如下:&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&__init__ method
__enter__ method
running my_with
__exit__ method
Exited without Exception
------分割线-----
__init__ method
__enter__ method
running before Exception
__exit__ method
Exited with Exception
Traceback (most recent call last):
File &bin/python&, line 34, in &module&
exec(compile(__file__f.read(), __file__, &exec&))
File &test_with.py&, line 33, in &module&
test_with()
File &test_with.py&, line 28, in test_with
raise Exception
&/code&&/pre&&/div&&p&证明了会先执行__enter__方法, 然后调用with内的逻辑, 最后执行__exit__做退出处理, 并且, 即使出现异常也能正常退出&/p&&p&3、filter的用法&/p&&p&相对filter而言, map和reduce使用的会更频繁一些, filter正如其名字, 按照某种规则&b&过滤&/b&掉一些元素&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
lst = [1, 2, 3, 4, 5, 6]
# 所有奇数都会返回True, 偶数会返回False被过滤掉
print filter(lambda x: x % 2 != 0, lst)
&/code&&/pre&&/div&&br&&p&4、一行作判断&/p&&p&当条件满足时, 返回的为等号后面的变量, 否则返回else后语句。&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&lst = [1, 2, 3]
new_lst = lst[0] if lst is not None else None
print new_lst
# 打印结果
&/code&&/pre&&/div&&br&&p&5、装饰器之单例&/p&&p&使用装饰器实现简单的单例模式&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&# 单例装饰器
def singleton(cls):
instances = dict()
# 初始为空
def _singleton(*args, **kwargs):
if cls not in instances:
#如果不存在, 则创建并放入字典
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return _singleton
@singleton
class Test(object):
if __name__ == '__main__':
t1 = Test()
t2 = Test()
# 两者具有相同的地址
print t1, t2
&/code&&/pre&&/div&&br&&p&6、staticmethod装饰器&/p&&p&类中两种常用的装饰, 首先区分一下他们:&/p&&ul&&li&普通成员函数, 其中第一个隐式参数为&b&对象&/b&&/li&&li&&b&classmethod装饰器&/b&, 类方法(给人感觉非常类似于OC中的类方法), 其中第一个隐式参数为&b&类&/b&&/li&&li&&b&staticmethod装饰器&/b&, 没有任何隐式参数. &b&python中的静态方法类似与C++中的静态方法&/b&&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
class A(object):
# 普通成员函数
def foo(self, x):
print &executing foo(%s, %s)& % (self, x)
@classmethod
# 使用classmethod进行装饰
def class_foo(cls, x):
print &executing class_foo(%s, %s)& % (cls, x)
@staticmethod
# 使用staticmethod进行装饰
def static_foo(x):
print &executing static_foo(%s)& % x
def test_three_method():
# 直接调用噗通的成员方法
obj.foo(&para&)
# 此处obj对象作为成员函数的隐式参数, 就是self
obj.class_foo(&para&)
# 此处类作为隐式参数被传入, 就是cls
A.class_foo(&para&)
#更直接的类方法调用
obj.static_foo(&para&)
# 静态方法并没有任何隐式参数, 但是要通过对象或者类进行调用
A.static_foo(&para&)
if __name__ == '__main__':
test_three_method()
# 函数输出
executing foo(&__main__.A object at 0x100ba4e10&, para)
executing class_foo(&class '__main__.A'&, para)
executing class_foo(&class '__main__.A'&, para)
executing static_foo(para)
executing static_foo(para)
&/code&&/pre&&/div&&p&7、property装饰器&/p&&p&将property与装饰器结合实现属性私有化(&b&更简单安全的实现get和set方法&/b&)。&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&#python内建函数
property(fget=None, fset=None, fdel=None, doc=None)
&/code&&/pre&&/div&&p&fget是获取属性的值的函数,fset是设置属性值的函数,fdel是删除属性的函数,doc是一个字符串( 像注释一样)。从实现来看,这些参数都是可选的。&/p&&p&property有三个方法getter(), setter()和delete() 来指定fget, fset和fdel。 这表示以下这行:&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&class Student(object):
#相当于property.getter(score) 或者property(score)
def score(self):
return self._score
@score.setter #相当于score = property.setter(score)
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value & 0 or value & 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
&/code&&/pre&&/div&&br&&p&8、iter魔法&/p&&ul&&li&通过yield和__iter__的结合,我们可以把一个对象变成可迭代的&/li&&li&通过__str__的重写, 可以直接通过想要的形式打印对象&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
class TestIter(object):
def __init__(self):
self.lst = [1, 2, 3, 4, 5]
def read(self):
for ele in xrange(len(self.lst)):
def __iter__(self):
return self.read()
def __str__(self):
return ','.join(map(str, self.lst))
__repr__ = __str__
def test_iter():
obj = TestIter()
for num in obj:
if __name__ == '__main__':
test_iter()
&/code&&/pre&&/div&&br&&p&9、神奇partial&/p&&p&partial使用上很像C++中仿函数(函数对象)。&/p&&p&在stackoverflow给出了类似与partial的运行方式:&/p&&br&&div class=&highlight&&&pre&&code class=&language-text&&def partial(func, *part_args):
def wrapper(*extra_args):
args = list(part_args)
args.extend(extra_args)
return func(*args)
return wrapper
&/code&&/pre&&/div&&p&利用用&a href=&///?target=http%3A///article/javascript-bibao.html& class=&internal&&闭包&/a&的特性绑定预先绑定一些函数参数,返回一个可调用的变量, 直到真正的调用执行:&/p&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
def sum(a, b):
return a + b
def test_partial():
fun = partial(sum, 2)
# 事先绑定一个参数, fun成为一个只需要一个参数的可调用变量
print fun(3)
# 实现执行的即是sum(2, 3)
if __name__ == '__main__':
test_partial()
# 执行结果
&/code&&/pre&&/div&&p&10、神秘eval&/p&&p&eval我理解为一种内嵌的python解释器(这种解释可能会有偏差), 会解释字符串为对应的代码并执行, 并且将执行结果返回。&/p&&p&看一下下面这个例子:&/p&&br&&p&#!/usr/bin/env python&/p&&div class=&highlight&&&pre&&code class=&language-text&&# -*- coding: utf-8 -*-
def test_first():
def test_second(num):
return num
action = {
# 可以看做是一个sandbox
&para&: 5,
&test_first& : test_first,
&test_second&: test_second
def test_eavl():
condition = &para == 5 and test_second(test_first) & 5&
res = eval(condition, action)
# 解释condition并根据action对应的动作执行
&/code&&/pre&&/div&&br&&p&11、exec&/p&&ul&&li&exec在Python中会忽略返回值, 总是返回None, eval会返回执行代码或语句的返回值&/li&&li&exec和eval在执行代码时, 除了返回值其他行为都相同&/li&&li&在传入字符串时, 会使用compile(source, ‘&string&’, mode)编译字节码。 mode的取值为exec和eval&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&#!/usr/bin/env python
# -*- coding: utf-8 -*-
def test_first():
print &hello&
def test_second():
test_first()
print &second&
def test_third():
print &third&
action = {
&test_second&: test_second,
&test_third&: test_third
def test_exec():
exec &test_second& in action
if __name__ == '__main__':
test_exec()
# 无法看到执行结果
&/code&&/pre&&/div&&p&12、getattr&/p&&p&getattr(object, name[,
default])返回对象的命名属性,属性名必须是字符串。如果字符串是对象的属性名之一,结果就是该属性的值。例如, getattr(x,
‘foobar’)
等价于 x.foobar。 如果属性名不存在,如果有默认值则返回默认值,否则触发 AttributeError 。&/p&&br&&p&# 使用范例&/p&&div class=&highlight&&&pre&&code class=&language-text&&class TestGetAttr(object):
test = &test attribute&
def say(self):
print &test method&
def test_getattr():
my_test = TestGetAttr()
print getattr(my_test, &test&)
except AttributeError:
print &Attribute Error!&
getattr(my_test, &say&)()
except AttributeError: # 没有该属性, 且没有指定返回值的情况下
print &Method Error!&
if __name__ == '__main__':
test_getattr()
# 输出结果
test attribute
test method
&/code&&/pre&&/div&&br&&p&13、命令行处理&/p&&div class=&highlight&&&pre&&code class=&language-text&&def process_command_line(argv):
Return a 2-tuple: (settings object, args list).
`argv` is a list of arguments, or `None` for ``sys.argv[1:]``.
if argv is None:
argv = sys.argv[1:]
# initialize the parser object:
parser = optparse.OptionParser(
formatter=optparse.TitledHelpFormatter(width=78),
add_help_option=None)
# define options here:
parser.add_option(
# cu put --help last
'-h', '--help', action='help',
help='Show this help message and exit.')
settings, args = parser.parse_args(argv)
# check number of arguments, verify values, etc.:
parser.error('program takes no command- '
'&%s& ignored.' % (args,))
# further process settings & args if necessary
return settings, args
def main(argv=None):
settings, args = process_command_line(argv)
# application code here, like:
# run(settings, args)
if __name__ == '__main__':
status = main()
sys.exit(status)
&/code&&/pre&&/div&&br&&p&14、读写csv文件&/p&&div class=&highlight&&&pre&&code class=&language-text&&# 从csv中读取文件, 基本和传统文件读取类似
import csv
with open('data.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
# 向csv文件写入
import csv
with open( 'data.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(['name', 'address', 'age'])
# 单行写入
( 'xiaoming ','china','10'),
( 'Lily', 'USA', '12')]
writer.writerows(data)
# 多行写入
&/code&&/pre&&/div&&br&&p&15、各种时间形式转换只发一张网上的图, 然后查文档就好了, 这个是记不住的&/p&&img src=&/c2d989e16bdb80eb7a56_b.jpg& data-rawwidth=&739& data-rawheight=&549& class=&origin_image zh-lightbox-thumb& width=&739& data-original=&/c2d989e16bdb80eb7a56_r.jpg&&&p&只发一张网上的图, 然后查文档就好了, 这个是记不住的&/p&&br&&p&16、字符串格式化一个非常好用, 很多人又不知道的功能:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&&& name = &andrew&
&&& &my name is {name}&.format(name=name)
'my name is andrew'
&/code&&/pre&&/div&
看了一圈没有人提到的,但是之前在网上不知道什么地方看过,现在又搜了一下,把它贴出来。本篇文章转来转去已经找不到原文链接了,放一下摘抄的链接: 1、显示有限的接口到外部当发布python第三方package时,并不希望代码中所有的函数…
&img src=&/v2-c1b2fbacfdffa8dff9c7474_b.jpg& data-rawwidth=&812& data-rawheight=&740& class=&origin_image zh-lightbox-thumb& width=&812& data-original=&/v2-c1b2fbacfdffa8dff9c7474_r.jpg&&&p&这个是一个新系列,翻译 Duke University 的 STA 663 课程的全套内容,课件讲义教材练习题等等。&/p&&p&之前我在&a href=&/p/?fc=1&group_id=027840#comment-& class=&internal&&Computational Statistics in Python&/a& 这里提到了要翻译这套课程。&br&这几天就开始了。&br&本系列为对 Duke 大学 sta663 Computational-statistics-with-Python 课程的中文翻译。&br&项目地址:&a href=&/?target=https%3A///Kivy-CN/Computational-statistics-with-Python-CN& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Kivy-CN/Computational-statistics-with-Python-CN&i class=&icon-external&&&/i&&/a&&br&&/p&&p&中文版阅读地址就在本专栏内,就不贴了。&/p&&p&中文版翻译稿源文件地址:&a href=&/?target=https%3A///Kivy-CN/Computational-statistics-with-Python-CN& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&/Kivy-CN/Comp&/span&&span class=&invisible&&utational-statistics-with-Python-CN&/span&&span class=&ellipsis&&&/span&&i class=&icon-external&&&/i&&/a&&/p&&p&英文原版官网地址:&a href=&/?target=http%3A//people.duke.edu/%7Eccc14/sta-663/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Computational Statistics in Python&i class=&icon-external&&&/i&&/a&&/p&&p&英文版源文件地址:&a href=&/?target=https%3A///cliburn/Computational-statistics-with-Python& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&/cliburn/Comp&/span&&span class=&invisible&&utational-statistics-with-Python&/span&&span class=&ellipsis&&&/span&&i class=&icon-external&&&/i&&/a&&/p&&br&&p&首先,这个翻译的质量依然不会很高,因为我水平有限,以及是用空闲时间偶尔翻译,所以大家发现错误请一定提出批评,我会及时改正的。&/p&&p&另外我会逐步翻译 Duke 大学的 STA 633 课程的课件、纲要、教案 等等全部内容 &/p&&p&他们的这个课程和之前
&a href=&/p/& class=&internal&&我翻译ThinkPython&/a& 正好能衔接 &/p&&p&这个课程之后,又正好有足够的知识储备来尝试 斯坦福的 CS229 :&a href=&/?target=http%3A//cs229.stanford.edu/materials.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Machine Learning (Course handouts)&i class=&icon-external&&&/i&&/a&&/p&&p&也就是吴恩达当年带的那个机器学习的课程&/p&
这个是一个新系列,翻译 Duke University 的 STA 663 课程的全套内容,课件讲义教材练习题等等。之前我在 这里提到了要翻译这套课程。 这几天就开始了。 本系列为对 Duke 大学 sta663 Computational-statistics-with-Pyt…
&img src=&/v2-7aecb625fc9a3e4e86a74e_b.png& data-rawwidth=&1255& data-rawheight=&864& class=&origin_image zh-lightbox-thumb& width=&1255& data-original=&/v2-7aecb625fc9a3e4e86a74e_r.png&&今天发现 Duke 大学的一个站点,网站地址在这里:&a href=&/?target=http%3A//people.duke.edu/%7Eccc14/sta-663/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Computational Statistics in Python&i class=&icon-external&&&/i&&/a&&p&里面有从 Python 基础入门到 PyCUDA 乃至一些数理统计相关的完整的学习链。&/p&&p&简直太棒了!&br&&/p&&p&每一部分的衔接都很自然,个人认为学习曲线非常平缓,而涉及的范围却相当广泛。&/p&&p&当然内容有一些多,另外就是估计某些语言方面存在压力的新手朋友可能会觉得吃力一些。&/p&&p&另外就是估计有的朋友可能不方便访问境外的网站。&/p&&p&所以我先用 Httrack 下载了一份该站点的全部文件镜像,分享地址在这里:&a href=&/?target=https%3A///s/1nuB276T& class=& external& target=&_blank& rel=&nofollow noreferrer&&&span class=&invisible&&https://&/span&&span class=&visible&&/s/1nuB276&/span&&span class=&invisible&&T&/span&&span class=&ellipsis&&&/span&&i class=&icon-external&&&/i&&/a&&/p&&p&然后等接下来我忙完了手头的事情,会准备对这一套教程进行汉化翻译。&/p&&p&另外之前的 GlumPy 的翻译工作也会届时一同继续。&/p&&p&现在手头的事情有些紧迫,我忙完了这一段时间,就可以全身心投入到文档翻译上面了。&/p&&br&&p&根据&a href=&/people/f15c4cd6db290ca73ea6f& data-hash=&f15c4cd6db290ca73ea6f& class=&member_mention& data-editable=&true& data-title=&@刘允鹏& data-hovercard=&p$b$f15c4cd6db290ca73ea6f&&@刘允鹏&/a& 的建议,以及&a href=&/people/de76f52ed& data-hash=&de76f52ed& class=&member_mention& data-title=&@Jacob& data-editable=&true& data-hovercard=&p$b$de76f52ed&&@Jacob&/a&Jacob.Zeng 提供的 cliburn 的 Github 链接,我对原版的&a href=&/?target=https%3A///cliburn/Computational-statistics-with-Python& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&cliburn/Computational-statistics-with-Python&i class=&icon-external&&&/i&&/a& 进行了 fork,弄成了 &a href=&/?target=https%3A///Kivy-CN/Computational-statistics-with-Python-CN& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Kivy-CN/Computational-statistics-with-Python-CN&i class=&icon-external&&&/i&&/a& 等接下来就是抽空来逐步翻译了。&/p&&p&大家有兴趣的可以参考 &a href=&/p/& class=&internal&&这篇文章&/a& 来加入 Kivy-CN,来参与到翻译中来~&/p&
今天发现 Duke 大学的一个站点,网站地址在这里:里面有从 Python 基础入门到 PyCUDA 乃至一些数理统计相关的完整的学习链。简直太棒了! 每一部分的衔接都很自然,个人认为学习曲线非常平缓,而涉及的范围却相当广泛。…
已有帐号?
无法登录?
社交帐号登录
1136 人关注
413 条内容
6205 人关注
21775 条内容
17474 人关注
11040 条内容
385 人关注
702 条内容
283 人关注

我要回帖

更多关于 怎么连接不上手机 的文章

 

随机推荐