IPython笔记本互动功能:如何保存更新后的功能参数 [英] IPython notebook interactive function: how to save the updated function parameters
问题描述
我在Ipython笔记本中编写了下面的代码来生成一个由参数a控制的sigmoid函数,该参数定义了sigmoid中心的位置,b定义了其宽度:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x,a,b):
#sigmoid函数,参数a = center; b =宽度
s = 1 /(1 + np.exp( - (xa)/ b))
返回100.0 *(s-min(s))/(max(s)-min(s ))#将sigmoid归一化为0-100
x = np.linspace(0,10,256)
sigm = sigmoid(x,a = 5,b = 1)
fig = plt.figure(figsize =(24,6))
ax1 = fig.add_subplot(2,1,1)
ax1.set_xticks([])
ax1.set_xticks([])
plt.plot(x,sigm,lw = 2,color ='black')
plt.xlim(x.min(),x.max())
$ c
我想为参数a和b添加交互性,所以我重新编写了如下函数:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython.html.widgets import interactive
from IPython.display导入显示
def sigmoid_demo(a = 5,b = 1):
x = np.linspace(0,10,256)
s = 1 /(1 (s-min(s))/(max(s)-min(s))+ np.exp( - (xa)/(b + 0.1)))#+0.1以避免被0除去。 s))#将sigmoid标准化为0-100
fig = plt.figure(figsize =(24,6))
ax1 = fig.add_ subplot(2,1,1)
ax1.set_xticks([])
ax1.set_yticks([])
plt.plot(x,sn,lw = 2,color ='black ')
plt.xlim(x.min(),x.max())
$ b $ = widgets.interactive(sigmoid_demo,a = 5,b = 1)
显示(w)
是否可以将修改后的sigmoid函数参数a和b动态保存为变量是由滑块修改的,所以它们可以在另一个子图或单独的图中使用,并进行进一步的计算。?
我经历了所有交互式小部件的例子I可以找到但没有看到任何指向那个方向的东西(这些小部件相当新,所以它们的文档看起来不像其他功能那么广泛)。任何建议的方向是正确的。赞赏。
解决方案您想手动创建浮动窗口小部件:$ b $来自IPython.html.widgets的b
import FloatSlider
#与之前一样
F1 = FloatSlider(value = 5,min = 0,max = 10)
w = interactive(sigmoid_demo,a = F1,b = 7)
display(w)
然后您可以随时访问
F1.value
,它将成为当前滑块值。
您也可以遍历
w.widgets
但这很痛苦。I wrote the code below in Ipython notebook to generate a sigmoid function controlled by parameters a which defines the position of the sigmoid center, and b which defines its width:
%matplotlib inline import numpy as np import matplotlib.pyplot as plt def sigmoid(x,a,b): #sigmoid function with parameters a = center; b = width s= 1/(1+np.exp(-(x-a)/b)) return 100.0*(s-min(s))/(max(s)-min(s)) # normalize sigmoid to 0-100 x = np.linspace(0,10,256) sigm = sigmoid(x, a=5, b=1) fig = plt.figure(figsize=(24,6)) ax1 = fig.add_subplot(2, 1, 1) ax1.set_xticks([]) ax1.set_xticks([]) plt.plot(x,sigm,lw=2,color='black') plt.xlim(x.min(), x.max())
I wanted to add interactivity for parameters a and b so I re-wrote the function as below:
%matplotlib inline import numpy as np import matplotlib.pyplot as plt from IPython.html.widgets import interactive from IPython.display import display def sigmoid_demo(a=5,b=1): x = np.linspace(0,10,256) s = 1/(1+np.exp(-(x-a)/(b+0.1))) # +0.1 to avoid dividing by 0 sn = 100.0*(s-min(s))/(max(s)-min(s)) # normalize sigmoid to 0-100 fig = plt.figure(figsize=(24,6)) ax1 = fig.add_subplot(2, 1, 1) ax1.set_xticks([]) ax1.set_yticks([]) plt.plot(x,sn,lw=2,color='black') plt.xlim(x.min(), x.max()) w=widgets.interactive(sigmoid_demo,a=5,b=1) display(w)
Is it possible to save the modified sigmoid function parameters a and b to variables dynamically as they are modified by the sliders, so they can be used in another subplot or separate plot, and do further calculations.?
I've gone through all examples of interactive widgets I could find but have not see anything pointing in that direction (these widgets are fairly new, so documentation for them does not seem as extensive as for other features). Any suggestions in the right direction is appreciated.
解决方案You want to create the float widget manually:
from IPython.html.widgets import FloatSlider # same as before F1 = FloatSlider(value=5, min=0, max=10) w=interactive(sigmoid_demo, a=F1, b=7) display(w)
Then you can access
F1.value
any time after and it will be the current slider value.You can also loop through
w.widgets
but that's painful.这篇关于IPython笔记本互动功能:如何保存更新后的功能参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!