Kaggle competition: Titanic - Machine Learning from Disaster
lag_period = 24
resultsy= pd.DataFrame()
for j in range(600):
dfr = initi[initi.sat_id==j][['y','type']]
train = dfr[dfr.type=='train'].reset_index(drop=True)
 test = dfr[dfr.type=='test'].reset_index(drop=True)
 i=0
while(i*24<len(test)) :
df = pd.concat(( train, test), axis = 0, ignore_index=True)
 df['target'] = df.y
features = []
for period_mult in range(1,3,1):
df["lag_period_{}".format(period_mult)] = df.target.shift(period_mult*lag_period)
 features.append("lag_period_{}".format(period_mult))
df['lagf_mean'] = df[features].mean(axis = 1)
 features.extend(['lagf_mean'])
 model = LinearRegression()
train_df = df[df.type=='train'][features + ['target']].dropna()
test_df = df[df.type=='test'][features].reset_index(drop=True)
 test_df = test_df.loc[:23,:]
model.fit(train_df.drop(['target'], axis = 1), train_df['target'])
 forecast = model.predict(test_df)
 test.loc[i*24:i*24+23,:]['y'] = forecast
test.loc[i*24:i*24+23,:]['type'] = 'train'
i+=1
resultsy = pd.concat(( resultsy, test),axis=0, ignore_index=True )<drop_column>
train = pd.read_csv(".. /input/titanic/train.csv", index_col='PassengerId')
 test = pd.read_csv('.. /input/titanic/test.csv', index_col='PassengerId')
 train.head(10 )
resultsy.drop(['type'], axis=1, inplace=True )<drop_column>
train.corr().abs() ['Survived'].sort_values(ascending=False )
resultsz.drop(['type'], axis=1, inplace=True )<concatenate>
print(train.isnull().sum())
 print('
The observation ratio of missing Cabin values is',round(train['Cabin'].isnull().sum() /len(train),2))
print('The observation ratio of missing Age values is',round(train['Age'].isnull().sum() /len(train),2))
print('
')
 print(test.isnull().sum())
 print('
The observation ratio of missing Cabin values is',round(test['Cabin'].isnull().sum() /len(test),2))
print('The observation ratio of missing Age values is',round(test['Age'].isnull().sum() /len(test),2))
all_results = pd.concat(( resultsx,resultsy,resultsz), axis=1 )<concatenate>
train_copy = train.copy()
train_copy['Cabin'] = train_copy['Cabin'].apply(lambda x: 0 if pd.isnull(x)else 1 )
df = pd.concat(( all_results, initi[initi.type=='test'][['x_sim', 'y_sim', 'z_sim']].reset_index(drop=True)) , axis=1 )<filter>
train.corr().abs() ['Age'].sort_values(ascending=False )
df.loc[:243,:]<feature_engineering>
class age_inferer(BaseEstimator, TransformerMixin):
def __init__(self, columns=None):
self.columns = columns
def fit(self, X, y=None, **fit_params):
self.train_mean_age = X.groupby(['Pclass'])['Age'].mean()
return self
def transform(self, X, **transform_params):
X['Age'] = X.apply(lambda row: self.train_mean_age[row.Pclass] if row.Age!=row.Age else row.Age, axis=1)
 return X
df['error'] = np.linalg.norm(df[['x', 'y', 'z']].values - df[['x_sim', 'y_sim', 'z_sim']].values, axis=1 )<define_variables>
train_copy = train.copy()
train_copy['Title'] = train_copy.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
submit = df[['error']]<load_from_csv>
df = pd.DataFrame(columns = ['Feature','Count','Survived','Survivel_Percent'])
 feature = 'Title'
categories = train_copy[feature].value_counts().index.tolist()
for cat in categories:
tmp = train_copy[(train_copy[feature]==cat)]
cnt = len(tmp)
 cnt_target=len(train_copy[(train_copy[feature]==cat)&(train_copy['Survived']==1)])
 df.loc[cat] = [feature,cnt,cnt_target,cnt_target/cnt]
df.sort_index()
sub = pd.read_csv('/kaggle/input/sputnik/sub.csv' )<feature_engineering>
class feature_engineering(BaseEstimator, TransformerMixin):
def __init__(self, columns=None):
self.columns = columns
def fit(self, X, y=None, **fit_params):
return self
def transform(self, X, **transform_params):
X['Title'] = X.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
X['Title'] = X['Title'].apply(lambda x: x if x in ['Mrs', 'Master', 'Miss', 'Dr','Mr','Rev' ] else 'else')
 X['Family_Size'] = X['SibSp']+X['Parch'] + 1
X = X.drop(columns=['SibSp', 'Parch'])
 X['has_cabin'] = X['Cabin'].apply(lambda x: 0 if pd.isnull(x)else 1)
 return X
submit['id'] =(sub.reset_index() ).id<prepare_output>
cat_pipeline = Pipeline([ 
("imputer", SimpleImputer(strategy="most_frequent")) , 
("cat_encoder", OneHotEncoder()),
] )
submit.index=sub.id<drop_column>
num_attribs = ["Age", "Fare","Family_Size"]
cat_attribs = [ "Sex", "Embarked","Pclass","Title","has_cabin"]
columns_trans = ColumnTransformer([ 
("num", num_pipeline, num_attribs), 
("cat", cat_pipeline, cat_attribs),
] )
submit.drop(['id'], axis=1, inplace=True )<save_to_csv>
full_pipeline = Pipeline([ 
('age_inferer', age_inferer(['Age', 'Pclass'])) , 
('feature_engineering', feature_engineering()), 
('ColumnTransformer', columns_trans),
] )
submit.to_csv('submit1.csv' )<import_modules>
y_train = train["Survived"]
train = train.drop(columns=["Survived"])
 x_train = full_pipeline.fit_transform(train)
import numpy as np
import pandas as pd
import os<load_from_csv>
def run_classifiers(X_train: pd.DataFrame , y_train: pd.DataFrame):
df = pd.DataFrame(columns=['Model', 'Accuracy'])
 models = [ 
('LogReg', LogisticRegression()), 
('RF', RandomForestClassifier()), 
('KNN', KNeighborsClassifier()), 
('XGB', XGBClassifier()), 
('SVC',SVC(probability=True)) , 
('GBC',GradientBoostingClassifier()),
]
for name, model in models:
accuracy = cross_val_score(model, x_train, y_train, cv=5, scoring='accuracy' ).mean()
df = df.append({'Model': name, 'Accuracy': accuracy}, ignore_index = True)
 return df.sort_values(by=['Accuracy'], ascending=False )
train_dataset_full = pd.read_csv('.. /input/08_train_dataset.csv' )<load_from_csv>
run_classifiers(x_train,y_train )
test_dataset = pd.read_csv('.. /input/08_test_dataset_no_response.csv' )<import_modules>
final_classifier = SVC(probability=True)
 final_classifier.fit(x_train, y_train)
 x_test = full_pipeline.transform(test)
 result = final_classifier.predict(x_test)
<import_modules><EOS> 
submission = pd.DataFrame({'PassengerId':test.index,'Survived':result})
 submission.to_csv('submissionRF_optim_param.csv',index=False )