Kaggle competition: Titanic - Machine Learning from Disaster
combine.KitchenAbvGr.value_counts()<filter>
%matplotlib inline
cf.go_offline()
combine.KitchenAbvGr[combine.KitchenQual.isnull() ]<count_values>
train_data=pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data=pd.read_csv('/kaggle/input/titanic/test.csv')
combine.KitchenQual[combine.KitchenAbvGr == 1].value_counts()<count_values>
train_data.isnull().sum().sort_values(ascending = False )
combine['KitchenQual']=combine['KitchenQual'].fillna('TA')
 combine.KitchenQual.value_counts()<count_values>
test_data.isnull().sum().sort_values(ascending = False )
combine.Functional.value_counts()<count_values>
train_data.drop('Cabin',axis=1,inplace=True)
 test_data.drop('Cabin',axis=1,inplace=True )
combine['Functional']=combine['Functional'].fillna('Typ')
 combine.Functional.value_counts()<count_values>
def impute_age(cols):
Age=cols[0]
Pclass=cols[1]
if pd.isnull(Age):
if Pclass == 1:
return 37
elif Pclass == 2:
return 29
else:
return 24
else:
return Age
combine.FireplaceQu.value_counts()<count_values>
train_data['Age'] = train_data[['Age','Pclass']].apply(impute_age,axis=1 )
combine.Fireplaces.value_counts()<count_missing_values>
test_data['Age'] = test_data[['Age','Pclass']].apply(impute_age,axis=1 )
combine.FireplaceQu.isnull().sum()<count_values>
train_data.isnull().sum().sort_values(ascending = False )
combine['FireplaceQu']=combine['FireplaceQu'].fillna('None')
 combine.FireplaceQu.value_counts()<count_values>
test_data.isnull().sum().sort_values(ascending = False )
combine.GarageType.value_counts()<count_values>
list_of_non_numeric_data=list(train_data.select_dtypes(include='object'))
list_of_non_numeric_data
combine['GarageType']=combine['GarageType'].fillna('None')
 combine.GarageType.value_counts()<count_missing_values>
train_data.drop('Ticket',axis=1,inplace=True)
 test_data.drop('Ticket',axis=1,inplace=True )
combine['GarageYrBlt']=combine['GarageYrBlt'].fillna(0)
 combine.GarageYrBlt.isnull().sum()<count_values>
def getTitles(name):
name = str(name)
 title = name.split('.')[0]
title = title.split(',')
 return title[1]
combine.GarageFinish.value_counts()<count_values>
train_data['Title'] = train_data['Name'].apply(getTitles)
 train_data['Title']
combine['GarageFinish']=combine['GarageFinish'].fillna('None')
 combine.GarageFinish.value_counts()<count_values>
test_data['Title'] = test_data['Name'].apply(getTitles)
 test_data['Title']
combine.GarageCars.value_counts()<count_values>
def cleanTitle(title):
if title in [' Mr',' Mrs',' Master',' Miss']:
return title
else:
return "Others"
combine['GarageCars']=combine['GarageCars'].fillna(0)
 combine.GarageCars.value_counts()<feature_engineering>
train_data['Title'] = train_data['Title'].apply(cleanTitle)
 test_data['Title'] = test_data['Title'].apply(cleanTitle )
combine['GarageArea']=combine['GarageArea'].fillna(0)
 combine.GarageArea.isnull().sum()<count_values>
Title_train = pd.get_dummies(train_data['Title'],drop_first=True)
 Title_test = pd.get_dummies(test_data['Title'],drop_first=True)
 sex_train = pd.get_dummies(train_data['Sex'],drop_first=True)
 embark_train = pd.get_dummies(train_data['Embarked'],drop_first=True)
 sex_test = pd.get_dummies(test_data['Sex'],drop_first=True)
 embark_test = pd.get_dummies(test_data['Embarked'],drop_first=True)
combine.GarageQual.value_counts()<count_values>
train_data.drop(['Sex','Embarked','Name','Title'],axis=1,inplace=True)
 test_data.drop(['Sex','Embarked','Name','Title'],axis=1,inplace=True )
combine['GarageQual']=combine['GarageQual'].fillna('None')
 combine.GarageQual.value_counts()<count_values>
train_data=pd.concat([train_data,sex_train,embark_train,Title_train],axis=1)
 test_data=pd.concat([test_data,sex_test,embark_test,Title_test],axis=1 )
combine.GarageCond.value_counts()<count_values>
from sklearn.preprocessing import MinMaxScaler
combine['GarageCond']=combine['GarageCond'].fillna('None')
 combine.GarageCond.value_counts()<count_values>
X_train=train_data.drop(['Survived','PassengerId'],axis=1)
 y_train= train_data['Survived']
X_test=test_data.drop(['PassengerId'],axis=1 )
combine.PoolArea.value_counts()<count_values>
Scaler=MinMaxScaler()
combine.PoolQC.value_counts()<count_values>
X_train = Scaler.fit_transform(X_train)
 X_test = Scaler.transform(X_test )
combine['PoolQC']=combine['PoolQC'].fillna('None')
 combine.PoolQC.value_counts()<count_values>
from sklearn.linear_model import LogisticRegression
combine.Fence.value_counts()<count_values>
logmodel = LogisticRegression(max_iter=10000)
 logmodel.fit(X_train,y_train )
combine['Fence']=combine['Fence'].fillna('None')
 combine.Fence.value_counts()<count_values>
test_data['Survived']=logmodel.predict(X_test )
<count_values><EOS> 
test_data[['PassengerId', 'Survived']].to_csv('kaggle_submission.csv', index = False )