Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
sns.set(style = 'whitegrid')
 sns.distributions._has_statsmodels = False
test_ingr_count = {}
m = test_data.shape[0]
for i in range(m):
for j in test_data["ingredients"][i]:
if j in test_ingr_count.keys() :
test_ingr_count[j] += 1
else:
test_ingr_count[j] = 1
len(test_ingr_count )<feature_engineering>
data_train = pd.read_csv('.. /input/titanic/train.csv')
 data_test = pd.read_csv('.. /input/titanic/test.csv' )
train_ingr_missing = []
for i in test_ingr_count.keys() :
if i not in train_ingr_count.keys() :
train_ingr_missing.append(i)
 print(len(train_ingr_missing))
for i in train_ingr_missing:
train_ingr_count[i] = 0
print(len(train_ingr_count))<feature_engineering>
train = data_train.copy()
test = data_test.copy()
test_ingr_missing = []
for i in train_ingr_count.keys() :
if i not in test_ingr_count.keys() :
test_ingr_missing.append(i)
 print(len(test_ingr_missing))
for i in test_ingr_missing:
test_ingr_count[i] = 0
print(len(test_ingr_count))<feature_engineering>
train['Cabin'] = train['Cabin'].str.get(0)
 test['Cabin'] = test['Cabin'].str.get(0 )
for i in train_ingr_count.keys() :
train_data[i] = np.zeros(len(train_data))<feature_engineering>
num_data = train[['Age', 'SibSp', 'Parch', 'Fare']]
cat_data = train[['Survived', 'Pclass', 'Sex', 'Cabin', 'Embarked']]
for i in test_ingr_count.keys() :
test_data[i] = np.zeros(len(test_data))<feature_engineering>
print('Train :
',train.isnull().sum())
 print('
')
 print('Test :
', test.isnull().sum() )
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
train_data[j].iloc[i] = 1<feature_engineering>
train['Age'].fillna(train['Age'].median() , inplace = True)
 test['Age'].fillna(train['Age'].median() , inplace = True)
 train['Fare'].fillna(train['Fare'].median() , inplace = True)
 test['Fare'].fillna(train['Fare'].median() , inplace = True)
 train.dropna(subset=['Embarked'] , inplace = True )
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
test_data[j].iloc[i] = 1<drop_column>
train.drop(['Cabin'], axis = 1, inplace = True)
 test.drop(['Cabin'], axis = 1, inplace = True )
train_data.drop('ingredients',axis=1,inplace=True)
 test_data.drop('ingredients',axis=1,inplace=True )<drop_column>
print('Train :
',train.isnull().sum())
 print('
')
 print('Test :
', test.isnull().sum() )
test_data = test_data[train_data.drop('cuisine',axis=1 ).columns]<split>
train['LastName'] = train['Name'].str.split(',', expand=True)[0]
test['LastName'] = test['Name'].str.split(',', expand=True)[0]
X = train_data.drop(['id','cuisine'],axis=1)
 y = train_data['cuisine']
X_train,X_test,y_train,y_test = train_test_split(X,y)
 print(X_test.shape,y_test.shape)
 print(X_train.shape,y_train.shape )<train_model>
train['Train'] = 1
test['Train'] = 0
alldata = pd.concat(( train, test), sort = False ).reset_index(drop = True)
 sur_data = []
died_data = []
for index, row in alldata.iterrows() :
s = alldata[(alldata['LastName']==row['LastName'])&(alldata['Survived']==1)]
d = alldata[(alldata['LastName']==row['LastName'])&(alldata['Survived']==0)]
s=len(s)
 if row['Survived'] == 1:
s-=1
d=len(d)
 if row['Survived'] == 0:
d-=1
sur_data.append(s)
 died_data.append(d)
 alldata['FamilySurvived'] = sur_data
alldata['FamilyDied'] = died_data
lr = LogisticRegression()
lr.fit(X_train,y_train)
 lr.score(X_test,y_test )<predict_on_test>
train = alldata[alldata['Train'] == 1]
test = alldata[alldata['Train'] == 0]
pred = lr.predict(test_data.drop('id',axis=1))<create_dataframe>
train['Fare'] = np.log1p(train['Fare'])
 test['Fare'] = np.log1p(test['Fare'] )
submission = pd.DataFrame(data=pred,columns=['cuisine'])
 submission['id'] = test_data['id']
submission.set_index("id",inplace=True )<save_to_csv>
le = LabelEncoder()
le.fit(train['Pclass'])
 train['Pclass'] = le.transform(train['Pclass'])
 ohe = OneHotEncoder(sparse = False, drop = 'first', categories = 'auto')
 ohe.fit(train[['Sex', 'Embarked']])
 ohecategory_train = ohe.transform(train[['Sex', 'Embarked']])
 ohecategory_test = ohe.transform(test[['Sex', 'Embarked']])
 for i in range(ohecategory_train.shape[1]):
train['dummy_variable_' + str(i)] = ohecategory_train[:,i]
for i in range(ohecategory_test.shape[1]):
test['dummy_variable_' + str(i)] = ohecategory_test[:,i]
print('Train shape :', train.shape)
 print('Test shape :', test.shape )
submission.to_csv('submission.csv' )<import_modules>
sc = StandardScaler()
sc.fit(train[['Age', 'SibSp', 'Parch', 'Fare']])
 train[['Age', 'SibSp', 'Parch', 'Fare']] = sc.transform(train[['Age', 'SibSp', 'Parch', 'Fare']])
 test[['Age', 'SibSp', 'Parch', 'Fare']] = sc.transform(test[['Age', 'SibSp', 'Parch', 'Fare']])
import pandas as pd
import json
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer<load_from_disk>
train.drop(['PassengerId', 'Name', 'Sex', 'Ticket', 'Embarked', 'LastName', 'Train'], axis = 1, inplace = True)
 test.drop(['PassengerId', 'Name', 'Sex', 'Ticket', 'Embarked', 'LastName', 'Train'], axis = 1, inplace = True )
with open('.. /input/train.json')as train_data:
data = json.load(train_data )<define_variables>
X_train = train.iloc[:, 1:].values
y_train = train.iloc[:, 0].values
X_test = test.iloc[:, 1:].values
y_test = test.iloc[:, 0].values
print('X_train :
', X_train[0:5])
 print('y_train :
', y_train[0:5] )
features = [x['ingredients'] for x in data]<define_variables>
clf = KNeighborsClassifier(leaf_size = 1, metric = 'minkowski', n_neighbors = 12, p = 1, weights = 'distance')
 accuracies = cross_val_score(clf, X_train, y_train, cv = 10)
 print('Accuracies : ', accuracies)
 print('AVG Accuracies : ', accuracies.mean())
 print('STD:',accuracies.std())
features_list = []
for feature in features:
single_item = ''
for item in feature:
item= item.replace(' ','-')
 single_item = single_item + ' ' + item
single_item = single_item[1:]
features_list.append(single_item )<feature_engineering>
clf.fit(X_train, y_train)
 y_pred = clf.predict(X_test)
 y_pred = y_pred.astype('int64')
 submission = pd.DataFrame()
submission['PassengerId'] = data_test['PassengerId']
submission['Survived'] = y_pred
submission['Survived'].value_counts()
<categorify><EOS> 
submission.to_csv(r'Submission.csv', index = False, header = True )