Example #1
0
def tsne_html(datastore, d, spread=50, tsne=True):
    X, COLOR, IMG, dw, dh, xs, ys = datastore
    model = TSNE(n_components=2, random_state=234)
    model.perplexity = d['prep']
    model.learning_rate = d['lr']
    if tsne:
        np.set_printoptions(suppress=False)
        out = model.fit_transform(X)
        # plot the result
        vis_x = out[:, 0] * spread
        vis_y = out[:, 1] * spread
    else:
        vis_x = np.array(xs) * spread
        vis_y = np.array(ys) * spread

    p = figure(plot_width=800,
               plot_height=800,
               x_range=(-80, 80),
               y_range=(-80, 80),
               output_backend="webgl")

    p.image_rgba(image=IMG, x=vis_x, y=vis_y, dw=dw, dh=dh)

    show(p)
Example #2
0
def tsne_faces(prep, X, COLOR, lr, d, thumbs):
    model = TSNE(n_components=2, random_state=234)
    model.perplexity = prep
    model.learning_rate = lr
    d['prep'] = prep
    d['lr'] = lr

    np.set_printoptions(suppress=False)
    out = model.fit_transform(X)
    # plot the result
    vis_x = out[:, 0]
    vis_y = out[:, 1]

    hover = HoverTool(tooltips="""
    <div>
        <div>
            <img
                src="@pics" height="42" width="42"
                style="float: left; margin: 0px 15px 15px 0px;"
                border="0"
            ></img>
            <span style="font-size: 15px; color: #966;">$index</span>
        </div>
        <div>
            <span style="font-size: 10px; color: #696;">(@xvals, @yvals)</span>
        </div>
    </div>
    """)

    TOOLS = "pan,wheel_zoom,reset,lasso_select"
    p = figure(title="faceLocations", tools=TOOLS,
               output_backend="webgl")  #,figsize=(15, 15))

    p.add_tools(hover)

    source = ColumnDataSource(data=dict(xvals=list(vis_x),
                                        yvals=list(vis_y),
                                        fill_color=list(COLOR),
                                        pics=list(thumbs)))

    p.scatter("xvals",
              "yvals",
              color='fill_color',
              source=source,
              fill_alpha=0.2,
              size=5)

    source.callback = CustomJS(args=dict(p=p),
                               code="""
        var inds = cb_obj.get('selected')['1d'].indices;
        var d1 = cb_obj.get('data');
        var dat_len = inds.length;
        //console.log(d1)
        
        var js_tsne_x = new Array();
        var js_tsne_y = new Array();
        
        //console.log(inds);
        
        //console.log('starting');
        //console.log('length:' + dat_len)
        for (i=0; i< dat_len; i++){
            js_tsne_x.push(d1.xvals[inds[i]]);
            js_tsne_y.push(d1.yvals[inds[i]]);
        }
        
        //console.log(js_tsne_x);
        //console.log(js_tsne_y);
        
        var kernel = IPython.notebook.kernel;
        IPython.notebook.kernel.execute("inds = " + inds);
        IPython.notebook.kernel.execute("tsne_x = "+ js_tsne_x);
        IPython.notebook.kernel.execute("tsne_y = "+ js_tsne_y);
        
        """)

    show(p)
    return (prep, lr)