Kaggle competition: Titanic - Machine Learning from Disaster
X_train, X_test, y_train, y_test = train_test_split(train.headline, train.is_sarcastic )<train_model>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
sarcasm_classfication.fit(X_train, y_train )<compute_train_metric>
temp = np.where(np.isnan(train_data.Age))
len(temp[0] )
print(roc_auc_score(y_train, sarcasm_classfication.decision_function(X_train)))
 print(roc_auc_score(y_test, sarcasm_classfication.decision_function(X_test)))
 cross_validate(sarcasm_classfication, train.headline, train.is_sarcastic, cv=5, scoring='roc_auc' )<feature_engineering>
for item in temp[0]:
train_data.Age.at[item] = np.mean(train_data.Age)
 temp = np.where(np.isnan(train_data.Age))
len(temp[0] )
def remove_punctuation(dataframe):
rgx = '('s|[!?,.:;'$])'
tmp = dataframe.copy()
tmp['headline'] = tmp['headline'].str.replace(rgx, '')
 return tmp<find_best_model_class>
temp = np.where(np.isnan(test_data.Fare))
for item in temp[0]:
test_data.Fare.at[item] = np.mean(test_data.Fare)
 temp = np.where(np.isnan(test_data.Fare))
len(temp[0] )
tmp = train.copy()
tmp = remove_punctuation(tmp)
 X_train, X_test, y_train, y_test = train_test_split(tmp.headline, tmp.is_sarcastic)
 sarcasm_classfication.fit(X_train, y_train)
 print(roc_auc_score(y_train, sarcasm_classfication.decision_function(X_train)))
 print(roc_auc_score(y_test, sarcasm_classfication.decision_function(X_test)))
 cross_validate(sarcasm_classfication, X_train, y_train, cv=5, scoring='roc_auc' )<feature_engineering>
temp = np.where(np.isnan(test_data.Age))
for item in temp[0]:
test_data.Age.at[item] = np.mean(train_data.Age)
 temp = np.where(np.isnan(test_data.Age))
len(temp[0] )
def get_top_n_words(corpus, n=None):
vec = CountVectorizer().fit(corpus)
 bag_of_words = vec.transform(corpus)
 sum_words = bag_of_words.sum(axis=0)
 words_freq = [(word, sum_words[0, idx], idx)for word, idx in vec.vocabulary_.items() ]
words_freq = sorted(words_freq, key = lambda x: x[1], reverse=True)
 words = [x[0] for x in words_freq]
count = [x[1] for x in words_freq]
return pd.DataFrame({'Words': words[:n], 'Amount': count[:n]} )<find_best_model_class>
y = train_data['Survived']
features = ['Pclass', 'Sex', 'SibSp', 'Parch','Fare', 'Age']
x = pd.get_dummies(train_data[features])
 x_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators = 100, max_depth = 5, random_state = 1)
 model.fit(x, y)
 predictions = model.predict(x_test )
t = train.copy()
for word in tmp.Words:
t.headline.str.replace(word, '')
 X_train, X_test, y_train, y_test = train_test_split(t.headline, t.is_sarcastic)
 sarcasm_classfication.fit(X_train, y_train)
 print(roc_auc_score(y_train, sarcasm_classfication.decision_function(X_train)))
 print(roc_auc_score(y_test, sarcasm_classfication.decision_function(X_test)))
 cross_validate(sarcasm_classfication, X_train, y_train, cv=5, scoring='roc_auc' )<feature_engineering>
output = pd.DataFrame({'PassengerId' : test_data.PassengerId, 'Survived' : predictions})
 output.to_csv('submission.csv', index = False)
 print("Your submission was successfully saved!" )