我注意到有复杂无穷大的有趣结果.
In [1]: import numpy as np
In [2]: np.isinf(1j * np.inf)
Out[2]: True
In [3]: np.isinf(1 * 1j * np.inf)
Out[3]: True
In [4]: np.isinf(1j * np.inf * 1)
Out[4]: False
Run Code Online (Sandbox Code Playgroud)
这是nan相关的.但最终的结果是奇怪的.
这是一个numpy bug吗?我应该采取哪些不同的做法?
numpy似乎不是复杂无穷大的好朋友
虽然我们可以评估:
In[2]: import numpy as np
In[3]: np.mean([1, 2, np.inf])
Out[3]: inf
Run Code Online (Sandbox Code Playgroud)
以下结果更加繁琐:
In[4]: np.mean([1 + 0j, 2 + 0j, np.inf + 0j])
Out[4]: (inf+nan*j)
...\_methods.py:80: RuntimeWarning: invalid value encountered in cdouble_scalars
ret = ret.dtype.type(ret / rcount)
Run Code Online (Sandbox Code Playgroud)
我不确定想象中的部分对我有意义.但如果我错了,请发表评论.
有关与numpy中复杂无穷大相互作用的任何见解?
我正在阅读卷积神经网络教程.我希望在训练模型后可视化每层的输出.例如,在函数"evaluate_lenet5"中,我想将一个实例(它是一个图像)传递给网络,并查看每个图层的输出以及为输入设置训练神经网络的类.我认为在每个图层的图像和权重向量上做点积可能很容易,但它根本不起作用.
我有每个图层的对象:
# Reshape matrix of rasterized images of shape (batch_size, 28 * 28)
# to a 4D tensor, compatible with our LeNetConvPoolLayer
# (28, 28) is the size of MNIST images.
layer0_input = x.reshape((batch_size, 1, 28, 28))
# Construct the first convolutional pooling layer:
# filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24)
# maxpooling reduces this further to (24/2, 24/2) = (12, 12)
# 4D output tensor is thus of shape (batch_size, …Run Code Online (Sandbox Code Playgroud) 我想这与整数类型溢出有关。但是为什么奇怪的错误信息呢?
In [36]: import numpy as np
In [37]: np.log2(2**63)
Out[37]: 63.0
In [38]: np.log2(2**64)
Traceback (most recent call last):
File "<ipython-input-38-f1b1c814f08c>", line 1, in <module>
np.log2(2**64)
AttributeError: 'int' object has no attribute 'log2'
Run Code Online (Sandbox Code Playgroud)
为什么说“没有属性”而不是“整数溢出”?还是我误解了错误的原因?我可以看到此错误消息如何在帮助查找问题所在时非常混乱。
以下代码无法按预期工作:
import matplotlib.pyplot as plt
plt.plot(range(100000), '.')
plt.draw()
ax = plt.gca()
lblx = ax.get_xticklabels()
lblx[1]._text = 'hello!'
ax.set_xticklabels(lblx)
plt.draw()
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想原因是自动xticklabels在get_xticklabels()被调用时没有时间完全创建.事实上,通过添加plt.pause(1)
import matplotlib.pyplot as plt
plt.plot(range(100000), '.')
plt.draw()
plt.pause(1)
ax = plt.gca()
lblx = ax.get_xticklabels()
lblx[1]._text = 'hello!'
ax.set_xticklabels(lblx)
plt.draw()
plt.show()
Run Code Online (Sandbox Code Playgroud)
会给出预期的
我对这种状态不满意(不得不手动插入延迟).我主要担心的是:我怎么知道需要等多少时间?当然这取决于数字元素的数量,机器强度等.
所以我的问题是:是否有一些标志知道matplotlib已经完成了所有元素的绘制?或者有更好的方法来做我正在做的事情?
令人惊讶的 ipython 魔法%timeit错误:
In[1]: a = 2
In[2]: %timeit a = 2 * a
Traceback (most recent call last):
File "...\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-97-6f70919654d1>", line 1, in <module>
get_ipython().run_line_magic('timeit', 'a = 2 * a')
File "...\site-packages\IPython\core\interactiveshell.py", line 2314, in run_line_magic
result = fn(*args, **kwargs)
File "<...\site-packages\decorator.py:decorator-gen-61>", line 2, in timeit
File "...\site-packages\IPython\core\magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "...\site-packages\IPython\core\magics\execution.py", line 1158, in timeit
time_number = …Run Code Online (Sandbox Code Playgroud) 这让我感到惊讶......为了说明我使用这个小代码来计算1M随机数的平均值和中位数:
import numpy as np
import statistics as st
import time
listofrandnum = np.random.rand(1000000,)
t = time.time()
print('mean is:', st.mean(listofrandnum))
print('time to calc mean:', time.time()-t)
print('\n')
t = time.time()
print('median is:', st.median(listofrandnum))
print('time to calc median:', time.time()-t)
Run Code Online (Sandbox Code Playgroud)
结果如下:
mean is: 0.499866595037
time to calc mean: 2.0767598152160645
median is: 0.499721597395
time to calc median: 0.9687695503234863
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么平均值比中位数慢?中位数需要一些排序算法(即比较),而均值需要求和.总和是否比比较慢?
我将非常感谢您对此的见解.
刚刚__length_hint__()从PEP 424(https://www.python.org/dev/peps/pep-0424/)遇到了这个非常棒的迭代器方法.哇!一种获取迭代器长度而不会耗尽迭代器的方法.
我的问题:
编辑: BTW,我看到__length__hint__()从当前位置到结束的计数.即部分消耗的迭代器将报告剩余长度.有趣.
我已经看到了建议的解决方案和解决方法,但是找不到关于不允许在迭代过程中更改集的选择的解释。你能帮我理解为什么可以吗
In [1]: l = [1]
In [2]: for i in l:
l.append(2*i)
if len(l)>10:
break
In [3]: l
Out[3]: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
Run Code Online (Sandbox Code Playgroud)
虽然这不行
In [4]: l = {1}
In [5]: for i in l:
l.add(2*i)
if len(l)>10:
break
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-5-b5bdff4a382b> in <module>()
----> 1 for i in l:
2 l.add(2*i)
3 if len(l)>10:
4 break
5
RuntimeError: Set changed size during iteration
Run Code Online (Sandbox Code Playgroud)
在迭代时更改集合有什么不好?
我知道集合中的顺序未定义,因此next …
当使用无限生成器时,我可以if用来防止项目进入列表,但不是为了打破循环.
这实际上会崩溃并挂起.甚至KeyboardInterrupt不能救我(为什么BTW?)
import itertools
a = [x for x in itertools.count() if x<10]
Run Code Online (Sandbox Code Playgroud)
如果我使用自己的无限发电机
def mygen():
i=0
while True:
yield i
i +=1
Run Code Online (Sandbox Code Playgroud)
这将持续到 KeyboardInterrupt
a = [x for x in mygen() if x<10]
Run Code Online (Sandbox Code Playgroud)
现在,显然对于一个适当的for循环,我们可以在满足条件时中断
a = []
for x in itertools.count():
a.append(x)
if x>9:
break
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:有没有办法打破列表理解?
python ×9
numpy ×5
python-3.x ×3
generator ×1
infinite ×1
ipython ×1
matplotlib ×1
mean ×1
python-2.7 ×1
statistics ×1
theano ×1