import pandas as pd
import os
os.chdir('C:\Users\sheng\MyProjects\WindowsProjects\Titanic')
df = pd.read_csv('train.csv')
#%%
for i in range(1,4):
    print i, len(df[ (df['Sex'] == 'male') & (df['Pclass']==i) ])
#%%
df.dtypes #show data types of all fields
df.head(3) # show first few rows
df.shape # show number of cols and rows
df.describe() # show stats for each feature
#%%
df['Age'] # select a column
df['Age'][:10] # select first 10 rows of a column
df['Age'].mean() # return some states of a column
df[df['Age'] > 60][['Age','Sex','SibSp']] # select by criteria



#%%
import pylab as plt
df['Age'].hist() # histogram of a feature
plt.show()

#%%
df['Gender'] = df['Sex'].map( {'female': 0, 'male': 1}).astype(int) # create a new column
df['Embarked'].unique()
df['OnBoard'] = df['Embarked'].map( {'C': 0, 'S': 1, 'Q': 2, None: -1}).astype(int) # create a new column

#%% filling up missing ages with per class means
import numpy as np
median_ages = np.zeros([2,3])

for i in range(2): #iterate over classes
    for j in range(3):
        median_ages[i,j] = df[(df['Gender']==i) & \
        (df['Pclass'] == j+1)]['Age'].dropna().median()

#%%
df['AgeFill'] = df['Age'] # create a copy
df.loc[100,'Age'] # a sample of using 2D indexing
for i in range(0, 2):
    for j in range(0, 3):
        df.loc[ (df.Age.isnull()) & (df.Gender == i) & (df.Pclass == j+1),\
                'AgeFill'] = median_ages[i,j]

#%%
#df[['Age','AgeFill']].head(50)
df[ df['Age'].isnull() ][['Gender','Pclass','Age','AgeFill']].head(10)

#%%
df['FamilySize'] = df['SibSp'] + df['Parch']
#%% drop all non-numerical features
droplist = df.dtypes[df.dtypes.map(lambda x: x=='object')]

#%%
dlist = []
for index, value in droplist.iteritems():
    dlist.append(index)
#%%    
df = df.drop(dlist, axis=1)
#%%
df = df.drop(['Age'], axis = 1)

#%%
train_data = df.values

#%%
Xtrain = train_data[:,2:]
ytrain = train_data[:,1]

#%%
from sklearn import svm
from sklearn.metrics import accuracy_score
clf = svm.SVC()
clf.fit(Xtrain, ytrain)
ypred = clf.predict(Xtrain)

#%%
print accuracy_score(ytrain, ypred)

