Kaggle competition: Titanic - Machine Learning from Disaster
train = pd.read_csv('.. /input/otto-group-product-classification-challenge/train.csv')
 test = pd.read_csv('.. /input/otto-group-product-classification-challenge/test.csv')
 sampleSubmission = pd.read_csv('.. /input/otto-group-product-classification-challenge/sampleSubmission.csv')
 train.shape, test.shape<prepare_x_and_y>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
train["target"] = train["target"].str.replace('Class_', '')
 train["target"] = train["target"].astype(int)- 1
X_train = train.drop(['id','target'] , axis=1)
 y_train = train["target"]
X_test = test.drop('id', axis=1 )<define_variables>
test['Age'].replace(np.nan,np.median(test['Age'].dropna()),inplace=True)
 train['Age'].replace(np.nan,np.median(train['Age'].dropna()),inplace=True )
y_pred = np.zeros(( len(X_test), 9))
models = []
oof_train = np.zeros(( len(X_train),9))<data_type_conversions>
train['Sex'].replace({'male':1,'female':0},inplace=True)
 test['Sex'].replace({'male':1,'female':0},inplace=True )
NFOLDS = 5
cv = KFold(n_splits=NFOLDS, shuffle=True, random_state=0)
 params = {
'objective': 'multiclass',
'num_class': 9,
}
for fold_id,(train_index, valid_index)in enumerate(cv.split(X_train)) :
X_tr = X_train.loc[train_index, :]
X_val = X_train.loc[valid_index, :]
y_tr = y_train[train_index].astype(int)
 y_val = y_train[valid_index].astype(int)
 lgb_train = lgb.Dataset(X_tr, y_tr)
 lgb_eval = lgb.Dataset(X_val, y_val)
 model = lgb.train(params, lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=10,
num_boost_round=1000,
early_stopping_rounds=10)
 oof_train[valid_index] = model.predict(X_val, num_iteration=model.best_iteration)
 y_pred += model.predict(X_test, num_iteration=model.best_iteration)/NFOLDS<save_to_csv>
embarked=pd.get_dummies(train['Embarked'])
 t_embarked=pd.get_dummies(test['Embarked'])
 train=train.join(embarked)
 test=test.join(t_embarked )
submit = pd.concat([sampleSubmission[['id']], pd.DataFrame(y_pred)], axis = 1)
 submit.columns = sampleSubmission.columns
submit.to_csv('submit.csv', index=False )<import_modules>
train.drop(['Name','SibSp','Parch','Ticket','Cabin','Embarked'],inplace=True,axis=1)
 test.drop(['Name','SibSp','Parch','Ticket','Cabin','Embarked'],inplace=True,axis=1 )
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfTransformer
import lightgbm as lgb<feature_engineering>
test['Fare'].replace({np.nan:np.median(test['Fare'].dropna())},inplace=True )
pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 100)
 dtypes = {f"feat_{i}": "int32" for i in range(1, 94)}
dtypes["id"] = "int32"
dtypes["target"] = "string"<load_from_csv>
test.set_index('PassengerId',inplace=True)
 train.set_index('PassengerId',inplace=True )
df_train = pd.read_csv(
 "/kaggle/input/otto-group-product-classification-challenge/train.csv",
dtype=dtypes
).set_index("id" )<feature_engineering>
X_train=train.drop('Survived',axis=1)
 y_train=train['Survived']
class_to_order = dict()
order_to_class = dict()
for idx, col in enumerate(df_train.target.unique()):
order_to_class[idx] = col
class_to_order[col] = idx
df_train["target_ord"] = df_train["target"].map(class_to_order ).astype("int16")
 feature_columns = [
col for col in df_train.columns if col.startswith("feat_")
 ]
target_column = ["target_ord"]<split>
forest=RandomForestClassifier(max_depth=4,n_estimators=2000)
 forest.fit(X_train,y_train)
 y_pred=forest.predict_proba(test)
 y_pred=(y_pred[:,1]>0.45 ).astype(int)
 final_df=pd.DataFrame({'PassengerId':test.index,'Survived':y_pred})
 final_df.set_index('PassengerId',inplace=True )
X_train, X_valid, y_train, y_valid = train_test_split(
 df_train[feature_columns], df_train[target_column],
test_size=0.3, random_state=42,
stratify=df_train[target_column]
)<data_type_conversions>
final_df.to_csv('submit.csv' )
tfidf = TfidfTransformer()
tfidf_feature_train = tfidf.fit_transform(X_train ).toarray().astype("float32")
 tfidf_feature_valid = tfidf.transform(X_valid ).toarray().astype("float32" )<concatenate>
svc=SVC(C=1,gamma=10)
 svc.fit(X_train,y_train)
 y_pred2=svc.predict(test)
 final_df2=pd.DataFrame({'PassengerId':test.index,'Survived':y_pred2})
 final_df2.set_index('PassengerId',inplace=True )
<init_hyperparams><EOS> 
final_df2.to_csv('submit.csv' )