Kaggle competition: Titanic - Machine Learning from Disaster
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.impute import KNNImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RepeatedStratifiedKFold,cross_val_score
from sklearn.feature_selection import chi2,SelectKBest,mutual_info_classif
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import RandomizedSearchCV,GridSearchCV,StratifiedKFold
df_train = pd.read_csv('/kaggle/input/dataanalyticscoaching/train.csv')
 df_test = pd.read_csv('/kaggle/input/dataanalyticscoaching/test.csv' )<count_missing_values>
train=pd.read_csv("/kaggle/input/titanic/train.csv")
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 target=train['Survived']
sub=pd.DataFrame(test['PassengerId'] )
df_train.isnull().sum().sum()<count_missing_values>
null_data=['Survived','Pclass','Age','SibSp','Parch','Fare']
imputer=KNNImputer()
imputer.fit(train[null_data])
 b=imputer.transform(train[null_data])
 a=pd.DataFrame(b)
 train['Age']=a.iloc[:,2]
train['Fare']=a.iloc[:,5]
df_test.isnull().sum().sum()<count_values>
print(train.isnull().sum() ,'
',test.isnull().sum())
 a=train.groupby(train['Pclass'])['Age'].median()
test['Age']=test['Age'].fillna(test['Pclass'].map(a))
test['Fare'].fillna(test['Fare'].median() ,inplace=True)
 train['Cabin'].fillna('X',inplace=True)
 test['Cabin'].fillna('X',inplace=True)
 train['Embarked'].fillna('S',inplace=True)
 print('
',train.isnull().sum() ,'
',test.isnull().sum())
df_train['target'].value_counts()<define_variables>
x=[]
for i in range(len(train)) :
a=train['Cabin'][i][0]
x.append(a)
 train['Cabin']=x
x=[]
for i in range(len(test)) :
x.append(test['Cabin'][i][0])
 test['Cabin']=x
train['Cabin']=train['Cabin'].map({'A':3,'B':3,'C':3,'D':2,"E":2,'F':2,'T':3,'X':0,'G':1,})
 test['Cabin']=test['Cabin'].map({'A':3,'B':3,'C':3,'D':2,"E":2,'F':2,'T':3,'X':0,'G':1,})
 a=[]
for i in range(len(train)) :
a.append(train['Name'][i].split() [1])
 train['Title']=a
a=[]
for i in range(len(test)) :
a.append(test['Name'][i].split() [1])
 test['Title']=a
train['Passenger']=train['SibSp']+train['Parch']+1
test['Passenger']=test['SibSp']+test['Parch']+1
def family(size):
a=''
if(size<=1):
a='lonely'
elif(size<=3):
a='nuclear'
elif(size<=6):
a='middle'
else:
a='large'
return a
train['Passenger']=train['Passenger'].map(family)
 test['Passenger']=test['Passenger'].map(family)
 a=[]
for i in range(len(train['Ticket'])) :
if not(train['Ticket'][i][0].isdigit()):
a.append(1)
 else:
a.append(0)
 train['Ticket']=a
a=[]
for i in range(len(test['Ticket'])) :
if not(test['Ticket'][i][0].isdigit()):
a.append(1)
 else:
a.append(0)
 test['Ticket']=a
cat_feats = ['sex', 'cp', 'fbs', 'restecg', 'exang', 'slope', 'ca', 'thal']<define_variables>
train.drop(['Name','PassengerId'],axis=1,inplace=True)
 test.drop(['Name','PassengerId'],axis=1,inplace=True)
 encode=LabelEncoder()
encode.fit(train['Sex'])
 train['Sex']=encode.transform(train['Sex'])
 test['Sex']=encode.transform(test['Sex'])
 encode.fit(train['Embarked'])
 train['Embarked']=encode.transform(train['Embarked'])
 test['Embarked']=encode.transform(test['Embarked'])
 encode.fit(train['Passenger'])
 train['Passenger']=encode.transform(train['Passenger'])
 test['Passenger']=encode.transform(test['Passenger'])
num_feats = list(set(df_train.columns)- set(cat_feats)- {'id', 'target'})
 print(num_feats )<prepare_x_and_y>
a=pd.DataFrame(train['Title'].value_counts())
 a.index
train['Title']=train['Title'].replace(['Dr.', 'Rev.', 'y', 'Planke,',
'Impe,', 'Gordon,', 'Mlle.', 'Col.', 'Major.', 'Pelsmaeker,', 'Capt.',
'Shawah,', 'Messemaeker,', 'Billiard,', 'Carlo,', 'Mulder,', 'Ms.',
'the', 'Jonkheer.', 'Don.', 'Mme.', 'Melkebeke,', 'der', 'Steen,',
'Cruyssen,', 'Velde,', 'Walle,'],'Rare')
 test['Title']=test['Title'].replace(['Dr.', 'Rev.', 'y', 'Planke,',
'Impe,', 'Gordon,', 'Mlle.', 'Col.', 'Major.', 'Pelsmaeker,', 'Capt.',
'Shawah,', 'Messemaeker,', 'Billiard,', 'Carlo,', 'Mulder,', 'Ms.',
'the', 'Jonkheer.', 'Don.', 'Mme.', 'Melkebeke,', 'der', 'Steen,',
'Cruyssen,', 'Velde,', 'Walle,'],'Rare')
 train['Title']=train['Title'].map({'Mr.':1,'Miss.':2,'Mrs.':2,'Rare':0,'Master.':3})
 test['Title']=test['Title'].map({'Mr.':1,'Miss.':2,'Mrs.':2,'Rare':0,'Master.':3,'Brito,':0,'Khalil,':0,'Palmquist,':0})
 train.drop('Survived',axis=1,inplace=True )
X_train = df_train[cat_feats + num_feats]
y_train = df_train['target']<import_modules>
fs=SelectKBest(score_func=chi2,k='all')
 fs.fit(train,target)
 x=fs.transform(train)
 for i in range(len(fs.scores_)) :
print('Feature %d: %f' %(i, fs.scores_[i]))
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.preprocessing import StandardScaler<prepare_x_and_y>
train.drop(['SibSp','Ticket','Parch'],axis=1,inplace=True)
 test.drop(['SibSp','Ticket','Parch'],axis=1,inplace=True )
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names].values<create_dataframe>
train['genderclass']=train['Sex']*train['Pclass']
test['genderclass']=test['Sex']*test['Pclass']
num_pipeline = Pipeline([ 
('selector', DataFrameSelector(num_feats)) , 
('std_scaler', StandardScaler())
 ] )<create_dataframe>
xtrain,xvalid,ytrain,yvalid=train_test_split(train,target,test_size=0.25)
 models = {"KNN": KNeighborsClassifier() ,
"Logistic Regression": LogisticRegression(max_iter=10000),
"Random Forest": RandomForestClassifier() ,
"SVC" : SVC(probability=True),
"DecisionTreeClassifier" : DecisionTreeClassifier() ,
"AdaBoostClassifier" : AdaBoostClassifier(algorithm='SAMME', base_estimator=DecisionTreeClassifier() ,
learning_rate=1.5, n_estimators=2, random_state=7),
"GradientBoostingClassifier" : GradientBoostingClassifier(max_depth=4, max_features=0.3, min_samples_leaf=100,
n_estimators=300),
"GaussianNB" : GaussianNB() ,
"LinearDiscriminantAnalysis" : LinearDiscriminantAnalysis() ,
"QuadraticDiscriminantAnalysis" : QuadraticDiscriminantAnalysis() }
scores={}
cv=RepeatedStratifiedKFold(n_splits=10,n_repeats=3)
 np.random.seed(42)
 model_scores = {}
for name, model in models.items() :
score=cross_val_score(model,train,target,cv=cv,scoring='accuracy',n_jobs=-1)
 scores[name]=np.mean(score)
 print(scores)
cat_pipeline = Pipeline([ 
('selector', DataFrameSelector(cat_feats)) ,
] )<merge>
a=GradientBoostingClassifier()
a.fit(train,target)
 b=a.predict(test)
 sub['Survived']=b
full_pipeline = FeatureUnion(transformer_list=[ 
('num_pipeline', num_pipeline), 
('cat_pipeline', cat_pipeline)
 ] )<feature_engineering>
sub.to_csv('ver3.csv',index=False )