import numpy
data = numpy.random.randint(0, 10, (6,8))
test = set(numpy.random.randint(0, 10, 5))
Run Code Online (Sandbox Code Playgroud)
我想要一个表达式,其值是一个布尔数组,具有相同的形状data(或者,至少可以重新整形为相同的形状),告诉我相应的术语data是否在set.
例如,如果我想知道哪些元素data严格小于6,我可以使用单个矢量化表达式,
a = data < 6
Run Code Online (Sandbox Code Playgroud)
计算6x8布尔ndarray.相反,当我尝试一个明显等效的布尔表达式时
b = data in test
Run Code Online (Sandbox Code Playgroud)
我得到的是一个例外:
TypeError: unhashable type: 'numpy.ndarray'
Run Code Online (Sandbox Code Playgroud)
编辑:下面的可能性#4给出了错误的结果,感谢hpaulj和Divakar让我走上正轨.
在这里我比较四种不同的可能性
np.in1d(data, np.hstack(test)).np.in1d(data, np.array(list(test))).np.in1d(data, test).这是Ipython会话,略微编辑以避免空行
In [1]: import numpy as np
In [2]: nr, nc = 100, 100
In [3]: top = …Run Code Online (Sandbox Code Playgroud) 我有不同的 ANCIENT 模板来将 Jupyter 笔记本转换为 PDF,但从 5.0 版开始jupyter-nbconvert(至少,这是 Debian 上的二进制文件的名称)默认使用 XeLaTeX,并且碰巧,其中一些模板在以下情况下很好与 XeLaTeX 一起使用,其他人仍然需要 pdfLaTeX。
我可以使用jupyter-nbconvert --to latex --template old_template MyNotebook并稍后pdflatex从 CLI运行,这是我迄今为止所做的,但我想知道是否有办法告诉 Jupyter 我想使用pdflatex.
我知道 NBConvert 有一个配置选项,
PDFExporter.latex_command : List
Default: ['xelatex', '{filename}', '-quiet']
Shell command used to compile latex.
Run Code Online (Sandbox Code Playgroud)
但我更喜欢命令行上的某些内容,以便我可以定义别名以针对不同的用例使用不同的 TeX 引擎。
我正在尝试在图形中添加颜色条,但我不明白它是如何工作的.问题是我通过以下方式制作自己的颜色代码:
x = np.arange(11)
ys = [i+x+(i*x)**2 for i in range(11)]
colors = cm.rainbow(np.linspace(0, 1, len(ys)))
Run Code Online (Sandbox Code Playgroud)
并colors[i]会给我一个新的颜色.然后我使用(自制)函数来选择相关数据并相应地绘制它们.这看起来像这样:
function(x,y,concentration,temperature,1,37,colors[0])
function(x,y,concentration,temperature,2,37,colors[1])
# etc
Run Code Online (Sandbox Code Playgroud)
现在我想在颜色条中添加颜色,我可以更改标签.我该怎么做呢?
我已经看过几个例子,你将所有数据绘制成一个数组,带有自动颜色条,但是在这里我逐个绘制数据(通过使用函数来选择相关数据).
编辑:
函数(x,y,浓度,温度,1,37,颜色[0])看起来像这样(简化):
def function(x,y,c,T,condition1,condition2,colors):
import matplotlib.pyplot as plt
i=0
for element in c:
if element == condition1:
if T[i]==condition2:
plt.plot(x,y,color=colors,linewidth=2)
i=i+1
return
Run Code Online (Sandbox Code Playgroud) 本质上,我试图在轴位置x = 1处绘制具有-.k样式的垂直线,但是,似乎无法在axvline参数内使用此kwarg:
pyplot.axvline(x=1,ymin=0,ymax=0.35, '-.k')
Run Code Online (Sandbox Code Playgroud)
返回错误:
File "<ipython-input-70-04f296edd639>", line 7
pyplot.axvline(x=1,ymin=0,ymax=0.35, '-.k')
SyntaxError: non-keyword arg after keyword arg
Run Code Online (Sandbox Code Playgroud)
因此,有人可以建议我这种情况下正确的扭曲是什么...
我写的时候:
lines = (line.strip() for line in open('a_file'))
Run Code Online (Sandbox Code Playgroud)
文件是立即打开还是仅在我开始使用生成器表达式时才访问文件系统?
您如何将应用程序配置为具有以下功能:
将api端点定义为 app.add_route('/v1/tablets', TabletsCollection())
而且仍然可以像这样使用QueryParams https://example.com/api/v1/tablets?limit=12&offset=50&q=tablet name
集合有两种方法on_get来检索整个列表并使用过滤器和on_post创建单个记录。
我已经在网上搜索了一段时间,如何获得query_string和正确解析的参数?
我在zsh shell中使用IPython并且我在定义具有多行的函数时遇到了麻烦.
例如,在原始的Python shell中,我可以像这样定义一个函数f:
>>> def f(x):
... a = x + 1
... return a
...
>>>
Run Code Online (Sandbox Code Playgroud)
当我在IPython中完成它时,当'a = x + 1'行结束时,defination立即结束.它看起来像这样:
In [4]: def f(x):
...: a = x + 1
In [5]:
Run Code Online (Sandbox Code Playgroud)
如何通过更改某些配置或安装其他版本的IPy来修复它?
我的Python和IPython版本:
Python 3.5.6 |Anaconda, Inc.|
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
Run Code Online (Sandbox Code Playgroud) 我正在 python 中创建一个函数,它允许我创建两个并行图,并且它们共享两个轴:
\ndef PlotManager(data1,data2,fig):\n f, (ax1, ax2) = fig.subplots(2, 1, sharey=True,sharex=True)\n\n #Plot1 sopra\n x_axis = data1.index\n #Plot and shade the area between the upperband and the lower band grey\n ax1.fill_between(x_axis,data1[\'Upper\'],data1[\'Lower\'], color = \'grey\', alpha= 0.5)\n #Plot the closing price and the moving average\n ax1.plot(x_axis,data1[\'Close\'],color = \'gold\',lw = 3,label = \'Close Price\', alpha= 0.5)\n ax1.plot(x_axis,data1[\'SMA\'],color = \'blue\',lw = 3,label = \'Simple Moving Average\', alpha= 0.5)\n ax1.scatter(x_axis,data1[\'Buy\'],color="green", lw=3,label="Buy",marker = "^", alpha=1)\n ax1.scatter(x_axis,data1[\'Sell\'],color="red", lw=3,label="Sell",marker = "v", alpha = 1)\n #Set the …Run Code Online (Sandbox Code Playgroud) 我有一个 2 暗的 numpy 数组,从一个起点到每个端点的距离,我想对距离进行排序,但要做到这一点,我必须给距离一个索引,这样我就不会失去对端点的引用。
如何添加索引?最好没有循环
我已经使用numpy.expand_dims将数组扩展了 1 dim,但我不知道如何附加索引。
所以我目前的做法是:
indexArray = [1,2,3]
distances = np.array([[0.3, 0.5, 0.2], [0.7, 0.1, 0.5], [0.2, 0.3, 0.8]])
distances = np.expand_dims(distances, axis=2)
Run Code Online (Sandbox Code Playgroud)
现在距离看起来像:
[[[0.3], [0.5], [0.2]],
[[0.7], [0.1], [0.5]],
[[0.2], [0.3], [0.8]]]
Run Code Online (Sandbox Code Playgroud)
现在我想附加 indexArray 使距离数组看起来像:
[[[1, 0.3], [2, 0.5], [3, 0.2]],
[[1, 0.7], [2, 0.1], [3, 0.5]],
[[1, 0.2], [2, 0.3], [3, 0.8]]]
Run Code Online (Sandbox Code Playgroud) 我刚刚安装了最新版本的 Matplotlib (3.4.1)。当我在以下行中使用一些代码时
ax = fig.gca(projection='3d')
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
Run Code Online (Sandbox Code Playgroud)Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
我如何解决这个问题,以便我的代码不发出此警告?
python ×9
matplotlib ×4
numpy ×2
colorbar ×1
generator ×1
ipython ×1
jupyter ×1
nbconvert ×1
query-string ×1
shell ×1