<jupyter_start><jupyter_text>PCA and t-SNE on Sugiharo's impossible cylinderThis notebook implements the 3D equations for making Sugihara's impossible cylinder as described in http://users.dickinson.edu/~richesod/RichesonImpossibleCylinder.pdfWe then do PCA and t-SNE on 1000 randomly sampled points.  The parametric equation for the positive coordinate half for $ -1 \leq t \leq 1$ is$$ r_1(t) = ( t, \frac{1}{2}(1-|t|+ \sqrt{1-t^2}, \frac{1}{2}(- 1 + |t|+ \sqrt{1-t^2})$$ $$ x_1 = t $$$$ y_2 = \frac{1}{2}(1-|t|+ \sqrt{1-t^2}$$$$ z_2 = \frac{1}{2}(- 1 + |t|+ \sqrt{1-t^2})$$  For the negative half for $ -1 \leq t \leq 1$ we have:$$ r_2(t) = ( t, \frac{1}{2}(- 1+|t| - \sqrt{1-t^2}, \frac{1}{2}( 1 + - |t| - \sqrt{1-t^2})$$ $$ x_2 = t $$$$ y_2 = \frac{1}{2}(- 1 + |t| - \sqrt{1-t^2})$$$$ z_2 = \frac{1}{2}(+ 1 - |t| - \sqrt{1-t^2})$$<jupyter_code>import numpy as np
import sklearn
from math import sqrt
import numpy as np
import random
def r1(t):
    if -1 < t > 1: print('t is outside the [-1,1] range!')
    x1 = t
    y1 = 0.5*( 1 - abs(t) + sqrt(1 - t**2) )
    z1 = 0.5*(-1 + abs(t) + sqrt(1 - t**2) )
    return [x1,y1,z1]

def r2(t):
    if -1 < t > 1: print('t is outside the [-1,1] range!')
    x2 = t
    y2 = 0.5*(-1 + abs(t) - sqrt(1 - t**2) )
    z2 = 0.5*(+1 - abs(t) - sqrt(1 - t**2) )
    return [x2,y2,z2]
# generate 500 r1 points 
r1_500 = np.array([r1(random.uniform(-1,1)) for i in range(500)])

# generate 500 r2 points
r2_500 = np.array([r2(random.uniform(-1,1)) for i in range(500)])

#concatenate the two arrays then slice into x,y,z
points = np.concatenate([r1_500, r2_500])
x1 = r1_500[:,:1,].flatten()
y1 = r1_500[:,1:2,].flatten()
z1 = r1_500[:,2:3,].flatten()

x2 = r2_500[:,:1,].flatten()
y2 = r2_500[:,1:2,].flatten()
z2 = r2_500[:,2:3,].flatten()

import plotly.plotly as py
import plotly.graph_objs as go
import plotly
import numpy as np
plotly.offline.init_notebook_mode() # run at the start of every notebook<jupyter_output><empty_output><jupyter_text>Plot 1000 points of Sugiharo's impossible cylinder edge<jupyter_code>trace1 = go.Scatter3d(
    x=x1,
    y=y1,
    z=z1,
    name = 'Positive Half',
    mode='markers',
    marker=dict(
        color='rgb(0, 127, 0)',
        size=10,
        line=dict(
            color='rgba(0, 50, 0, 0.14)',
            width=0.5
        ),
        opacity=0.6
    )
)

trace2 = go.Scatter3d(
    x=x2,
    y=y2,
    z=z2,
    name = 'Negative Half',
    mode='markers',
    marker=dict(
        color='rgb(127, 0, 0)',
        size=10,
        symbol='circle',
        line=dict(
            color='rgb(204, 0, 0)',
            width=1
        ),
        opacity=0.6
    )
)
data = [trace1, trace2]
layout = go.Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)
fig = go.Figure(data=data, layout=layout)
plotly.offline.iplot(fig, filename='sugiharo-cylinder-scatter')<jupyter_output><empty_output><jupyter_text>Now we do PCA<jupyter_code>import numpy as np
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(points)
pca_points1 = pca.transform(points[0:500])
pca_points2 = pca.transform(points[500:1000])
pca_x1 = pca_points1[:,:1,].flatten()
pca_y1 = pca_points1[:,1:2,].flatten()

pca_x2 = pca_points2[:,:1,].flatten()
pca_y2 = pca_points2[:,1:2,].flatten()


trace0 = go.Scatter(
    x = pca_x1,
    y = pca_y1,
    name = 'Positive Half',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(0, 150, 0, .8)',
        line = dict(
            width = 1,
            color = 'rgb(0, 0, 0)'
        )
    )
)

trace1 = go.Scatter(
    x = pca_x2,
    y = pca_y2,
    name = 'Negative Half',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(255, 30, 30, .9)',
        line = dict(
            width = 1,
        )
    )
)

data = [trace0, trace1]

layout = dict(title = "PCA on 1000 points of Sugiharo's cylinder",
              yaxis = dict(zeroline = False),
              xaxis = dict(zeroline = False)
             )

fig = dict(data=data, layout=layout)
plotly.offline.iplot(fig, filename='sugiharo-pca-scatter')<jupyter_output><empty_output><jupyter_text>Now we do t-SNE<jupyter_code>from sklearn.manifold import TSNE

tsne_points = TSNE(learning_rate=1000).fit_transform(points)

tsne_x1 = tsne_points[0:500][:,:1,].flatten()
tsne_y1 = tsne_points[0:500][:,1:2,].flatten()
tsne_x2 = tsne_points[500:1000][:,:1,].flatten()
tsne_y2 = tsne_points[500:1000][:,1:2,].flatten()
trace0 = go.Scatter(
    x = tsne_x1,
    y = tsne_y1,
    name = 'Positive Half',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(0, 150, 0, .8)',
        line = dict(
            width = 1,
            color = 'rgb(0, 0, 0)'
        )
    )
)

trace1 = go.Scatter(
    x = tsne_x2,
    y = tsne_y2,
    name = 'Negative Half',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(255, 30, 30, .9)',
        line = dict(
            width = 1,
        )
    )
)

data = [trace0, trace1]

layout = dict(title = "t-SNE on 1000 points of Sugiharo's cylinder",
              yaxis = dict(zeroline = False),
              xaxis = dict(zeroline = False)
             )

fig = dict(data=data, layout=layout)
plotly.offline.iplot(fig, filename='sugiharo-pca-scatter')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1766472686314999, 'fraction_numerical': 0.0679556973906514, 'mean_word_length': 2.6744827586206896, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '189399', 'license': 'Unlicense', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2193, 'n_words': 484, 'path': '.ipynb_checkpoints/sugiharo_pca_tnse-checkpoint.ipynb', 'repo_name': 'Munfred/sugiharo', 'size': '5327'}