Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
traincount = int(len(train["Date"]))
testcount = int(len(test["Date"])) 
<categorify>
train = pd.read_csv('.. /input/titanic/train.csv' )
train.Province_State = train.Province_State.fillna(0)
 empty = 0
for i in range(0,traincount):
if(train.Province_State[i] == empty):
train.Province_State[i] = train.Country_Region[i]<categorify>
train.loc[train['Age']>=18,'Adult']=1
train.loc[train['Age']<18,'Adult']=0
test.Province_State = test.Province_State.fillna(0)
 empty = 0
for i in range(0,testcount):
if(test.Province_State[i] == empty):
test.Province_State[i] = test.Country_Region[i]<categorify>
train.loc[train['Fare']>=31,'Rich']=1
train.loc[train['Fare']<31,'Rich']=0
label = preprocessing.LabelEncoder()
train.Country_Region = label.fit_transform(train.Country_Region)
 train.Province_State = label.fit_transform(train.Province_State )<categorify>
train.drop(['PassengerId','Name','SibSp','Parch','Ticket','Cabin','Age','Fare'],axis=1,inplace=True )
test.Country_Region = label.fit_transform(test.Country_Region)
 test.Province_State = label.fit_transform(test.Province_State)
<prepare_x_and_y>
train = pd.get_dummies(train,drop_first=True )
X = np.c_[train["Province_State"], train["Country_Region"], train["DayCount"], train["Month"], train["Day"]]
Xt = np.c_[test["Province_State"], test["Country_Region"], test["DayCount"], test["Month"], test["Day"]]<prepare_x_and_y>
train.count()
Y1 = train["ConfirmedCases"]
Y2 = train["Fatalities"]<choose_model_class>
train = train.astype(np.int )
regr = XGBRegressor(n_estimators = 1500, gamma = 0, learning_rate = 0.8, random_state = 42, max_depth = 50, subsample = 1, reg_lambda = 0, reg_alpha = 0.5)
 regr1 = XGBRegressor(n_estimators = 1500, gamma = 0, learning_rate = 0.8, random_state = 42, max_depth = 50, subsample = 1, reg_lambda = 0, reg_alpha = 0.5 )<compute_test_metric>
test = pd.read_csv('.. /input/titanic/test.csv' )
def rmsle(y_true, y_pred):
return mean_squared_log_error(y_true, y_pred)**(1/2);<train_model>
test.loc[test['Age']>=18,'Adult']=1
test.loc[test['Age']<18,'Adult']=0
test.loc[test['Fare']>=31,'Rich']=1
test.loc[test['Fare']<31,'Rich']=0
regr.fit(X,Y1.ravel())
 yscore = regr.predict(X)
 a = abs(yscore)
 b=abs(Y1)
 ascore =(rmsle(a,b))
print("RMSLE of Confirmed Cases is ",ascore )<predict_on_test>
test.drop(['PassengerId','Name','SibSp','Parch','Ticket','Cabin','Age','Fare'],axis=1,inplace=True )
ypred = regr.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20 )<compute_test_metric>
test = pd.get_dummies(test,drop_first=True )
regr1.fit(X,Y2.ravel())
 ypred2 = regr1.predict(Xt)
 yptest = regr1.predict(X)
 yptest = np.round(yptest)
 c = abs(yptest)
 d = abs(Y2)
 ascore = rmsle(c,d)
 print("RMSLE of Fatalities is", ascore)
<prepare_output>
test = test.astype(np.int )
ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )<create_dataframe>
test.count()
ypc = pd.DataFrame()
forecast = test["ForecastId"]<prepare_output>
x_train = train.drop('Survived',axis=1)
 y_train = train['Survived']
x_test = test
ypc.insert(0,"ForecastId",forecast,False)
 ypc.insert(1,"ConfirmedCases",ypred,False)
 ypc.insert(2, "Fatalities",ypred2,False )<save_to_csv>
model = LogisticRegression()
ypc.to_csv('submission.csv', index=False )<string_transform>
model.fit(x_train,y_train )
dim = 300
num_words = 50000
max_len = 100
print('Fiting tokenizer')
 tokenizer = Tokenizer(num_words=num_words)
 tokenizer.fit_on_texts(df['question_text'])
 print('spliting data')
 df_train,df_test = train_test_split(df, random_state=1)
 print('text to sequence')
 x_train = tokenizer.texts_to_sequences(df_train['question_text'])
 x_test = tokenizer.texts_to_sequences(df_test['question_text'])
 print('pad sequence')
 x_train = pad_sequences(x_train,maxlen=max_len)
 x_test = pad_sequences(x_test, maxlen=max_len)
 y_train = df_train['target'].values
y_test = df_test['target'].values
print(x_train.shape)
 print(y_train.shape)
<init_hyperparams>
y_pred = model.predict(x_test )
print('loading word2vec model...')
 word2vec = gensim.models.KeyedVectors.load_word2vec_format('.. /input/embeddings/GoogleNews-vectors-negative300/GoogleNews-vectors-negative300.bin', binary=True)
 print('vocab:',len(word2vec.vocab))
all_embs = word2vec.vectors
emb_mean,emb_std = all_embs.mean() , all_embs.std()
print(emb_mean,emb_std)
 print(num_words,' from ',len(tokenizer.word_index.items()))
embedding_matrix = np.random.normal(emb_mean, emb_std,(num_words, dim))
count = 0
for word, i in tokenizer.word_index.items() :
if i>=num_words:
break
if word in word2vec.vocab:
embedding_matrix[i] = word2vec.word_vec(word)
 else:
count += 1
print('embedding matrix size:',embedding_matrix.shape)
 print('Number of words not in vocab:',count)
 del word2vec
gc.collect()<compute_train_metric>
s = pd.read_csv(".. /input/titanic/gender_submission.csv" )
print('Glove...')
 def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open('.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
print(len(all_embs))
word_index = tokenizer.word_index
embedding_matrix_glov = np.random.normal(emb_mean, emb_std,(num_words, dim))
count=0
for word, i in word_index.items() :
if i >= num_words:
break
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix_glov[i] = embedding_vector
else:
count += 1
print('embedding matrix size:',embedding_matrix_glov.shape)
 print('Number of words not in vocab:',count)
 del embeddings_index,all_embs
gc.collect()<statistical_test>
f = {"PassengerId":s["PassengerId"],"Survived":y_pred}
f = pd.DataFrame(f )
<statistical_test><EOS> 
f.to_csv('output.csv',index=False )