Kaggle competition: Titanic - Machine Learning from Disaster
for col in X_train.columns:
print(X_train[col].value_counts())
 print('____' )<split>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
%%time
num_split = 5
num_iter = 3
stop_round = 50
scores = []
y_pred_cva = np.zeros(len(X_test))
scores = []
for h in range(num_iter):
kf = KFold(n_splits=num_split, random_state=h, shuffle=True)
 for i,(train_ix, test_ix)in tqdm(enumerate(kf.split(X_train, y_train))):
X_train_, y_train_ = X_train.values[train_ix], y_train.values[train_ix]
X_val, y_val = X_train.values[test_ix], y_train.values[test_ix]
clf = LGBMRegressor(n_estimators=9999, random_state=71, colsample_bytree=0.9,
learning_rate=0.05, min_child_samples=20,
min_child_weight=0.001, min_split_gain=0.0, num_leaves=15)
 clf.fit(X_train_, y_train_, early_stopping_rounds=stop_round, eval_metric='rmse', eval_set=[(X_val, y_val)])
 y_pred = clf.predict(X_val)
 score = mean_squared_error(y_val, y_pred)**0.5
scores.append(score)
 y_pred_cva += clf.predict(X_test)
 print(clf.predict(X_test))
print(np.mean(scores))
print(scores)
 y_pred_cva /=(num_split * num_iter )<prepare_output>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
y_pred_exp = np.exp(y_pred_cva)- 1
y_pred_exp<load_from_csv>
pip install lofo-importance
submission = pd.read_csv('.. /input/exam-for-students20200129/sample_submission.csv', index_col=0)
 submission.ConvertedSalary = y_pred_exp<save_to_csv>
%matplotlib inline
submission.to_csv('submission.csv')
 submission<set_options>
lr = lgb.LGBMClassifier()
lr.fit(X_test[["Pclass", "Sex", "SibSp", "Parch" ,"Age", "Fare" , "Embarked"]], X_test["Survived"])
 fi = FLOFOImportance(lr, X_test, ["Pclass", "Sex", "SibSp", "Parch" ,"Age", "Fare" , "Embarked"], 'Survived', scoring="neg_mean_absolute_error")
 importances = fi.get_importance()
importances
%matplotlib inline
plt.style.use('ggplot')
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_rows', 20 )<import_modules>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch" ,"Age", "Fare" , "Embarked"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = lgb.LGBMClassifier().fit(X, y )
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_auc_score
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.feature_extraction.text import TfidfVectorizer
from category_encoders import OrdinalEncoder, OneHotEncoder, TargetEncoder, CatBoostEncoder
from tqdm import tqdm_notebook as tqdm
import lightgbm as lgb
from lightgbm import LGBMClassifier 
<load_from_csv>
params = {"num_leaves": [16,12,8,4],
"learning_rate": [0.4, 0.1, 0.8, 0.05],
"max_depth":[10,8,6,4],
"feature_fraction": [0.1, 0.3, 0.5, 0.8],
"subsample": [0.2, 0.5, 0.75]}
df_train = pd.read_csv('.. /input/exam-for-students20200129/train.csv', index_col=0)
<load_from_csv>
cv_model = GridSearchCV(model , params, cv=10, verbose=2 , n_jobs=-1 ).fit(X , y )
df_test = pd.read_csv('.. /input/exam-for-students20200129/test.csv', index_col=0)
<define_variables>
cv_model.best_params_
tgt_col = 'ConvertedSalary'<data_type_conversions>
model_tuned = lgb.LGBMClassifier(max_depth= 10, feature_fraction=0.1, learning_rate=0.1,
num_leaves= 16,
subsample= 0.2 ).fit(X , y )
df_train2 = df_train.copy()
df_test2 = df_test.copy()
df_train2['missing_amnt'] = df_train2.isnull().sum(axis=1)
 df_test2['missing_amnt'] = df_test2.isnull().sum(axis=1 )<feature_engineering>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )