In [1]:
##################################################################################
############################### LOADING DATA #####################################
##################################################################################
In [2]:
import pandas as pd
import numpy as np
import data_handler as dh
import matplotlib.pyplot as plt
from matplotlib import offsetbox
%matplotlib inline
In [3]:
train,test = dh.load_titanic("~/data/kaggle/titanic/")
x_train, y_train, x_test, y_test = dh.clean_titanic(train,test)
In [4]:
print x_train.shape, y_train.shape, x_test.shape, y_test.shape
In [5]:
x_train.head()
Out[5]:
In [6]:
y_train.head()
Out[6]:
In [7]:
def plot_embedding(x, y):
x_min, x_max = np.min(x, 0), np.max(x, 0)
x = (x - x_min) / (x_max - x_min)
plt.figure()
plt.rcParams['figure.figsize'] = (6, 6)
ax = plt.subplot(111)
#for i in range(x.shape[0]):
# plt.scatter(x[i, 0], x[i, 1], color=y[i])
plt.scatter(x[:, 0], x[:, 1], c=y, s=40)
#if hasattr(offsetbox, 'AnnotationBbox'):
# # only print thumbnails with matplotlib > 1.0
# shown_images = np.array([[1., 1.]]) # just something big
# for i in range(y.shape[0]):
# dist = np.sum((x[i] - shown_images) ** 2, 1)
# if np.min(dist) < 4e-3:
# # don't show points that are too close
# continue
# shown_images = np.r_[shown_images, [X[i]]]
plt.xticks([]), plt.yticks([])
In [8]:
##################################################################################
###################### Stand prescaling (mean:0 stddev*1) ########################
##################################################################################
In [9]:
from sklearn import preprocessing
In [10]:
scaler = preprocessing.StandardScaler()
scaler.fit(x_train)
Out[10]:
In [14]:
x_scaled = scaler.transform(x_train)
x_scaled_test = scaler.transform(x_test)
In [15]:
print x_scaled[0:10,:]
In [35]:
x_scaled.mean(axis=0)
Out[35]:
In [34]:
x_test.mean()
Out[34]:
In [16]:
##################################################################################
###################### PCA (Principle Component Analysis) ########################
##################################################################################
In [17]:
from sklearn import decomposition
In [18]:
pca = decomposition.PCA(n_components=2)
x_train_pca = pca.fit_transform(x_scaled)
x_test_pca = pca.transform(x_scaled_test)
In [19]:
print x_train_pca[0:10,:], "\n", x_test_pca[0:3,:],
In [20]:
plot_embedding(x_train_pca, y_train.values)
In [ ]:
##################################################################################
###################### ICA (Independent Component Analysis) ######################
##################################################################################
In [21]:
ica = decomposition.FastICA(n_components=2)
x_train_ica = ica.fit_transform(x_scaled)
x_test_ica = ica.transform(x_scaled_test)
In [22]:
print x_train_ica[0:10,:], "\n", x_test_ica[0:3,:],
In [23]:
plot_embedding(x_train_ica, y_train.values)
In [24]:
##################################################################################
############### tSNE (t-distributed Stochastic Neighbor Embedding.) ##############
##################################################################################
In [25]:
from sklearn import manifold
In [26]:
tsne = manifold.TSNE(n_components=2, random_state=0)
# np.set_printoptions(suppress=True)
x_train_tsne = tsne.fit_transform(x_scaled, y=y_train)
#x_test_tsne = tsne.transform(x_scaled_test)
In [27]:
print x_train_tsne[0:10,:]
In [28]:
plot_embedding(x_train_tsne, y_train.values)
In [29]:
##################################################################################
########################### ISOMap (Isometric Mapping) ###########################
##################################################################################
In [30]:
iso = manifold.Isomap(n_components=2)
x_train_iso = iso.fit_transform(x_scaled, y=y_train)
x_test_iso = iso.transform(x_scaled_test)
In [31]:
print x_train_iso[0:10,:]
In [32]:
plot_embedding(x_train_iso, y_train.values)
In [ ]: