Kaggle competition: Titanic - Machine Learning from Disaster
test.drop('id',axis=1,inplace=True )<prepare_x_and_y>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv" )
x_test=test.values.reshape(-1,28,28,1)/255<predict_on_test>
import re
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.metrics import roc_curve, roc_auc_score
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
pred=np.argmax(model.predict(x=x_test),1 )<save_to_csv>
def preprecess(data):
data.Cabin.fillna("N", inplace = True)
 data.loc[data.Cabin.str[0] == "A", "Cabin"] = "A"
data.loc[data.Cabin.str[0] == "B", "Cabin"] = "B"
data.loc[data.Cabin.str[0] == "C", "Cabin"] = "C"
data.loc[data.Cabin.str[0] == "D", "Cabin"] = "D"
data.loc[data.Cabin.str[0] == "E", "Cabin"] = "E"
data.loc[data.Cabin.str[0] == "F", "Cabin"] = "F"
data.loc[data.Cabin.str[0] == "G", "Cabin"] = "G"
data.loc[data.Cabin.str[0] == "T", "Cabin"] = "N"
data["Sex"].replace("female", 1, inplace=True)
 data["Sex"].replace("male", 0, inplace=True)
 data["Age"].fillna(data["Age"].median() ,inplace=True)
 data["Fare"].fillna(data["Fare"].median() ,inplace=True)
 data.Pclass.replace(1,"First",inplace=True)
 data.Pclass.replace(2,"Second",inplace=True)
 data.Pclass.replace(3,"Third",inplace=True)
 data = pd.get_dummies(data=data, columns=["Cabin","Embarked","Pclass"])
 return data
