考虑C中的以下代码:
for(int i=0; i<10 && some_condition; ++i){
do_something();
}
Run Code Online (Sandbox Code Playgroud)
我想在Python中编写类似的东西.我能想到的最好的版本是:
i = 0
while some_condition and i<10:
do_something()
i+=1
Run Code Online (Sandbox Code Playgroud)
坦率地说,我不喜欢while模仿for循环的循环.这是由于忘记增加计数器变量的风险.另一个选择是增加这种风险:
for i in range(10):
if not some_condition: break
do_something()
Run Code Online (Sandbox Code Playgroud)
重要的澄清
some_condition 并不意味着在循环期间计算,而是指定是否首先启动循环
我指的是Python2.6
哪种款式首选?有没有更好的成语呢?
以下是Xorshift RNG 的基本实现(复制自维基百科):
uint32_t xor128(void) {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t;
t = x ^ (x << 11);
x = y; y = z; z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
Run Code Online (Sandbox Code Playgroud)
我理解这w是返回的值x,y并且z是状态("内存")变量.但是,我无法理解多个内存变量的目的.谁能解释我这一点?
另外,我尝试将上面的代码复制到Python:
class R2:
def __init__(self):
self.x = x = 123456789
self.y …Run Code Online (Sandbox Code Playgroud) 如何(如果有的话)在ggplot2中的轴刻度上显示两个替代单位?我想要实现的是这样的:

PyBrain是一个python库,提供(除其他外)易于使用的人工神经网络.
我无法使用pickle或cPickle正确地序列化/反序列化PyBrain网络.
请参阅以下示例:
from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
import cPickle as pickle
import numpy as np
#generate some data
np.random.seed(93939393)
data = SupervisedDataSet(2, 1)
for x in xrange(10):
y = x * 3
z = x + y + 0.2 * np.random.randn()
data.addSample((x, y), (z,))
#build a network and train it
net1 = buildNetwork( data.indim, 2, data.outdim )
trainer1 = BackpropTrainer(net1, dataset=data, verbose=True)
for i in xrange(4):
trainer1.trainEpochs(1)
print '\tvalue after %d …Run Code Online (Sandbox Code Playgroud) 可以按如下方式选择numpy数组中的元素
a = np.random.rand(100)
sel = a > 0.5 #select elements that are greater than 0.5
a[sel] = 0 #do something with the selection
b = np.array(list('abc abc abc'))
b[b==a] = 'A' #convert all the a's to A's
Run Code Online (Sandbox Code Playgroud)
np.where函数使用此属性来检索索引:
indices = np.where(a>0.9)
Run Code Online (Sandbox Code Playgroud)
我想做的是能够在这样的元素选择中使用正则表达式.例如,如果我想从b上面选择与[Aab]regexp 匹配的元素,我需要编写以下代码:
regexp = '[Ab]'
selection = np.array([bool(re.search(regexp, element)) for element in b])
Run Code Online (Sandbox Code Playgroud)
这对我来说太过分了.有没有更短更优雅的方式来做到这一点?
我需要logit和inverse logit函数logit(inv_logit(n)) == n.我使用numpy,这就是我所拥有的:
import numpy as np
def logit(p):
return np.log(p) - np.log(1 - p)
def inv_logit(p):
return np.exp(p) / (1 + np.exp(p))
Run Code Online (Sandbox Code Playgroud)
以下是价值观:
print logit(inv_logit(2))
2.0
print logit(inv_logit(10))
10.0
print logit(inv_logit(20))
20.000000018 #well, pretty close
print logit(inv_logit(50))
Warning: divide by zero encountered in log
inf
Run Code Online (Sandbox Code Playgroud)
现在让我们测试负数
print logit(inv_logit(-10))
-10.0
print logit(inv_logit(-20))
-20.0
print logit(inv_logit(-200))
-200.0
print logit(inv_logit(-500))
-500.0
print logit(inv_logit(-2000))
Warning: divide by zero encountered in log
-inf
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:实现这些功能的正确方法是什么,以便要求logit(inv_logit(n)) == n适用n于尽可能宽的范围(至少[-1e4; 1e4]? …
在python 2.6中,为什么以下行有效?
my_line = 'foo' 'bar'
Run Code Online (Sandbox Code Playgroud)
如果这是有效的,为什么不是以下内容:
my_list = 1 2
Run Code Online (Sandbox Code Playgroud)
第一个例子是字符串连接,但是,以下内容也无效(感谢上帝):
foo = 'foo'
bar = 'bar'
foo_bar = foo bar
Run Code Online (Sandbox Code Playgroud) 考虑以下会话.如何解释差异?我认为这a += b是一种语法糖(因而相当于)a = a + b.显然我错了.
>>> import numpy as np
>>> a = np.arange(24.).reshape(4,6)
>>> print a
[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10. 11.]
[ 12. 13. 14. 15. 16. 17.]
[ 18. 19. 20. 21. 22. 23.]]
>>> for line in a:
... line += 100
...
>>> print a #a has been changed
[[ 100. 101. 102. 103. 104. 105.]
[ 106. 107. 108. 109. …Run Code Online (Sandbox Code Playgroud) 以下python代码创建包含正态分布值的矩阵的热图
import numpy as np
from matplotlib import pylab as plt
np.random.seed(123) #make sure we all have same data
m = np.random.randn(200).reshape(10, 20)
plt.imshow(m, cmap='RdYlGn', interpolation='nearest')
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)
这是此代码的输出

我想通过"淡出"接近零的值来增强该图像的对比度.我可以通过使用原始数据的disigmoid缩放来轻松完成此操作,如下所示:
def disigmoidScaling(values, steepnessFactor=1, ref=None):
''' Sigmoid scaling in which values around a reference point are flattened
arround a reference point
Scaled value y is calculated as
y = sign(v - d)(1 - exp(-((x - d)/s)**2)))
where v is the original value, d is the referenc point and s is the
steepness …Run Code Online (Sandbox Code Playgroud)