# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
ROOT='/home/alexanderliao/data/Kaggle/competitions/tgs-salt-identification-challenge'
import pandas as pd
from tqdm import tqdm_notebook
import numpy as np
from keras.preprocessing.image import load_img

train_df = pd.read_csv(ROOT+"/train.csv", index_col="id", usecols=[0])
depths_df = pd.read_csv(ROOT+"/depths.csv", index_col="id")
train_df = train_df.join(depths_df)
test_df = depths_df[~depths_df.index.isin(train_df.index)]
train_df["images"] = [np.array(load_img(ROOT+"/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
#train_df["masks"] = [np.array(load_img(ROOT+"/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]

train_im=[]
for image in train_df["images"]:
    train_im.append(image.flatten())
train_df.drop(["images"])
"""
train_msk=[]
for image in train_df["masks"]:
    train_msk.append(image.flatten())
"""

test_df["images"] = [np.array(load_img(ROOT+"/test/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(test_df.index)]

# -

test_im=[]
for image in test_df["images"]:
    test_im.append(image.flatten())
test_df.drop(["images"])

# +
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

pca = PCA(n_components=20)
pca_result = pca.fit_transform(train_im)
tsne = TSNE(n_components=3, verbose=1, perplexity=100, n_iter=5000)
#tsne_results = tsne.fit_transform(pca_result)
tsne_results = pca_result

# +
train_df['tsne-one'] = tsne_results[:,0]
train_df['tsne-two'] = tsne_results[:,1] 
train_df['tsne-three'] = tsne_results[:,2]

"""
train_df['pca-one'] = pca_result[:,0]
train_df['pca-two'] = pca_result[:,1] 
train_df['pca-three'] = pca_result[:,2]
"""
print('Explained variation per principal component: {}'.format(pca.explained_variance_ratio_))


pca = PCA(n_components=50)
pca_result = pca.fit_transform(train_msk)

train_df['pca-mask-one'] = pca_result[:,0]
train_df['pca-mask-two'] = pca_result[:,1] 
train_df['pca-mask-three'] = pca_result[:,2]

print('Explained variation per principal component: {}'.format(pca.explained_variance_ratio_))

# +
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
import random
# %matplotlib inline  


fig = pyplot.figure()
ax = Axes3D(fig)

ax.scatter(train_df['tsne-one'],train_df['tsne-two'],train_df['tsne-three'])
pyplot.show()

# +
pca = PCA(n_components=20)
pca_result = pca.fit_transform(test_im)
tsne = TSNE(n_components=3, verbose=1, perplexity=100, n_iter=5000)
#tsne_results = tsne.fit_transform(pca_result)
tsne_results = pca_result

test_df['tsne-one'] = tsne_results[:,0]
test_df['tsne-two'] = tsne_results[:,1] 
test_df['tsne-three'] = tsne_results[:,2]

fig = pyplot.figure()
ax = Axes3D(fig)

ax.scatter(test_df['tsne-one'],test_df['tsne-two'],test_df['tsne-three'])
pyplot.show()
# -



StarcoderdataJupyterScriptsDedupFiltered