Kaggle competition: Titanic - Machine Learning from Disaster
pred_test = model.predict(X_test )<save_to_csv>
%matplotlib inline
data_train = pd.read_csv('.. /input/train.csv')
 data_test = pd.read_csv('.. /input/test.csv')
 data_train.sample(3)
pred_test = pd.Series(pred_test.reshape(-1), name='is_attributed')
 sub = pd.concat([click_id, pred_test], axis=1)
 sub.to_csv('sub.csv', index=False )<load_pretrained>
data_train.isna().sum()
z = zipfile.ZipFile('.. /input/train.csv.zip')
 df = pd.read_csv(z.open('train.csv'))
z = zipfile.ZipFile('.. /input/test.csv.zip')
 test = pd.read_csv(z.open('test.csv'))
def hr_func(ts):
return(float )(ts[11:13])
 df['Dates'] = df['Dates'].apply(hr_func)
 df['HourCos']=0
df['HourSin']=0
def hourtocos(ts):
ts=ts*2*math.pi/24
return math.cos(ts)
 def hourtosin(ts):
ts=ts*2*math.pi/24
return math.sin(ts)
 df['HourCos']=df['Dates'].apply(hourtocos)
 df['HourSin']=df['Dates'].apply(hourtosin)
 X = df[['X','Y','HourCos','HourSin']]
Y=df[['Category']]
clf=RandomForestClassifier(n_estimators=100,min_samples_split=350)
 clf.fit(X,Y)
 outcomes = clf.predict_proba() 
<define_variables>
features=['Age','Pclass','SibSp','Parch','Fare']
data_train_numeric=data_train[features].as_matrix()
data_test_numeric=data_test[features].as_matrix()
data_train_imputed=pd.DataFrame(KNN(6 ).complete(data_train_numeric),index=data_train.index)
 data_test_imputed=pd.DataFrame(KNN(6 ).complete(data_test_numeric),index=data_test.index)
 data_train['Age_imputed']=data_train_imputed.iloc[:,0]
data_test['Age_imputed']=data_train_imputed.iloc[:,0]
outcomes<0.01<set_options>
data_train[data_train['Age'].isnull() ].head()
data_train=data_train.drop('Age',axis=1)
 data_test=data_test.drop('Age',axis=1)
import pandas as pd
import numpy as np
import gc
from sklearn.metrics import roc_auc_score
from collections import defaultdict
from tqdm.notebook import tqdm
import lightgbm as lgb
import riiideducation
import matplotlib.pyplot as plt
import seaborn as sns
import random
import os 
<define_variables>
data_train['Age']=data_train['Age_imputed']
data_test['Age']=data_test['Age_imputed']
data_train=data_train.drop('Age_imputed',axis=1)
 data_test=data_test.drop('Age_imputed',axis=1)
 data_train.head()
class UserFeats(object):
def __init__(
 self 
):
self._ans_cnt = 0
self._ans_corr_cnt = 0
self._questions_seen_id = dict()
self._part_info = dict()
self._last_container = -1
self._last_timestamp = [np.nan for i in range(4)]
self._last_correct_timestamp = [np.nan, np.nan]
self._last_incorrect_timestamp = [np.nan, np.nan]
self._total_prequestions_time_count = 0
self._total_prexlpan_count = 0
self._difficult_solved_count = 0
self._simple_mistake_count = 0
self._lecture_worked = 0
self._past_lecture_time = np.nan
def update_lc(self, time):
self._lecture_worked += 1
self._past_lecture_time = time 
<init_hyperparams>
def encode_features(df_train, df_test):
features = [ 'Sex', 'Lname', 'Title','Age','Fare']
df_combined = pd.concat([df_train[features], df_test[features]])
 for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(df_combined[feature])
 df_train[feature] = le.transform(df_train[feature])
 df_test[feature] = le.transform(df_test[feature])
 return df_train, df_test
data_train, data_test = encode_features(data_train, data_test)
 data_train.head()
