所以在R中,当我有一个由4列组成的数据框时,调用它df并且我想通过组的总和来计算比率,我可以用这样的方式:
// generate data
df = data.frame(a=c(1,1,0,1,0),b=c(1,0,0,1,0),c=c(10,5,1,5,10),d=c(3,1,2,1,2));
| a b c d |
| 1 1 10 3 |
| 1 0 5 1 |
| 0 0 1 2 |
| 1 1 5 1 |
| 0 0 10 2 |
// compute sum product ratio
df = df%>% group_by(a,b) %>%
mutate(
ratio=c/sum(c*d)
);
| a b c d ratio |
| 1 1 10 3 0.286 |
| 1 1 5 1 0.143 |
| …Run Code Online (Sandbox Code Playgroud) 当我正在阅读这个答案/sf/answers/688898101/关于从字符串中删除重复项的问题时,我无法理解该index属性的含义
''.join(sorted(set(foo), key=foo.index))
Run Code Online (Sandbox Code Playgroud)
我做了这个例子运行:
foo = "Wicked long string"
>>> "".join(sorted(set(foo),key = foo.index))
'Wicked longstr'
>>> "".join(sorted(set(foo)))
' Wcdegiklnorst'
Run Code Online (Sandbox Code Playgroud)
这让我觉得它有助于保持角色的顺序.
我正在实现神经网络,并希望使用ReLU作为神经元的激活功能.此外,我正在使用SDG和反向传播来训练网络.我正在使用范式XOR问题测试神经网络,到目前为止,如果我使用逻辑函数或双曲正切作为激活函数,它会正确地对新样本进行分类.
我一直在阅读使用Leaky ReLU作为激活函数的好处,并在Python中实现它,如下所示:
def relu(data, epsilon=0.1):
return np.maximum(epsilon * data, data)
Run Code Online (Sandbox Code Playgroud)
NumPynp的名称在哪里.相关的衍生物实现如下:
def relu_prime(data, epsilon=0.1):
if 1. * np.all(epsilon < data):
return 1
return epsilon
Run Code Online (Sandbox Code Playgroud)
使用此功能作为激活我得到不正确的结果.例如:
输入= [0,0] - >输出= [0.43951457]
输入= [0,1] - >输出= [0.46252925]
输入= [1,0] - >输出= [0.34939594]
输入= [1,1] - >输出= [0.37241062]
可以看出,输出与预期的XOR输出大不相同.所以问题是,有没有特别考虑使用ReLU作为激活功能?
请不要犹豫,向我询问更多背景信息或代码.提前致谢.
编辑:导数中有一个错误,因为它只返回一个浮点值,而不是NumPy数组.正确的代码应该是:
def relu_prime(data, epsilon=0.1):
gradients = 1. * (data > epsilon)
gradients[gradients == 0] = epsilon
return gradients
Run Code Online (Sandbox Code Playgroud) python numpy machine-learning neural-network activation-function
我正在尝试测量在Python中运行一组指令所需的时间,但我不想写下这样的内容:
start = time.clock()
...
<lines of code>
...
time_elapsed = time.clock() - start
Run Code Online (Sandbox Code Playgroud)
相反,我想知道是否有一种方法可以将指令块作为参数发送给返回已用时间的函数,如
time_elapsed = time_it_takes(<lines of code>)
Run Code Online (Sandbox Code Playgroud)
这种方法的实现可能是这样的
def time_it_takes(<lines of code>):
start = time.clock()
result = <lines of code>
return (result, time.clock() - start)
Run Code Online (Sandbox Code Playgroud)
有人知道我能否做到这一点?提前致谢.