Kaggle competition: Titanic - Machine Learning from Disaster
import lightgbm as lgb<import_modules>
raw_data = pd.read_csv('.. /input/train.csv')
 raw_data.head()
from tqdm.notebook import tqdm<import_modules>
print(raw_data.columns)
 print(raw_data.isnull().sum() )
from sklearn.preprocessing import LabelEncoder<load_from_csv>
raw_data.groupby(['Pclass', 'Sex', 'Embarked'])['Survived'].mean()
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )<load_from_csv>
y = raw_data.Survived
features = ['Pclass', 'Sex', 'Embarked']
X = raw_data[features].copy()
region_metadata = pd.read_csv(".. /input/covid19-forecasting-metadata/region_metadata.csv" )<feature_engineering>
def reformat(x):
d_sex = {'female': 0, 'male': 1}
d_emb = {'C': 0, 'Q': 1, 'S': 2, '3':3}
x['Sex'] = [d_sex[sex] for sex in x['Sex'].values]
x['Embarked'] = x['Embarked'].fillna('3')
 x['Embarked'] = [d_emb[elem] for elem in x['Embarked'].values]
reformat(X)
 X.head()
def transform_geo_location(df):
lat = df.lat.values
lon = df.lon.values
df["lat_sin"] = np.sin(2 * np.pi * lat / 360)
 df["lat_cos"] = np.cos(2 * np.pi * lat / 360)
 df["lon_sin"] = np.sin(2 * np.pi * lon / 360)
 df["lon_cos"] = np.cos(2 * np.pi * lon / 360)
 return df<categorify>
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X )
region_metadata = transform_geo_location(region_metadata)
 geo_location_columns = ["lat_sin", "lat_cos", "lon_sin", "lon_cos"]<load_from_csv>
model = RandomForestClassifier()
model.fit(X_scaled,y)
 cross_val_score(model , X_scaled , y , cv=5 )
country_data = pd.read_csv(".. /input/countries-of-the-world/countries of the world.csv")
 country_data.Country = country_data.Country.apply(lambda c: c.rstrip(" "))
country_data.rename(
 columns={
"Country": "Country_Region",
},
inplace=True
)<define_variables>
def applyModel(X_test):
X = X_test[features].copy()
reformat(X)
 X = scaler.transform(X)
 preds = model.predict(X)
 return preds
country_meta_columns = [
'Coastline(coast/area ratio)',
'Net migration',
'Infant mortality(per 1000 births)',
'GDP($ per capita)',
'Literacy(%)',
'Phones(per 1000)',
'Arable(%)',
'Crops(%)',
'Other(%)',
'Climate',
'Birthrate',
'Deathrate',
'Agriculture',
'Industry',
'Service'
]<data_type_conversions>
X_test = pd.read_csv('.. /input/test.csv')
 preds = applyModel(X_test)
 output = pd.DataFrame({'PassengerId': X_test.PassengerId,
'Survived': preds})
 output.to_csv('submission.csv', index=False )