class QuesFeats(object):
def __init__(self, feats_tuple):
self.part = feats_tuple[0]
self.crr_cnt = feats_tuple[1]
self.total_cnt = feats_tuple[2]
self.explan_false_mean = feats_tuple[4]
self.explan_true_mean = feats_tuple[5]
self.var = feats_tuple[6]
self.bundle_num = feats_tuple[7]
self.part_mean_correct = feats_tuple[8]
self.part_total_correct = feats_tuple[9]
self.part_total_count = feats_tuple[13]
self.part_var = feats_tuple[10]
self.part_bundle_id = feats_tuple[11]
self.content_explan_sum = feats_tuple[12]
self.tags_lsi = feats_tuple[15]
def get_acc(self, c_id):
return self.crr_cnt[c_id]/self.total_cnt[c_id]
def get_var(self, c_id):
return self.var[c_id] 
<merge>
X_all = data_train.drop(['Survived','Cabin', 'PassengerId'], axis=1)
 y_all = data_train['Survived']
num_test = 0.20
X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test, random_state=23 )
def df_preprocessing(df, q_tmp):
df['prior_question_had_explanation'] = df.prior_question_had_explanation.fillna(False ).astype('int8')
 df['timestamp'] = df['timestamp']/(1000*1000)
 df['prior_question_elapsed_time'] = df['prior_question_elapsed_time']/(1000*1000)
 df['prior_question_elapsed_time'].fillna(0.013, inplace = True)
 df = pd.merge(df, q_tmp[['question_id', 'part']], left_on = 'content_id', right_on = 'question_id', how = 'left')
 del q_tmp
return df<define_variables>
from sklearn.model_selection import GridSearchCV
def handle_features(df,user_dict,content_f, add):
if add:
row = df[['user_id', 'answered_correctly', 'task_container_id', 'content_id', 'part', 'prior_question_elapsed_time', 'prior_question_had_explanation','timestamp']]
res = dict()
total = user_dict[row[0]]._ans_cnt if user_dict[row[0]]._ans_cnt !=0 else 1
if row[4] in user_dict[row[0]]._part_info:
if row[3] not in user_dict[row[0]]._questions_seen_id:
user_dict[row[0]]._questions_seen_id[row[3]] = 0
else:
user_dict[row[0]]._part_info[row[4]] = [0,0]
user_dict[row[0]]._questions_seen_id[row[3]] = 0
user_dict[row[0]]._part_info[row[4]] = [0,0]
res['task_container_id'] = row[2]
res['content_id'] = row[3]
res['part'] = row[4]
res['prior_question_elapsed_time'] = row[5]
res['prior_question_had_explanation'] = row[6]
res['timestamp'] = row[7]
res['u_answered_correctly_count'] = user_dict[row[0]]._ans_corr_cnt
res['u_answered_incorrectly_count'] = user_dict[row[0]]._ans_cnt - user_dict[row[0]]._ans_corr_cnt
res['u_answered_correctly_avg'] = user_dict[row[0]]._ans_corr_cnt/total
res['u_elapsed_time_avg'] = user_dict[row[0]]._total_prequestions_time_count/total
res['u_explanation_avg'] = user_dict[row[0]]._total_prexlpan_count/total
if row[2] == user_dict[row[0]]._last_container:
res['timestamp_u_recency_1'] = row[7] - user_dict[row[0]]._last_timestamp[0]
res['timestamp_u_recency_2'] = row[7] - user_dict[row[0]]._last_timestamp[1]
res['timestamp_u_recency_3'] = row[7] - user_dict[row[0]]._last_timestamp[2]
res['u_timestamp_correct_recency'] = row[7] - user_dict[row[0]]._last_correct_timestamp[0]
res['u_timestamp_incorrect_recency'] = row[7] - user_dict[row[0]]._last_incorrect_timestamp[0]
else:
res['timestamp_u_recency_1'] = row[7] - user_dict[row[0]]._last_timestamp[1]
res['timestamp_u_recency_2'] = row[7] - user_dict[row[0]]._last_timestamp[2]
res['timestamp_u_recency_3'] = row[7] - user_dict[row[0]]._last_timestamp[3]
res['u_timestamp_correct_recency'] = row[7] - user_dict[row[0]]._last_correct_timestamp[1]
res['u_timestamp_incorrect_recency'] = row[7] - user_dict[row[0]]._last_incorrect_timestamp[1]
res['upart_answered_incorrect'] = user_dict[row[0]]._part_info[row[4]][1] - user_dict[row[0]]._part_info[row[4]][0]
res['upart_answered_correct'] = user_dict[row[0]]._part_info[row[4]][0]
res['upart_answered_crr_mean'] = user_dict[row[0]]._part_info[row[4]][0] /(user_dict[row[0]]._part_info[row[4]][1] if user_dict[row[0]]._part_info[row[4]][1] !=0 else 1)
 res['upart_answered_incorrect'] = 0
