Skip to content

Instantly share code, notes, and snippets.

@ytakashina
Last active April 8, 2018 02:36
Show Gist options
  • Save ytakashina/f48fed813c2d5f85978414f783c14658 to your computer and use it in GitHub Desktop.
Save ytakashina/f48fed813c2d5f85978414f783c14658 to your computer and use it in GitHub Desktop.

Networks

g = ig.Graph.Adjacency((pre_h_pred != 0).tolist())
g.es['weight'] = pre_h_pred[pre_h_pred.nonzero()]
g.vs['label'] = list(geos.values())
layt = g.layout('kk', dim=2)
N = len(pre_h_pred)
Xn = [layt[n][0] for n in range(N)]
Yn = [layt[n][1] for n in range(N)]
# Xe = [layt[n][0] for n in e.tuple for e in g.es]
# Ye = [layt[n][1] for n in e.tuple for e in g.es]
Xe = []
Ye = []
for e in g.es:
    Xe += [layt[e.tuple[0]][0], layt[e.tuple[1]][0]]
    Ye += [layt[e.tuple[0]][1], layt[e.tuple[1]][1]]

trace1 = plotly.graph_objs.Scatter(x=Xe, y=Ye, mode='lines', hoverinfo=None,
                                     line=plotly.graph_objs.Line(color='rgb(125,125,125)', width=1))
trace2 = plotly.graph_objs.Scatter(x=Xn, y=Yn, mode='markers', hoverinfo='text', text=list(geos.values()),
                                   marker=plotly.graph_objs.Marker(symbol='dot', size=20,
                                                                     line=plotly.graph_objs.Line(color='rgb(50,50,50)', width=0.5)
                                                                  )
                                   )

axis=dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='')
layout = plotly.graph_objs.Layout(
    title="Proposed",
    showlegend=False,
    scene=plotly.graph_objs.Scene(
        xaxis=plotly.graph_objs.XAxis(axis),
        yaxis=plotly.graph_objs.YAxis(axis),
        zaxis=plotly.graph_objs.ZAxis(axis)
    )
)
fig = plotly.graph_objs.Figure(data=[trace1, trace2], layout=layout)
plotly.offline.iplot(fig)

3D

trace = plotly.graph_objs.Scatter3d(x=X, y=Y, z=Z, mode='markers', marker=dict(size='2', line = dict(width = 1)))
plotly.offline.iplot([trace])
from mpl_toolkits import mplot3d

def plot_3D(elev=30, azim=30):
    ax = plt.subplot(projection='3d')
    ax.scatter3D(X[:, 0], X[:, 1], r, c=y, s=50, cmap='spring')
    ax.view_init(elev=elev, azim=azim)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('r')

輪郭線

def plot_with_contour(predict, xmin, xmax, ymin, ymax, d=1000):
    xv, yv = np.meshgrid(np.arange(xmin, xmax, (xmax - xmin) / d), np.arange(ymin, ymax, (ymax - ymin) / d))
    data = np.vstack([xv.reshape(d**2), yv.reshape(d**2)]).transpose()

    sns.lmplot(x='petal length (cm)', y='petal width (cm)', hue='class', data=df, fit_reg=False)
    prediction = predict(data)
    if np.sum(prediction) == 0:
        print('The predicted values are all zero. Cannot draw a contour.')
    else:
        plt.contour(xv, yv, prediction.reshape(d, d), colors='black')
  • 楕円の場合(まだ試してない)
x = [5,7,11,15,16,17,18]
y = [8, 5, 8, 9, 17, 18, 25]
cov = np.cov(x, y)
lambda_, v = np.linalg.eig(cov)
lambda_ = np.sqrt(lambda_)
from matplotlib.patches import Ellipse
import matplotlib.pyplot as plt
ax = plt.subplot(111, aspect='equal')
for j in xrange(1, 4):
    ell = Ellipse(xy=(np.mean(x), np.mean(y)),
                  width=lambda_[0]*j*2, height=lambda_[1]*j*2,
                  angle=np.rad2deg(np.arccos(v[0, 0])))
    ell.set_facecolor('none')
    ax.add_artist(ell)
plt.scatter(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment