在过去的几天里,我一直在努力改进python函数的运行时间,这需要使用余数函数(%)等等.我的主要测试案例是超过80,000个元素的numpy数组(单调增加),有10000次迭代,尽管我也尝试过各种其他大小.
最终我达到了剩余功能是一个主要瓶颈的地步,并尝试了各种解决方案.这是我在运行以下代码时发现的行为:
import numpy as np
import time
a = np.random.rand(80000)
a = np.cumsum(a)
d = 3
start_time1 = time.time()
for i in range(10000):
b = a % d
d += 0.001
end_time1 = time.time()
d = 3
start_time2 = time.time()
for i in range(10000):
b = a - (d * np.floor(a / d))
d += 0.001
end_time2 = time.time()
print((end_time1 - start_time1) / 10000)
print((end_time2 - start_time2) / 10000)
Run Code Online (Sandbox Code Playgroud)
输出是:
0.0031344462633132934
0.00022937238216400147
Run Code Online (Sandbox Code Playgroud)
当将数组大小增加到800,000时:
0.014903099656105041
0.010498356819152833
Run Code Online (Sandbox Code Playgroud)
(对于这篇文章,我只为实际输出运行了一次代码,同时试图理解这个问题我已经得到了这些结果.)
虽然这解决了我的运行时问题 - 但我很难理解为什么.我错过了什么吗?我能想到的唯一区别是额外函数调用的开销,但第一种情况非常极端(并且运行时间的1.5倍也不够好),如果是这种情况,我会认为存在该 …
如何从Windows中的Git Bash打开Sublime文本?我尝试在〜/ .bashrc文件中添加别名,但没有任何效果.我一直在寻找一些非常容易的东西但我在互联网上找不到.
如何在 Tensorflow.js 中改变张量的值?例如,如果我有一个这样创建的张量:
const a = tf.tensor1d([1,2,3,4])
如何更改张量的第三个元素的值?我知道张量是不可变的,而变量是可变的。
这样做:const a = tf.variable(tf.tensor1d([1,2,3,4]))似乎并没有解决问题。我不能做:
const a = a[0].assign(5)
我可以像这样在python tensorflow 中做到这一点:
a = tf.Variable([1,2,3,4])
a = a[0].assign(100)
with tf.Session() as sess:
sess.run(tf.global_variables_iniliazer())
print sess.run(a)
这输出 [100, 2,3,4]
我正在尝试将 softmax 函数应用于 numpy 数组。但我没有得到想要的结果。这是我试过的代码:
import numpy as np
x = np.array([[1001,1002],[3,4]])
softmax = np.exp(x - np.max(x))/(np.sum(np.exp(x - np.max(x)))
print softmax
Run Code Online (Sandbox Code Playgroud)
我认为x - np.max(x)代码没有减去每一行的最大值。需要从 x 中减去最大值以防止非常大的数字。
这应该输出
np.array([
[0.26894142, 0.73105858],
[0.26894142, 0.73105858]])
Run Code Online (Sandbox Code Playgroud)
但我得到:
np.array([
[0.26894142, 0.73105858],
[0, 0]])
Run Code Online (Sandbox Code Playgroud) 我试图将 laravel-forge 中服务器的 SSH 密钥添加到 bitbucket。ssh 密钥通常位于~/.ssh/id_rsa.publaravel-forge 终端内。Bitbucket 不允许我添加这个。通常通过单击“设置”下的“访问密钥”将其添加到 bitbucket 中。错误消息是:that key has already been added by a user。但我找不到团队中的其他用户添加了它。bitbucket 网站无法显示哪个用户添加了它。
指定索引后,如何从Tensorflow.js中的张量中获取值?
我正在尝试根据两列的值对 Pandas 数据框进行子集化。我试过这个代码:
df[df['gold']>0, df['silver']>0, df['bronze']>0]但这没有用。
我也试过:
df[(df['gold']>0 and df['silver']>0)。这也不起作用。我收到一条错误消息:
ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
你有什么建议?
当我pyspark在控制台中运行命令时,我试图启动 jupyter 笔记本。当我现在输入它时,它只会在控制台中启动和交互式 shell。但是,这不方便键入长代码行。有没有办法将 jupyter notebook 连接到 pyspark shell?谢谢。
我已经在 python virtualenv 中安装了 pyspark。我还在virtualenv 中安装了新发布的 jupyterlab http://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html 。我无法以 SparkContext 变量可用的方式在 jupyter-notebook 中触发 pyspark。
python ×3
numpy ×2
pyspark ×2
tensor ×2
apache-spark ×1
arrays ×1
bitbucket ×1
dataframe ×1
git ×1
git-bash ×1
indexing ×1
jupyter-lab ×1
mutate ×1
pandas ×1
performance ×1
pyspark-sql ×1
softmax ×1
sublimetext2 ×1
subset ×1
tensorflow ×1
virtualenv ×1
windows-7 ×1