res['upart_answered_correct'] = 0
res['upart_answered_crr_mean'] = 0
res['u_lecture_reviewed'] = user_dict[row[0]]._lecture_worked
res['u_lecture_avg'] = user_dict[row[0]]._lecture_worked/(user_dict[row[0]]._lecture_worked + total)
 res['u_last_lecture_timedif'] = row[7] - user_dict[row[0]]._past_lecture_time
res['u_solved_diffcult_questions_num'] = user_dict[row[0]]._difficult_solved_count
res['u_failed_simple_questions_num'] = user_dict[row[0]]._simple_mistake_count
res['u_questions_seen_time'] = 0.5
res['explan_false_mean'] = content_f.explan_false_mean[int(row[3])]
res['explan_true_mean'] = content_f.explan_true_mean[int(row[3])]
res['crr_cnt'] = content_f.crr_cnt[int(row[3])]
res['incrr_cnt'] = content_f.total_cnt[int(row[3])] - content_f.crr_cnt[int(row[3])]
res['var'] = content_f.var[int(row[3])]
res['bundle_num'] = content_f.bundle_num[int(row[3])]
res['part_mean_correct'] = content_f.part_mean_correct[int(row[3])]
res['part_total_correct'] = content_f.part_total_correct[int(row[3])]
res['part_total_incorrect'] = content_f.part_total_count[int(row[3])] - content_f.part_total_correct[int(row[3])]
res['part_var'] = content_f.part_var[int(row[3])]
res['part_bundle_id'] = content_f.part_bundle_id[int(row[3])]
res['content_explan_sum'] = content_f.content_explan_sum[int(row[3])]
res['tags_lsi'] = content_f.tags_lsi[int(row[3])]
return res
for num, row in tqdm(enumerate(df[['user_id', 'answered_correctly', 'task_container_id', 'content_id', 'part', 'prior_question_elapsed_time', 'prior_question_had_explanation','timestamp']].values)) :
if row[0] not in user_dict:
user_dict[row[0]] = UserFeats()
if row[4] in user_dict[row[0]]._part_info:
if row[3] not in user_dict[row[0]]._questions_seen_id:
user_dict[row[0]]._questions_seen_id[row[3]] = 0
else:
user_dict[row[0]]._part_info[row[4]] = [0,0]
user_dict[row[0]]._questions_seen_id[row[3]] = 0
user_dict[row[0]]._part_info[row[4]] = [0,0]
if row[1] == -1:
user_dict[row[0]]._past_lecture_time = row[7]
user_dict[row[0]]._lecture_worked += 1
continue
user_dict[row[0]]._ans_cnt += 1
user_dict[row[0]]._ans_corr_cnt += row[1]
user_dict[row[0]]._part_info[row[4]][0] += row[1]
user_dict[row[0]]._part_info[row[4]][1] += 1
if user_dict[row[0]]._questions_seen_id[row[3]] < 8:
user_dict[row[0]]._questions_seen_id[row[3]] += 1
if row[1] == 1:
if user_dict[row[0]]._last_correct_timestamp[1] != row[7]:
user_dict[row[0]]._last_correct_timestamp.pop(0)
 user_dict[row[0]]._last_correct_timestamp.append(row[7])
 else:
if user_dict[row[0]]._last_incorrect_timestamp[1] != row[7]:
user_dict[row[0]]._last_incorrect_timestamp.pop(0)
 user_dict[row[0]]._last_incorrect_timestamp.append(row[7])
 user_dict[row[0]]._total_prequestions_time_count += row[5]
user_dict[row[0]]._total_prexlpan_count += row[6]
if row[2] != user_dict[row[0]]._last_container:
user_dict[row[0]]._last_container = row[2]
user_dict[row[0]]._last_timestamp.pop(0)
 user_dict[row[0]]._last_timestamp.append(row[7])
 if content_f.get_acc(int(row[3])) < 0.574455 and row[1] == 1:
