image_output_1.png
import matplotlib.pyplot as plt # plotting

import numpy as np # linear algebra

import pandas as pd # data processing

import re # regular expressions

raw_data = pd.read_csv('../input/train.csv')
raw_data = shuffle(raw_data)
gender_dic = {'male': 0, 'female': 1}
raw_data.ix[:, 4] = raw_data.ix[:, 4].replace(gender_dic)
raw_data.fillna(0, inplace=True)
raw_data = raw_data.assign(Cabin_count=0)
raw_data = raw_data.assign(Cabin_letter=0)
raw_data = raw_data.assign(Cabin_number=0)
def map_cabin(row):
    cabin_str = row[10]
    if cabin_str == 0:
        return row
    cabin_parts = cabin_str.split(' ')
    row[12] = len(cabin_parts)
    lc = cabin_parts[len(cabin_parts) - 1]
    cl = re.findall('[A-Z]', lc)
    cn = re.findall('\\d+', lc)
    if len(cl) > 0:
        row[13] = ord(cl[0])
    else:
        row[13] = 0
    if len(cn) > 0:
        row[14] = int(cn[0])
    else:
        row[14] = 0
    return row
raw_data = raw_data.apply(map_cabin, axis=1)
data_X = raw_data.ix[:, [2, 4, 5, 6, 7, 9, 12, 13, 14]]
data_y = raw_data.ix[:, 1]
poly = PolynomialFeatures(2)
data_X = poly.fit_transform(data_X)
msk = np.random.rand(len(data_X)) < 0.8
train_X = data_X[msk]
train_y = data_y[msk]
cv_X = data_X[~msk]
cv_y = data_y[~msk]

pca = PCA(n_components=2)
t = pca.fit_transform(data_X)
t_x, t_y = t.T
plt.scatter(t_x, t_y, c=data_y)
plt.show()