def plot_grid_scores(self, x, hue=None, row=None, col=None, col_wrap=None,
**kwargs):
def none_if_none(x):
return None if x == 'None' else x
if has_widgets:
choices = ['None'] + list(unpack_grid_scores(self.model)
.columns.drop(['mean_', 'std_']))
@interact(x=choices, hue=choices, row=choices, col=choices)
def wrapper(x=x, hue=None, row=None, col=None):
return plot_grid_scores(self.model,
none_if_none(x),
'mean_',
hue=none_if_none(hue),
row=none_if_none(row),
col=none_if_none(col),
col_wrap=none_if_none(col_wrap),
**kwargs)
return wrapper
else:
return plot_grid_scores(self.model, x, 'mean_', hue=hue, row=row,
col=col, col_wrap=col_wrap, **kwargs)
python类interact()的实例源码
def __init__(self, loopFunc, **kw):
self.thread = None
self.loopFunc = loopFunc
ipywidgets.interact(self.toggler, x=ipywidgets.ToggleButton(**kw))
def __init__(self, gimbal):
self.gimbal = gimbal
ipywidgets.interact(self.fn, x=ipywidgets.ToggleButton(description='Motor Enable'))
def __init__(self, gimbal, number, axes=range(3), min=-0x8000, max=0x7fff, step=1):
self.gimbal = gimbal
self.number = number
self.axes = axes
self.widgets = [None] * 3
ThreadToggle(self._update, description='Refresh param %02x' % number)
for t in self.axes:
v = self.gimbal.getParam(number=number, target=t)
self.widgets[t] = ipywidgets.IntSlider(description='Param %02x t=%d' % (self.number, t),
value=v, min=min, max=max, step=step,layout=dict(width='100%'))
ipywidgets.interact(self._set, x=self.widgets[t], target=ipywidgets.fixed(t))
def __init__(self, gimbal):
self.gimbal = gimbal
self.controlPacket = None
xw = ipywidgets.IntSlider(value=0, min=-0x8000, max=0x7fff, step=1, layout=dict(width='100%'))
yw = ipywidgets.IntSlider(value=0, min=-0x8000, max=0x7fff, step=1, layout=dict(width='100%'))
zw = ipywidgets.IntSlider(value=0, min=-0x8000, max=0x7fff, step=1, layout=dict(width='100%'))
mw = ipywidgets.IntSlider(value=1, min=0, max=255, step=1, layout=dict(width='100%'))
ipywidgets.interact(self.setFn, x=xw, y=yw, z=zw, m=mw)
ThreadToggle(self.loopFn, description='Controller thread')
self.rate = ipywidgets.IntSlider(description='Update rate',
value=25, min=1, max=400, step=1, layout=dict(width='100%'))
display(self.rate)
def type_and_vis(self):
def input_box(tweet, grads, activations, over_words, over_units):
self.vis_activation([tweet], grads, activations, over_words, over_units)
return interact(input_box, tweet='This is a great tool', grads=False, activations=True,
over_words=True, over_units=False)
def plot(self, vis_func, img_path, label_list, figsize):
img = utils.load_img(img_path, target_size=self.img_shape_)
img = img[:,:,:3]
predictions = self.model_.predict(img2tensor(img, self.img_shape_))
predictions = softmax(predictions)
if not label_list:
prediction_text = decode_predictions(predictions)[0]
def _plot(label_id):
label_id = int(label_id)
text_label = get_pred_text_label(label_id)
label_proba = np.round(predictions[0,label_id], 4)
heatmap = vis_func(img, label_id)
for p in prediction_text:
print(p[1:])
plt.figure(figsize=figsize)
plt.subplot(1,2,1)
plt.title('label:%s\nscore:%s'%(text_label,label_proba))
plt.imshow(overlay(heatmap, img))
plt.subplot(1,2,2)
plt.imshow(img)
plt.show()
else:
def _plot(label_id):
print(pd.DataFrame(predictions, columns=label_list))
label_id = int(label_id)
text_label = label_list[label_id]
label_proba = np.round(predictions[0,label_id], 4)
heatmap = vis_func(img,label_id)
plt.figure(figsize=figsize)
plt.subplot(1,2,1)
plt.title('label:%s\nscore:%s'%(text_label,label_proba))
plt.imshow(overlay(heatmap, img))
plt.subplot(1,2,2)
plt.imshow(img)
plt.show()
return interact(_plot, label_id='1')
def browse(self, figsize=(16,10), labels=None):
def plot(layer_id, filter_id):
filepath = '{}/{}/{}/img.jpg'.format(self.save_dir_,
layer_id, filter_id)
img = plt.imread(filepath)
plt.figure(figsize=figsize)
if labels:
plt.title('Label: {}'.format(labels[int(filter_id)]))
plt.imshow(img)
plt.show()
return interact(plot, layer_id='1',filter_id='0')
def browse_layer(self, batch_size=25, cols=5):
def plot(layer_id, batch_id):
plt.figure(figsize=(14,20))
all_files = sorted(os.listdir('{}/{}'.format(self.save_dir_, layer_id)))
batch_id = int(batch_id)
img_list, label_list = [],[]
for f in all_files[batch_id*batch_size:(batch_id+1)*batch_size]:
img_path = os.path.join(self.save_dir_, layer_id, f, 'img.jpg')
img = plt.imread(img_path)
img_list.append(img)
label_list.append(f)
plot_list(img_list, label_list, cols_nr=cols)
return interact(plot, layer_id='17',batch_id='6')