user_dict[row[0]]._difficult_solved_count += 1
if content_f.get_acc(int(row[3])) > 0.856363 and row[1] == 0:
user_dict[row[0]]._simple_mistake_count += 1<define_variables>
param_grid={'C':[0.001,0.01,0.1,1,10,100]}
clf=LogisticRegression()
grid=GridSearchCV(clf,param_grid,cv=10,scoring='accuracy' ).fit(X_train,y_train)
 y_pred=grid.predict(X_test)
 acc_lreg=round(accuracy_score(y_pred,y_test)*100,2)
 print("Accuracy Score for Logestic Regression {0}".format(acc_lreg))
def process_questions(filepath):
default_list = []
q_features = [
'part',
'content_correct_num',
'content_total_num',
'content_correct_mean',
'content_explation_false_mean',
'content_explation_true_mean',
'var',
'bundle_num',
'part_mean_correct',
'part_total_correct',
'part_var',
'part_bundle_id',
'content_explan_sum',
'part_num_user',
'part_num_c',
'tags_lsi',
]
q = pd.read_pickle(filepath)[q_features]
part = tuple(q['part'].values)
 for i in q.keys() :
default_list.append(tuple(q[i].values))
q_list = tuple(default_list)
 q_feats = QuesFeats(q_list)
 del default_list, q_list, q
gc.collect()
return q_feats 
<load_from_csv>
clf=SVC()
Cs=[0.001,0.01,0.1,1,10,]
gammas=[0.001,0.01,0.1,1]
param_grid={'C':Cs,'gamma':gammas}
grid=GridSearchCV(clf,param_grid,cv=10,scoring='accuracy' ).fit(X_train,y_train)
 y_pred=grid.predict(X_test)
 acc_svc=round(accuracy_score(y_pred,y_test)*100,2)
 print("Accuracy Score for Support Vector Machine{0}".format(acc_svc))
