Kaggle competition: Spooky Author Identification
<SOS> metric: MulticlassLoss Kaggle data source: spooky-author-identification<load_from_csv>
%matplotlib inline
df_train = pd.read_csv('.. /input/train.csv')
 print(df_train.columns)
 print(df_train.dtypes)
 df_test = pd.read_csv('.. /input/test.csv')
 print(df_test.columns )<concatenate>
data_dir = '.. /input/'
train = pd.read_csv(data_dir + 'train.csv')
 test = pd.read_csv(data_dir + 'test.csv' )
df_all = pd.concat([df_train,df_test],axis=0 ).reset_index(drop=True )<feature_engineering>
all_words = set([])
 train_words = []
test_words = []
train_words_len = []
test_words_len = []
regex = re.compile('[ ]')
 def preprocess_sentence(sentence):
punctuations = [',', '.', '"',
df_all['Title'] = df_all['Name'].map(lambda x: re.compile(", (.*?)\." ).findall(x)[0])
 df_all['Title'][df_all.Title == 'Jonkheer'] = 'Master'
df_all['Title'][df_all.Title.isin(['Ms','Mlle'])] = 'Miss'
df_all['Title'][df_all.Title == 'Mme'] = 'Mrs'
df_all['Title'][df_all.Title.isin(['Capt', 'Don', 'Major', 'Col', 'Sir'])] = 'Sir'
df_all['Title'][df_all.Title.isin(['Dona', 'Lady', 'the Countess'])] = 'Lady'<count_values>
train['word_len'] = train_words_len
test['word_len'] = test_words_len
print(df_all['Title'].value_counts() )<feature_engineering>
feature_len = 150
", df_all['Deck'])
 ", df_all['Room'] )<data_type_conversions>
vocab_size = len(all_words)+ 1
df_all = pd.concat([df_all, df_all['Ticket'].str.split(' ', expand=True)], axis=1 ).drop('Ticket', axis=1)
 df_all.rename(columns={0: 'Ticket_Cls', 1: 'Ticket_Num'}, inplace=True)
 df_all['Ticket_Num'][df_all['Ticket_Num'].isnull() ]=df_all['Ticket_Cls']
df_all['Ticket_Num']=pd.to_numeric(df_all['Ticket_Num'], errors='coerce')
 df_all['Ticket_Cls'][df_all['Ticket_Cls'].str.isnumeric() ]='None'
print("Ticket_Cls
",df_all['Ticket_Cls'])
 print("Ticket_Num
",df_all['Ticket_Num'] )<data_type_conversions>
word2ids = {}
for index, word in enumerate(all_words):
word2ids[word] = index + 1
df_all = df_all.drop(2, axis=1)
 df_all = df_all.drop(['Cabin'], axis=1)
 df_all['Embarked'].fillna(df_all['Embarked'].dropna().mode().values.item() ,inplace=True)
 df_all['Fare'].fillna(df_all['Fare'].median() ,inplace=True)
 df_all['Ticket_Num'].fillna(df_all['Ticket_Num'].median() ,inplace=True )<feature_engineering>
x_train = []
x_test = []
y_train = []
for index, row in train.iterrows() :
x = np.zeros(feature_len)
 for ix, word in enumerate(train_words[index]):
if(ix >= feature_len):
break
x[ix] = word2ids[word]
x_train.append(x)
 y = [0, 0, 0]
if row['author']=='EAP':
y[0] = 1
elif row['author']=='HPL':
y[1] = 1
else:
y[2] = 1
y_train.append(y)
 for index, row in test.iterrows() :
x = np.zeros(feature_len)
 for ix, word in enumerate(test_words[index]):
if(ix >= feature_len):
break
x[ix] = word2ids[word]
x_test.append(x)
df_all['Age_Cls']=0
df_all['Age_Cls'][df_all['Age']<=10] = 0
df_all['Age_Cls'][(df_all['Age']>10)&(df_all['Age']<=20)] = 1
df_all['Age_Cls'][(df_all['Age']>20)&(df_all['Age']<=30)] = 2
df_all['Age_Cls'][(df_all['Age']>30)&(df_all['Age']<=40)] = 3
df_all['Age_Cls'][(df_all['Age']>40)&(df_all['Age']<=50)] = 4
df_all['Age_Cls'][(df_all['Age']>50)&(df_all['Age']<=60)] = 5
df_all['Age_Cls'][df_all['Age']>60] = 6<feature_engineering>
msk = np.random.rand(len(x_train)) < 0.95
x_train = np.array(x_train)
 y_train = np.array(y_train)
 x_valid = x_train[~msk]
y_valid = y_train[~msk]
x_train = x_train[msk]
y_train = y_train[msk]
df_all['Fare_Bin'] = pd.qcut(df_all['Fare'], 6, labels=['Fare_G1', 'Fare_G2', 'Fare_G3', 'Fare_G4', 'Fare_G5', 'Fare_G6'] )<count_unique_values>
import keras
from keras.layers import *
from keras.layers.embeddings import Embedding
from keras import regularizers
from keras.models import Model
from keras.optimizers import SGD, RMSprop, Adam
from keras.models import Sequential
from keras.layers import Dense, Activation
print(df_all.nunique() /len(df_all))
df_all.drop(['Name'],axis=1,inplace=True )<normalization>
import gensim
from gensim.models import *
scaler = preprocessing.StandardScaler()
df_all['Fare'] = scaler.fit_transform(df_all['Fare'].values.reshape(-1, 1))
df_all['Age'] = scaler.fit_transform(df_all['Age'].values.reshape(-1, 1))
df_all['Ticket_Num'] = scaler.fit_transform(df_all['Ticket_Num'].values.reshape(-1, 1))<data_type_conversions>
n_fact = 30
df_all.drop(['Ticket_Num'],axis=1,inplace=True)
 df_all.drop(['Ticket_Cls'],axis=1,inplace=True)
 df_all['Pclass'] = df_all['Pclass'].astype(object)
 df_all['SibSp'] = df_all['SibSp'].astype(object)
 df_all['Parch'] = df_all['Parch'].astype(object)
 df_all['Age_Cls'] = df_all['Age_Cls'].astype(object)
 df_all = pd.get_dummies(df_all)
 print(df_all.columns )<prepare_x_and_y>
all_sentences = train_words + test_words
word2vec = Word2Vec(all_sentences, size=n_fact, window=7, min_count=2, workers=4, iter=20 )
print('Create DataSet', datetime.now() ,)
 df_all.reset_index(drop=True,inplace=True)
 y = df_all['Survived'][df_all['Survived'].isnull() == False]
x_train = df_all[:len(y)].drop(['PassengerId','Survived'],axis=1 ).values
x_test = df_all[len(y):].drop(['PassengerId','Survived'],axis=1 ).values
x_train.shape, x_test.shape, y.shape<choose_model_class>
emb = np.zeros(( vocab_size, n_fact))
for word,idx in word2ids.items() :
print(word)
 if word in word2vec.wv:
emb[idx] = word2vec.wv[word]
else:
print("word is not present:" + str(word))
emb[idx] = normal(scale=0.6, size=(n_fact,))
class PerceptronClassification(BaseEstimator,ClassifierMixin):

 def __init__(self, rate = 0.01, number_of_iterations = 100):
self.rate = rate
self.number_of_iterations = number_of_iterations
def fit(self, X, y):

 self.weight_matrix = np.zeros(1 + X.shape[1])
 self.errors_list = []
for _ in range(self.number_of_iterations):
errors = 0
for xi, target in zip(X, y):
update = self.rate *(target - self.predict(xi))
self.weight_matrix[1:] += update * xi
self.weight_matrix[0] += update
errors += int(update != 0.0)
 self.errors_list.append(errors)
 return self
def dot_product(self, X):

 return(np.dot(X, self.weight_matrix[1:])+ self.weight_matrix[0])
 def predict(self, X):

 return np.where(self.dot_product(X)>= 0.0, 1, 0 )<train_model>
model = Sequential([
Embedding(vocab_size, n_fact, input_length=feature_len, weights=[emb], trainable=True),
Dropout(0.5),
Conv1D(30, 7, border_mode='same', activation='relu'),
Dropout(0.5),
Flatten() ,
BatchNormalization() ,
Dense(40, activation='relu'),
Dropout(0.6),
Dense(3, activation='softmax')] )
print('Define Classification model', datetime.now() ,)
 perceptron_classifiter=PerceptronClassification(number_of_iterations = 3000 )<choose_model_class>
model.compile(loss='categorical_crossentropy', optimizer=Adam() , metrics=['accuracy'])
 model.summary()
LR = LogisticRegression(random_state=10, solver='lbfgs', multi_class='ovr' )<choose_model_class>
model.fit(np.array(x_train), y_train, validation_data=(np.array(x_valid),y_valid), nb_epoch=3, batch_size=64 )
ADA = AdaBoostClassifier()<choose_model_class>
model.fit(np.array(x_train), y_train, validation_data=(np.array(x_valid),y_valid), nb_epoch=3, batch_size=64 )
SVM = svm.LinearSVC()<train_on_grid>
pred = model.predict(np.array(x_test))
<choose_model_class><EOS> 
submission = pd.DataFrame()
submission ['id'] = test['id']
submission ['EAP'] = pred[:,0]
submission ['HPL'] = pred[:,1]
submission ['MWS'] = pred[:,2]
submission.to_csv('sub.csv', index=False )