一、关于numpy的使用:
1.随机取值
In [2]: np.random.rand(3,2) Out[2]: array([[ 0.04287046, 0.68212741], [ 0.25322512, 0.50287344], [ 0.75530125, 0.55308281]])
2.randn 返回具有标准正态分布的样本
In [4]: np.random.randn(3,2) Out[4]: array([[-2.26840223, -0.24799291], [-0.81557012, 0.92537801], [ 0.04456033, -0.03864446]])
3.返回随机整数
In [8]: np.random.randint(2, size=10) Out[8]: array([0, 1, 1, 0, 0, 1, 0, 0, 0, 0])
In [9]: np.random.randint(1, size=5) Out[9]: array([0, 0, 0, 0, 0])
In [10]: np.random.randint(9, size=(4,5)) Out[10]: array([[2, 5, 3, 0, 0], [5, 5, 8, 0, 5], [8, 3, 3, 8, 1], [5, 5, 7, 4, 3]])
4.([size]) 返回半开区间内的一个随机浮点数 [0.0, 1.0)
In [12]: np.random.random((3,5)) Out[12]: array([[ 0.9406723 , 0.88534251, 0.48993398, 0.04959344, 0.69200616], [ 0.8317934 , 0.02332241, 0.07543021, 0.26467834, 0.29640229], [ 0.79139576, 0.72745639, 0.04238559, 0.15145797, 0.40844399]])
5.从给定的数字生成一个随机样本
#从np.arange(8)中选取大小问为3的样本 In [13]: np.random.choice(8,3) Out[13]: array([0, 2, 5])
In [14]: a = [3,5,6,8,2,6,8] In [15]: np.random.choice(a,1) Out[15]: array([3]) In [16]: np.random.choice(a,1) Out[16]: array([3]) In [17]: np.random.choice(a,3) Out[17]: array([2, 6, 8]) In [18]: np.random.choice(a,4) Out[18]: array([6, 3, 2, 8])
6.(x) 打乱x的顺序
In [22]: arr = np.arange(9) In [23]: np.random.shuffle(arr) In [24]: arr Out[24]: array([6, 0, 2, 7, 1, 3, 5, 8, 4])
更多详情请参考:
二、tf.()中的参数详情
distance = tf.reduce_sum(tf.abs(tf.add(x_train,tf.negative(x_test))),reduction_indices=1)
@ >
() 是总和。由于求和的对象是,它是沿某些维度的求和。指要汇总的维度。
详细解释见下图:
三、评估函数
1.中eval函数的作用是将字符串放入一个有效的表达式中,并返回结果。
直接代码示例:
In [1]: str1 = '1+4' In [2]: str2 = '[1,2,3,4,5,6,7]' In [3]: str3 = '[[1,],[2],[3,]]' In [6]: type(eval(str1)) Out[6]: int In [7]: type(eval(str2)) Out[7]: list In [8]: type(eval(str3)) Out[8]: list
2.中应用的.eval()函数:
print("Accuracy:",accuracy.eval({x:mnist.test.images,y:mnist.test.labels}))
.eval() 是 .run() 的替代品,以避免变量持有会话。
常见情况如下:
mat = tf.matmul(a,b) print(mat.eval())
四、格式化字符串()
用法:
它将传统的 % 方法替换为 {} 和:
1、使用位置参数
关键点:位置参数从下面的例子可以看出它不受顺序约束,可以是{},只要里面有对应的参数值,参数索引从0开始,传入的位置参数列表可以是*list
>>> li = [‘hoho’,18]
>>> ‘我叫{},年龄{}’.(‘hoho’,18)
‘我叫hoho,18岁’
>>> ‘我叫{1},年龄{0}’。(10,’hoho’)
‘我叫hoho,10岁’
>>> ‘我叫{1},年龄{0} {1}’.(10,’hoho ‘)
‘我叫hoho,10岁hoho’
>>> ‘我叫{},年龄{}’.(*li)
‘我叫hoho,18岁’
2、使用关键字参数
关键点:关键字参数值必须正确可以使用字典作为关键字参数传入值,在字典前加**即可
>>> hash = {'name':'hoho','age':18} >>> 'my name is {name},age is {age}'.format(name='hoho',age=19) 'my name is hoho,age is 19' >>> 'my name is {name},age is {age}'.format(**hash) 'my name is hoho,age is 18'
3、填充和格式化
:[填充字符][对齐][宽度]
>>> '{0:*>10}'.format(10) ##右对齐 '********10' >>> '{0:*>> '{0:*^10}'.format(10) ##居中对齐 '****10****'
4、精度和基数
>>> '{0:.2f}'.format(1/3) '0.33' >>> '{0:b}'.format(10) #二进制 '1010' >>> '{0:o}'.format(10) #八进制 '12' >>> '{0:x}'.format(10) #16进制 'a' >>> '{:,}'.format(12369132698) #千分位格式化 '12,369,132,698'
5、使用索引
>>> li ['hoho', 18] >>> 'name is {0[0]} age is {0[1]}'.format(li) 'name is hoho age is 18
© 版权声明
本站下载的源码均来自公开网络收集转发二次开发而来,
若侵犯了您的合法权益,请来信通知我们1413333033@qq.com,
我们会及时删除,给您带来的不便,我们深表歉意。
下载用户仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,访问及下载者下载默认同意本站声明的免责申明,请合理使用切勿商用。
THE END
暂无评论内容