def read_and_preprocess(feature_engineering = False):
train = '.. /input/riiid-test-answer-prediction/train.csv'
question_file = '.. /input/qdataset/questions_features.pkl'
feld_needed = ['timestamp', 'user_id', 'answered_correctly', 'content_id', 'content_type_id', 'prior_question_elapsed_time', 'prior_question_had_explanation']
q_tmp= pd.read_csv('.. /input/riiid-test-answer-prediction/questions.csv')
 q_tmp['part'] = q_tmp['part'].astype(np.int32)
 print('User feature calculation started...')
 print('
')
 user_dict = dict()
content_f = process_questions(question_file)
 chunk_round = 0
reader = pd.read_csv(train, iterator=True, chunksize=10000000)
 for chunk in reader:
print('chunk', chunk_round, 'started.... ')
 chunk = df_preprocessing(chunk, q_tmp)
 handle_features(chunk, user_dict, content_f, False)
 del chunk
gc.collect()
chunk_round += 1
print('User feature calculation completed...')
 print('
')
 return user_dict,content_f 
<load_pretrained>
clf=DecisionTreeClassifier()
param_grid={'max_depth':[2,4,6,8,10],'max_features':[2,3,4,5,6,7]}
grid=GridSearchCV(clf,param_grid,cv=10,scoring='accuracy' ).fit(X_train,y_train)
 y_pred=grid.predict(X_test)
 acc_dtree=round(accuracy_score(y_pred,y_test)*100,2)
 print("Accuracy score for decision tree{0}".format(acc_dtree))
user_dict,content_f = read_and_preprocess()
model = lgb.Booster(model_file='.. /input/trainmodel/model.txt')
 print('model load done.... ' )<define_variables>
clf=RandomForestClassifier()
param_grid={'max_depth':[2,4,6,8,10],'max_features':[2,3,4,5,6,7]}
grid=GridSearchCV(clf,param_grid,cv=10,scoring='accuracy' ).fit(X_train,y_train)
 y_pred=grid.predict(X_test)
 acc_rforest=round(accuracy_score(y_pred,y_test)*100,2)
 print("RandomForestAccuracy Score{0} ".format(acc_rforest))
TARGET = ['answered_correctly']
FEATURES = [
'timestamp',
'content_id',
'task_container_id',
'prior_question_elapsed_time',
'prior_question_had_explanation',
'part',
'u_answered_correctly_count',
'u_answered_correctly_avg',
'u_elapsed_time_avg',
'u_explanation_avg',
'timestamp_u_recency_1',
'timestamp_u_recency_2',
'timestamp_u_recency_3',
'u_timestamp_correct_recency',
'u_timestamp_incorrect_recency',
'upart_answered_incorrect',
'upart_answered_correct',
'upart_answered_crr_mean',
'u_lecture_reviewed',
'u_lecture_avg',
'u_last_lecture_timedif',
'u_solved_diffcult_questions_num',
'u_failed_simple_questions_num',
'u_questions_seen_time',
'explan_false_mean',
'explan_true_mean',
'crr_cnt',
'incrr_cnt',
'var',
'bundle_num',
'part_mean_correct',
'part_total_correct',
'part_total_incorrect',
'part_var',
'part_bundle_id',
'content_explan_sum',
'tags_lsi',
]
q_tmp= pd.read_csv('.. /input/riiid-test-answer-prediction/questions.csv')
 q_tmp['part'] = q_tmp['part'].astype(np.int32)
 env = riiideducation.make_env()
iter_test = env.iter_test()
previous_test_df = None
for(test_df, sample_prediction_df)in iter_test:
if previous_test_df is not None:
previous_test_df[TARGET] = eval(test_df["prior_group_answers_correct"].iloc[0])
 handle_features(previous_test_df, user_dict, content_f, False)
 test_df = df_preprocessing(test_df, q_tmp)
 previous_test_df = test_df.copy()
test_df[TARGET] = -2
test_df[TARGET] = test_df[TARGET].astype('float32')
 print(test_df.keys())
 for index, row in test_df.iterrows() :
try:
if row['user_id'] not in user_dict:
user_dict[row['user_id']] = UserFeats()
if row['content_type_id'] == 1:
user_dict[row['user_id']].update_lc(row['timestamp'])
 continue
if row['content_type_id'] == 0:
predict_dict = handle_features(row, user_dict, content_f, True)
 predict_list = []
for i in FEATURES:
predict_list.append(predict_dict[i])
 prd_value = float(model.predict([predict_list])[0])
 print('prd_value', prd_value)
 test_df.loc[test_df.row_id == row.row_id, TARGET] = prd_value
except Exception as e:
print(e)
 pass
env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )<import_modules>
clf=KNeighborsClassifier()
param_grid={'n_neighbors':[2,4,6,8,10],'weights':['uniform','distance']}
grid=GridSearchCV(clf,param_grid,cv=10,scoring='accuracy' ).fit(X_train,y_train)
 y_pred=grid.predict(X_test)
 acc_knn=round(accuracy_score(y_pred,y_test)*100,2)
 print("KnearrestNeighbors Accuracy Score{0} ".format(acc_knn))
import numpy as np
import pandas as pd
from tqdm.notebook import tqdm
import gc
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier
import optuna
from optuna.samplers import TPESampler
from sklearn.metrics import roc_auc_score
import riiideducation
import os<load_from_csv>
clf=GradientBoostingClassifier()
param_grid={'max_depth':[2,4,6,8,10],'max_features':[2,3,4,5,6,7]}
grid=GridSearchCV(clf,param_grid,cv=10,scoring='accuracy' ).fit(X_train,y_train)
 y_pred=grid.predict(X_test)
 acc_gbdtree=round(accuracy_score(y_pred,y_test)*100,2)
 print("GradientboostingClassifier accuracy Score {0}".format(acc_gbdtree))
%%time
used_data_types_dict = {
'question_id': 'int16',
'bundle_id': 'int16',
'correct_answer': 'int8',
'part': 'int8',
'tags': 'str',
}
questions = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols = used_data_types_dict.keys() , dtype=used_data_types_dict)
 lectures_df = pd.read_csv('.. /input/riiid-test-answer-prediction/lectures.csv')
<load_pretrained>
model=pd.DataFrame({'model':['GradientboostingClassifier','KnearrestNeighbors','RandomForest','DecisionTreeClassifier','SupportVectormachine','LogisticRegression'] ,'Acc_Score':[acc_gbdtree,acc_knn,acc_rforest,acc_dtree,acc_svc,acc_lreg]})
 model.sort_values('Acc_Score',ascending=False )
%%time
features_df = pd.read_pickle('.. /input/riiid-splitting-train-and-test-data/features_q_only.pkl.zip')
 train_df = pd.read_pickle('.. /input/riiid-splitting-train-and-test-data/train_q_only.pkl.zip' )<feature_engineering>