def age_detection(value):
if value < 18.0:
return "kid"
elif value >= 18.0 and value <=45.0:
return "adult"
else:
return "mature"
def group_title(data):
data["Title"] = data["Name"].map(lambda x: x.split(",")[1].split(".")[0].strip(" "))
data['Title'].replace('Master', "A", inplace=True)
 data['Title'].replace('Mr', "B", inplace=True)
 data['Title'].replace(['Ms','Mlle', 'Miss'], "C", inplace=True)
 data['Title'].replace(['Mme', 'Mrs'], "D", inplace=True)
 data['Title'].replace(['Dona', 'Lady', 'the Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'the'], "E", inplace=True)
 data = pd.get_dummies(data=data, columns=["Title"])
 return data
def outlier(data):
for column in ["Age", "Fare"]:
for sex in data.Sex.unique() :
sex_data = data[data["Sex"] == sex]
sex_column = sex_data[column]
Q1 = np.percentile(sex_column,25)
 Q3 = np.percentile(sex_column,75)
 IQR = Q3 - Q1
STEP = 1.5 * IQR
MAX_BORDER = Q3 + STEP
MIN_BORDER = Q1 - STEP
data.loc[(data["Sex"] == sex)&(data[column] > MAX_BORDER), column] = MAX_BORDER
data.loc[(data["Sex"] == sex)&(data[column] < MIN_BORDER), column] = MIN_BORDER
return data
def scaler(data):
data["Age"] = StandardScaler().fit_transform(data[["Age"]])
 data["Fare"] = StandardScaler().fit_transform(data[["Fare"]])
 return data
def submit(test_id,pred):
df=np.concatenate(( test_id.reshape(-1,1),pred.reshape(-1,1)) ,axis=1)
 df=pd.DataFrame(df,columns=['id','label'])
 df['id']=df['id'].astype('int32')
 df.to_csv('Submission.csv',index=False )<compute_test_metric>
train = outlier(train)
 test = outlier(test)
 train["Age_Detection"] = train["Age"].apply(age_detection)
 test["Age_Detection"] = test["Age"].apply(age_detection)
 train = pd.get_dummies(data = train, columns = ["Age_Detection"])
 test = pd.get_dummies(data = test, columns = ["Age_Detection"])
 train = preprecess(train)
 train = group_title(train)
 test = preprecess(test)
 test = group_title(test)
 train = scaler(train)
 test = scaler(test)
 train.drop(["Name","Ticket"],axis = 1 ,inplace = True)
 test.drop(["Name","Ticket"],axis = 1 ,inplace = True )
submit(test_id,pred )<import_modules>
train.drop("PassengerId", axis = 1, inplace=True )
import pandas as pd
import numpy as np
import os
import tensorflow as tf<import_modules>
y = train.Survived
X = train.drop("Survived", axis=1 )
from sklearn.model_selection import train_test_split<import_modules>
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.3, random_state = 1845 )
from sklearn.model_selection import train_test_split<load_from_csv>
xgb_params = {
'n_estimators' : [100,300,500,700],
'subsample' : [0.6, 0.8, 1.0],
'max_depth' : [3,4,5,6],
'learning_rate' : [0.1, 0.01, 0.02, 0.05],
'min_samples_split' : [2,5,10]
}
xgb = XGBClassifier()
xgb_cv_model = GridSearchCV(xgb, xgb_params, cv = 10, n_jobs = -1, verbose = 2 )
def get_data(file):
data = pd.read_csv(file)
 data = data.to_numpy()
labels = []
images = []
for row_ind in range(data.shape[0]):
labels.append(data[row_ind, 0])
 images.append(data[row_ind, 1:])
 images = np.reshape(images, newshape=(-1, 28, 28,1))
labels = np.array(labels)
 return images, labels
training_images, training_labels = get_data('.. /input/Kannada-MNIST/train.csv')
 val_images, val_labels = get_data('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 test_images, test_id = get_data('.. /input/Kannada-MNIST/test.csv')
 print(training_images.shape)
 print(training_labels.shape)
 print(test_images.shape )<load_from_csv>
xgb_cv_model.fit(X_train, y_train )
sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )<import_modules>
xgb_cv_model.best_params_
from keras.utils.np_utils import to_categorical<categorify>
xgb = XGBClassifier(learning_rate = 0.1,
max_depth = 5,
n_estimators = 100,
subsample = 0.8)
training_labels = to_categorical(training_labels, num_classes = 10 )<import_modules>
xgb_tuned = xgb.fit(X_train,y_train )
from sklearn.model_selection import train_test_split<import_modules>
y_pred = xgb_tuned.predict(X_test )
from sklearn.model_selection import train_test_split<split>
accuracy_score(y_test,y_pred )
X_train, X_val, Y_train, Y_val = train_test_split(training_images,training_labels, test_size = 0.2, random_state=10 )<import_modules>
print(classification_report(y_test,y_pred))
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop<choose_model_class>
lgbm_params = {
'n_estimators' : [100,300],
'subsample' : [0.6, 0.8, 1.0],
'max_depth' : [3,4,5,6],
'learning_rate' : [0.1, 0.01, 0.02, 0.05],
'min_child_samples' : [5,10,20]
}
lgbm = LGBMClassifier()
lgbm_cv_model = GridSearchCV(lgbm, lgbm_params, cv=10, n_jobs=-1, verbose = 2 )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.Conv2D(128,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(128,(3, 3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(192,(3, 3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(192,(3, 3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.5),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ] )<choose_model_class>
lgbm_cv_model.fit(X_train,y_train )
model.compile(optimizer= keras.optimizers.RMSprop(learning_rate=0.0005, rho=0.9), loss= "categorical_crossentropy", metrics=['accuracy'] )<train_model>
lgbm_cv_model.best_params_
model.fit(X_train, Y_train, batch_size = 120, epochs =40, validation_data =(X_val, Y_val), verbose = 2 )<save_to_csv>
lgbm = LGBMClassifier(learning_rate = 0.1,
max_depth = 4,
min_child_samples = 10,
n_estimators = 100,
subsample = 0.6)
results = model.predict(test_images)
 results = np.argmax(results,axis = 1)
 sample_sub['label']=results
sample_sub.to_csv('submission.csv',index=False)
<import_modules>
lgbm_mode = lgbm.fit(X_train,y_train )
import numpy as np
import pandas as pd
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem.wordnet import WordNetLemmatizer
import re
import tensorflow as tf
import tensorflow.keras.backend as K
from sklearn.model_selection import StratifiedKFold
from transformers import *
import tokenizers
from keras.layers import Dense, Flatten, Conv1D, Dropout, Input
from keras.models import Model, load_model
from keras.callbacks import ModelCheckpoint, EarlyStopping 
<load_from_csv>
y_pred = lgbm_mode.predict(X_test )
train_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv')
 test_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv')
 submission_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/sample_submission.csv' )<count_missing_values>
accuracy_score(y_test, y_pred )
train_df.isnull().sum()<correct_missing_values>
print(classification_report(y_test,y_pred))
train_df.dropna(inplace=True)
 train_df.reset_index(inplace=True )<count_missing_values>
test_id = test.PassengerId
test.drop("PassengerId", axis=1, inplace=True )
train_df.isnull().sum()<feature_engineering>
xgb = XGBClassifier(learning_rate = 0.1,
max_depth = 6,
n_estimators = 100,
subsample = 0.8)
def word(text):
review_word = BeautifulSoup(text ).get_text()
review_word = re.sub(r'[^a-zA-Z]',' ', review_word)
 words = review_word.lower().split()
stops = set(stopwords.words('english'))
words = [w for w in words if w not in stops]
return ' '.join(words)
 train_df['clean_text']=train_df['text'].apply(lambda x : word(x))<string_transform>
xgb_model = xgb.fit(X,y )
def f_emoji(text):
sentences=[]
emoji_text = emoji.demojize(text)
 line = re.sub(":","",emoji_text)
 for sentence in line:
sett = re.sub("_"," ",sentence)
 sentences.append(' '.join(sett))
return ''.join(sentences)
 def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 def remove_email(text):
line = re.compile(r'[\w\.-]+@[\w\.-]+')
 return line.sub(r'',text)
 def remove_hash(text):
line = re.sub(r'\
return ''.join(line)
 def remove_phone_num(text):
line = re.sub(r'\b\d{10}\b','', text)
 line = re.sub(r'(\d{3})\-(\d{3})\-(\d{4})','',line)
 return ''.join(line)
 def remove_year(text):
line=re.sub(r"\b(19[4-9][0-9]|20[0-1][0-9]|2020)\b",'',text)
 return ''.join(line)
 def remove_url(text):
url = re.sub(r'http[s]?[\:]?//\S+|www\.\S+\.\S+','',text)
 return ''.join(url)
 train_df['c_s_text']= train_df['selected_text'].copy()
train_df['c_s_text']= train_df['c_s_text'].apply(lambda x : remove_url(x))
train_df['c_s_text']= train_df['c_s_text'].apply(lambda x : remove_email(x))
train_df['c_s_text']= train_df['c_s_text'].apply(lambda x : remove_emoji(x))
train_df['c_s_text']= train_df['c_s_text'].apply(lambda x : f_emoji(x))
train_df['c_s_text']= train_df['c_s_text'].apply(lambda x : word(x)) 
<groupby>
test_pred = xgb_model.predict(test )
sent_r=train_df.groupby('sentiment')['sentiment'].count()<filter>
submission = pd.DataFrame({"PassengerId" : test_id, "Survived" : test_pred} )
<feature_engineering><EOS> 
submission.to_csv("submission.csv",index=False )