Kaggle competition: Titanic - Machine Learning from Disaster
train2 = train
print(( train2['ingredients'][0]))
print(arraytotext(train2['ingredients'][0]))<categorify>
%matplotlib inline
train_features = tfidf.fit_transform(arraytotext(train['ingredients']))
test_features = tfidf.transform(arraytotext(test['ingredients']))<choose_model_class>
df_train = pd.read_csv(".. /input/titanic/train.csv")
 df_test = pd.read_csv(".. /input/titanic/test.csv" )
classifier = SVC(C=200, kernel='rbf', degree=3,gamma=1, \
coef0=1, shrinking=True,tol=0.001, probability=False,\
cache_size=200,class_weight=None, verbose=False,\
max_iter=-1,decision_function_shape=None,\
random_state=None )<compute_train_metric>
df_train.isnull().sum()
model = OneVsRestClassifier(classifier)
 scores = cross_val_score(classifier,train_features, ytrain, cv=2)
 print("Accuracy: %0.2f(+/- %0.2f)" % \ 
(scores.mean() , scores.std() * 2))<train_model>
df_test.isnull().sum()
model.fit(train_features, ytrain )<predict_on_test>
df_train.drop(columns = ["Name" ,"Ticket" , "Cabin"], inplace=True)
 df_test.drop(columns = ["Name" ,"Ticket" , "Cabin"], inplace=True )
predictions = model.predict(test_features)
 print(predictions )<save_to_csv>
df_train.Sex.replace("male" , 0 , inplace =True)
 df_train.Sex.replace("female" , 1 , inplace =True)
 df_train.Embarked.replace("C" , 0 , inplace =True)
 df_train.Embarked.replace("S" , 1 , inplace =True)
 df_train.Embarked.replace("Q" , 2 , inplace =True )
submission = pd.DataFrame()
submission['id'] = Id
submission['cuisine'] = predictions
submission.to_csv('submission.csv', index=False)
<import_modules>
df_test.Sex.replace("male" , 0 , inplace =True)
 df_test.Sex.replace("female" , 1 , inplace =True)
 df_test.Embarked.replace("C" , 0 , inplace =True)
 df_test.Embarked.replace("S" , 1 , inplace =True)
 df_test.Embarked.replace("Q" , 2 , inplace =True )
tqdm.pandas()<load_from_disk>
df_train.Age.fillna(df_train.Age.median() , inplace= True)
 df_test.Age.fillna(df_train.Age.median() , inplace = True )
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json' )<feature_engineering>
df_test.Fare.fillna(df_train.Fare.median() , inplace = True )
train['num_ingredients'] = train['ingredients'].apply(len)
 train = train[train['num_ingredients'] > 1]<string_transform>
df_test.isnull().sum()
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text = ' '.join(ingredients)
 ingredients_text = ingredients_text.lower()
ingredients_text = ingredients_text.replace('-', ' ')
 words = []
for word in ingredients_text.split() :
if re.findall('[0-9]', word): continue
if len(word)<= 2: continue
if '’' in word: continue
word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return ' '.join(words)
 for ingredient, expected in [ 
('Eggs', 'egg'), 
('all-purpose flour', 'all purpose flour'), 
('purée', 'purée'), 
('1% low-fat milk', 'low fat milk'), 
('half & half', 'half half'), 
('safetida(powder)', 'safetida(powder)')
 ]:
actual = preprocess([ingredient])
 assert actual == expected, f'"{expected}" is excpected but got "{actual}"'<feature_engineering>
df_train.isnull().sum()
train['x'] = train['ingredients'].progress_apply(preprocess)
 test['x'] = test['ingredients'].progress_apply(preprocess)
 train.head()<feature_engineering>
X = df_train.drop(columns = ["Survived"])
 y = df_train["Survived"]
vectorizer = make_pipeline(
 TfidfVectorizer(sublinear_tf=True),
FunctionTransformer(lambda x: x.astype('float16'), validate=False)
)
 x_train = vectorizer.fit_transform(train['x'].values)
 x_train.sort_indices()
x_test = vectorizer.transform(test['x'].values )<categorify>
train_X, val_X, train_y, val_y = train_test_split(X, y, test_size=0.2, random_state=0 )
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(train['cuisine'].values)
 dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)) )<choose_model_class>
classifier = DecisionTreeClassifier(max_leaf_nodes=8 , random_state = 1)
 classifier.fit(train_X,train_y)
 preds_val = classifier.predict(df_test )
estimator = SVC(
 C=80,
kernel='rbf',
gamma=1.7,
coef0=1,
cache_size=500, 
)
 classifier = OneVsRestClassifier(estimator, n_jobs=-1 )<train_model>
test_out = pd.DataFrame({
'PassengerId': df_test.PassengerId,
'Survived': preds_val
})
 test_out.to_csv('submission.csv', index=False )