clf = RandomForestClassifier()
parameters = {'n_estimators': [4, 6, 9,12],
'max_features': ['log2', 'sqrt','auto'],
'criterion': ['entropy', 'gini'],
'max_depth': [2, 3, 5, 10],
'min_samples_split': [2, 3, 5],
'min_samples_leaf': [1,5,8]
}
acc_scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring=acc_scorer)
 grid_obj = grid_obj.fit(X_train, y_train)
 clf = grid_obj.best_estimator_
clf.fit(X_train, y_train)
def add_seen_before_to_train_df(features_df, train_df):
train_questions_only_df = features_df[features_df['answered_correctly']!=-1]
state = dict()
for user_id in train_questions_only_df['user_id'].unique() :
state[user_id] = {}
total = len(state.keys())
 user_content = train_questions_only_df.groupby('user_id')['content_id'].apply(np.array ).apply(np.sort ).apply(np.unique)
 user_attempts = train_questions_only_df.groupby(['user_id', 'content_id'])['content_id'].count().astype(np.uint8 ).groupby('user_id' ).apply(np.array ).values
for user_id, content, attempt in tqdm(zip(state.keys() , user_content, user_attempts),total=total):
state[user_id] = dict(zip(content, attempt))
del user_content, user_attempts, train_questions_only_df
big_list=[]
for pair in tqdm(train_df[['user_id','content_id']].values):
if pair[0] in state:
if pair[1] in state[pair[0]]:
big_list.append(state[pair[0]][pair[1]])
 state[pair[0]][pair[1]]+=1
else:
big_list.append(0)
 state[pair[0]][pair[1]]=1
else:
big_list.append(0)
 state[pair[0]]={pair[1]:1}
del state
train_df['seen_before']=big_list
train_df.seen_before = train_df.seen_before.clip(upper=5)
 del big_list
gc.collect()
return(train_df)
 train_df = add_seen_before_to_train_df(features_df, train_df )<categorify>
predictions = clf.predict(X_test)
 print(accuracy_score(y_test, predictions))
lects_df = features_df[features_df['answered_correctly']==-1]
lect_seen_df = pd.DataFrame(data=lects_df.user_id.value_counts())
 lect_seen_df.columns=['lectures_seen']
lect_seen_df.lectures_seen = lect_seen_df.lectures_seen.astype(float)
 lectures_df['type_of'] = lectures_df['type_of'].replace('solving question', 'solving_question')
 lectures_df = pd.get_dummies(lectures_df, columns=['part', 'type_of'])
 part_lectures_columns = [column for column in lectures_df.columns if column.startswith('part')]
types_of_lectures_columns = [column for column in lectures_df.columns if column.startswith('type_of_')]
lectures_df = lectures_df.set_index('lecture_id')
 train_lectures = features_df[features_df.answered_correctly == -1].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left')
 user_lecture_stats_part = train_lectures.groupby('user_id')[part_lectures_columns + types_of_lectures_columns].sum()
user_lecture_stats_part = user_lecture_stats_part.merge(lect_seen_df, left_index=True, right_index=True)
 user_lecture_stats_part.index.name = 'user_id'
user_lecture_stats_part['lectures_seen'] = user_lecture_stats_part['lectures_seen'].astype(int)
 del lects_df, lect_seen_df, train_lectures
user_lecture_stats_part<merge>
def run_kfold(clf):
kf = KFold(891, n_folds=10)
 outcomes = []
fold = 0
for train_index, test_index in kf:
fold += 1
X_train, X_test = X_all.values[train_index], X_all.values[test_index]
y_train, y_test = y_all.values[train_index], y_all.values[test_index]
clf.fit(X_train, y_train)
 predictions = clf.predict(X_test)
 accuracy = accuracy_score(y_test, predictions)
 outcomes.append(accuracy)
 print("Fold {0} accuracy: {1}".format(fold, accuracy))
mean_outcome = np.mean(outcomes)
 print("Mean Accuracy: {0}".format(mean_outcome))
run_kfold(clf)
train_questions_only_df = features_df[features_df['answered_correctly']!=-1]
train_questions_only_df = pd.merge(train_questions_only_df, questions[['part','tags']],
left_on='content_id', right_index=True, how = 'left')
 del features_df<data_type_conversions>
ids = data_test['PassengerId']
predictions = clf.predict(data_test.drop(['PassengerId','Cabin'], axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('titanic-predictions.csv', index = False)
 output.head()