Kaggle competition: OSIC Pulmonary Fibrosis Progression
<SOS> metric: LaplaceLogLikelihood Kaggle data source: osic-pulmonary-fibrosis-progression<define_variables>
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index
features = [
"item_id",
"dept_id",
"cat_id",
"store_id",
"state_id",
"event_name_1",
"event_type_1",
"event_name_2",
"event_type_2",
"snap_CA",
"snap_TX",
"snap_WI",
"sell_price",
"shift_t28",
"shift_t29",
"shift_t30",
"rolling_mean_t7",
"rolling_mean_t30",
"rolling_mean_t60",
"rolling_mean_t90",
"rolling_mean_t180",
"price_change_t1",
"price_change_t365",
"rolling_price_std_t7",
"rolling_price_std_t30",
"year",
"month",
"week",
"day",
"dayofweek",
"is_year_end",
"is_year_start",
"is_quarter_end",
"is_quarter_start",
"is_month_end",
"is_month_start",
"is_weekend",
]
params = {
'boosting_type': 'gbdt',
'metric': 'rmse',
'objective': 'regression',
'n_jobs': -1,
'seed': 236,
'learning_rate': 0.1,
'bagging_fraction': 0.75,
'bagging_freq': 10,
'colsample_bytree': 0.75}
train_set = lgb.Dataset(x_train[features], y_train)
 val_set = lgb.Dataset(x_val[features], y_val)
 del x_train, y_train 
<save_to_csv>
import os
import cv2
import pydicom
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import random
from tqdm.notebook import tqdm
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import mean_absolute_error
from tensorflow_addons.optimizers import RectifiedAdam
from tensorflow.keras import Model
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.optimizers import Nadam
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from PIL import Image
<data_type_conversions>
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed )
weight_mat = np.c_[np.ones([NUM_ITEMS,1] ).astype(np.int8),
pd.get_dummies(product.state_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.store_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.cat_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.dept_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.state_id.astype(str)+ product.cat_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.state_id.astype(str)+ product.dept_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.store_id.astype(str)+ product.cat_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.store_id.astype(str)+ product.dept_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.item_id.astype(str),drop_first=False ).astype('int8' ).values,
pd.get_dummies(product.state_id.astype(str)+ product.item_id.astype(str),drop_first=False ).astype('int8' ).values,
np.identity(NUM_ITEMS ).astype(np.int8)
 ].T
weight_mat_csr = csr_matrix(weight_mat)
 del weight_mat; gc.collect()
def weight_calc(data,product):
sales_train_val = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv')
 d_name = ['d_' + str(i+1)for i in range(1913)]
sales_train_val = weight_mat_csr * sales_train_val[d_name].values
df_tmp =(( sales_train_val>0)* np.tile(np.arange(1,1914),(weight_mat_csr.shape[0],1)))
 start_no = np.min(np.where(df_tmp==0,9999,df_tmp),axis=1)-1
flag = np.dot(np.diag(1/(start_no+1)) , np.tile(np.arange(1,1914),(weight_mat_csr.shape[0],1)))<1
sales_train_val = np.where(flag,np.nan,sales_train_val)
 weight1 = np.nansum(np.diff(sales_train_val,axis=1)**2,axis=1)/(1913-start_no)
 df_tmp = data[(data['date'] > '2016-03-27')&(data['date'] <= '2016-04-24')]
df_tmp['amount'] = df_tmp['demand'] * df_tmp['sell_price']
df_tmp =df_tmp.groupby(['id'])['amount'].apply(np.sum)
 df_tmp = df_tmp[product.id].values
weight2 = weight_mat_csr * df_tmp
weight2 = weight2/np.sum(weight2)
 del sales_train_val
gc.collect()
return weight1, weight2
weight1, weight2 = weight_calc(data,product)
 def wrmsse(preds, data):
y_true = data.get_label()
y_true = y_true[-(NUM_ITEMS * DAYS_PRED):]
preds = preds[-(NUM_ITEMS * DAYS_PRED):]
num_col = DAYS_PRED
reshaped_preds = preds.reshape(num_col, NUM_ITEMS ).T
reshaped_true = y_true.reshape(num_col, NUM_ITEMS ).T
train = weight_mat_csr*np.c_[reshaped_preds, reshaped_true]
score = np.sum(
 np.sqrt(
 np.mean(
 np.square(
 train[:,:num_col] - train[:,num_col:])
 ,axis=1)/ weight1)* weight2)
 return 'wrmsse', score, False
def wrmsse_simple(preds, data):
y_true = data.get_label()
y_true = y_true[-(NUM_ITEMS * DAYS_PRED):]
preds = preds[-(NUM_ITEMS * DAYS_PRED):]
num_col = DAYS_PRED
reshaped_preds = preds.reshape(num_col, NUM_ITEMS ).T
reshaped_true = y_true.reshape(num_col, NUM_ITEMS ).T
train = np.c_[reshaped_preds, reshaped_true]
weight2_2 = weight2[:NUM_ITEMS]
weight2_2 = weight2_2/np.sum(weight2_2)
 score = np.sum(
 np.sqrt(
 np.mean(
 np.square(
 train[:,:num_col] - train[:,num_col:])
 ,axis=1)/ weight1[:NUM_ITEMS])*weight2_2)
 return 'wrmsse', score, False<train_model>
seed_everything(42 )
params = {
'boosting_type': 'gbdt',
'metric': 'custom',
'objective': 'poisson',
'n_jobs': -1,
'seed': 236,
'learning_rate': 0.1,
'bagging_fraction': 0.75,
'bagging_freq': 10,
'colsample_bytree': 0.75}
model = lgb.train(params, train_set, num_boost_round = 2500, early_stopping_rounds = 50,
valid_sets = [train_set, val_set], verbose_eval = 100, feval= wrmsse)
 val_pred = model.predict(x_val[features])
 val_score = np.sqrt(metrics.mean_squared_error(val_pred, y_val))
print(f'Our val wrmsse score is {val_score}')
 y_pred = model.predict(test[features])
 test['demand'] = y_pred<save_to_csv>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )
predictions = test[['id', 'date', 'demand']]
predictions = pd.pivot(predictions, index = 'id', columns = 'date', values = 'demand' ).reset_index()
predictions.columns = ['id'] + ['F' + str(i + 1)for i in range(28)]
evaluation_rows = [row for row in submission['id'] if 'evaluation' in row]
evaluation = submission[submission['id'].isin(evaluation_rows)]
validation = submission[['id']].merge(predictions, on = 'id')
 final = pd.concat([validation, evaluation])
 final.to_csv('submission2.csv', index = False )<set_options>
Dropout_model = 0.25
FVC_weight = 0.5
Confidence_weight = 0.5
GaussianNoise_stddev = 0.2
warnings.filterwarnings('ignore')
<load_from_csv>
commits_df = pd.DataFrame(columns = ['commit_num', 'FVC_weight', 'Dropout_model', 'LB_score', 'seed', 'GaussianNoise_stddev'] )
path = ".. /input/m5-forecasting-accuracy"
calendar = pd.read_csv(os.path.join(path, "calendar.csv"))
selling_prices = pd.read_csv(os.path.join(path, "sell_prices.csv"))
sample_submission = pd.read_csv(os.path.join(path, "sample_submission.csv"))<load_from_csv>
n=0
commits_df.loc[n,'commit_num'] = 1
commits_df.loc[n,'Dropout_model'] = 0.4
commits_df.loc[n,'FVC_weight'] = 0.25
commits_df.loc[n,'LB_score'] = -6.8111
sales = pd.read_csv(os.path.join(path, "sales_train_validation.csv"))<categorify>
n=1
commits_df.loc[n,'commit_num'] = 4
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.25
commits_df.loc[n,'LB_score'] = -6.8105
def prep_calendar(df):
df = df.drop(["date", "weekday"], axis=1)
 df = df.assign(d = df.d.str[2:].astype(int))
df = df.fillna("missing")
 cols = list(set(df.columns)- {"wm_yr_wk", "d"})
 df[cols] = OrdinalEncoder(dtype="int" ).fit_transform(df[cols])
 df = reduce_mem_usage(df)
 return df
calendar = prep_calendar(calendar )<feature_engineering>
n=2
commits_df.loc[n,'commit_num'] = 6
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.35
commits_df.loc[n,'LB_score'] = -6.8158
def prep_selling_prices(df):
gr = df.groupby(["store_id", "item_id"])["sell_price"]
df["sell_price_rel_diff"] = gr.pct_change()
df["sell_price_roll_sd7"] = gr.transform(lambda x: x.rolling(7 ).std())
 df["sell_price_cumrel"] =(gr.shift(0)- gr.cummin())/(1 + gr.cummax() - gr.cummin())
 df = reduce_mem_usage(df)
 return df
selling_prices = prep_selling_prices(selling_prices )<count_values>
n=3
commits_df.loc[n,'commit_num'] = 8
commits_df.loc[n,'Dropout_model'] = 0.35
commits_df.loc[n,'FVC_weight'] = 0.25
commits_df.loc[n,'LB_score'] = -6.8107
sales.item_id.value_counts()<data_type_conversions>
n=4
commits_df.loc[n,'commit_num'] = 9
commits_df.loc[n,'Dropout_model'] = 0.35
commits_df.loc[n,'FVC_weight'] = 0.3
commits_df.loc[n,'LB_score'] = -6.8125
def reshape_sales(df, drop_d = None):
if drop_d is not None:
df = df.drop(["d_" + str(i + 1)for i in range(drop_d)], axis=1)
 df = df.assign(id=df.id.str.replace("_validation", ""))
df = df.reindex(columns=df.columns.tolist() + ["d_" + str(1913 + i + 1)for i in range(2 * 28)])
 df = df.melt(id_vars=["id", "item_id", "dept_id", "cat_id", "store_id", "state_id"],
var_name='d', value_name='demand')
 df = df.assign(d=df.d.str[2:].astype("int16"))
return df
sales = reshape_sales(sales, 1000 )<feature_engineering>
n=5
commits_df.loc[n,'commit_num'] = 10
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8089
def prep_sales(df):
df['lag_t28'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28))
df['rolling_mean_t7'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(7 ).mean())
 df['rolling_mean_t30'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(30 ).mean())
 df['rolling_mean_t60'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(60 ).mean())
 df['rolling_mean_t90'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(90 ).mean())
 df['rolling_mean_t180'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(180 ).mean())
 df['rolling_std_t7'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(7 ).std())
 df['rolling_std_t30'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(30 ).std())
 df = df[(df.d >= 1914)|(pd.notna(df.rolling_mean_t180)) ]
df = reduce_mem_usage(df)
 return df
sales = prep_sales(sales )<merge>
n=6
commits_df.loc[n,'commit_num'] = 11
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.15
commits_df.loc[n,'LB_score'] = -6.8100
sales = sales.merge(calendar, how="left", on="d")
 gc.collect()
sales.head()<merge>
n=7
commits_df.loc[n,'commit_num'] = 13
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.175
commits_df.loc[n,'LB_score'] = -6.8096
sales = sales.merge(selling_prices, how="left", on=["wm_yr_wk", "store_id", "item_id"])
 sales.drop(["wm_yr_wk"], axis=1, inplace=True)
 gc.collect()
sales.head()<drop_column>
n=8
commits_df.loc[n,'commit_num'] = 14
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.225
commits_df.loc[n,'LB_score'] = -6.8100
del selling_prices<categorify>
n=9
commits_df.loc[n,'commit_num'] = 15
commits_df.loc[n,'Dropout_model'] = 0.32
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8092
cat_id_cols = ["item_id", "dept_id", "store_id", "cat_id", "state_id"]
cat_cols = cat_id_cols + ["wday", "month", "year", "event_name_1",
"event_type_1", "event_name_2", "event_type_2"]
for i, v in tqdm(enumerate(cat_id_cols)) :
sales[v] = OrdinalEncoder(dtype="int" ).fit_transform(sales[[v]])
 sales = reduce_mem_usage(sales)
 sales.head()
gc.collect()<categorify>
n=10
commits_df.loc[n,'commit_num'] = 16
commits_df.loc[n,'Dropout_model'] = 0.25
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8093
test = sales[sales.d >= 1914]
test = test.assign(id=test.id + "_" + np.where(test.d <= 1941, "validation", "evaluation"),
F="F" +(test.d - 1913 - 28 *(test.d > 1941)).astype("str"))
test.head()
gc.collect()<prepare_x_and_y>
n=11
commits_df.loc[n,'commit_num'] = 17
commits_df.loc[n,'Dropout_model'] = 0.25
commits_df.loc[n,'FVC_weight'] = 0.5
commits_df.loc[n,'LB_score'] = -6.8283
def make_X(df):
X = {"dense1": df[dense_cols].to_numpy() }
for i, v in enumerate(cat_cols):
X[v] = df[[v]].to_numpy()
return X
X_test = make_X(test)
 flag =(sales.d < 1914)&(sales.d >= 1914 - 28)
 valid =(make_X(sales[flag]),
sales["demand"][flag])
 flag = sales.d < 1914 - 28
X_train = make_X(sales[flag])
 y_train = sales["demand"][flag]
del sales, flag
gc.collect()<import_modules>
n=12
commits_df.loc[n,'commit_num'] = 18
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8087
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import regularizers
from tensorflow.keras.layers import Dense, Input, Embedding, Dropout, concatenate, Flatten
from tensorflow.keras.models import Model<define_variables>
n=13
commits_df.loc[n,'commit_num'] = 19
commits_df.loc[n,'Dropout_model'] = 0.39
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8090
def create_model(lr=0.002):
tf.random.set_seed(173)
 tf.keras.backend.clear_session()
gc.collect()
dense_input = Input(shape=(len(dense_cols),), name='dense1')
 wday_input = Input(shape=(1,), name='wday')
 month_input = Input(shape=(1,), name='month')
 year_input = Input(shape=(1,), name='year')
 event_name_1_input = Input(shape=(1,), name='event_name_1')
 event_type_1_input = Input(shape=(1,), name='event_type_1')
 event_name_2_input = Input(shape=(1,), name='event_name_2')
 event_type_2_input = Input(shape=(1,), name='event_type_2')
 item_id_input = Input(shape=(1,), name='item_id')
 dept_id_input = Input(shape=(1,), name='dept_id')
 store_id_input = Input(shape=(1,), name='store_id')
 cat_id_input = Input(shape=(1,), name='cat_id')
 state_id_input = Input(shape=(1,), name='state_id')
 wday_emb = Flatten()(Embedding(7, 1 )(wday_input))
month_emb = Flatten()(Embedding(12, 1 )(month_input))
year_emb = Flatten()(Embedding(6, 1 )(year_input))
event_name_1_emb = Flatten()(Embedding(31, 1 )(event_name_1_input))
event_type_1_emb = Flatten()(Embedding(5, 1 )(event_type_1_input))
event_name_2_emb = Flatten()(Embedding(5, 1 )(event_name_2_input))
event_type_2_emb = Flatten()(Embedding(5, 1 )(event_type_2_input))
item_id_emb = Flatten()(Embedding(3049, 3 )(item_id_input))
dept_id_emb = Flatten()(Embedding(7, 1 )(dept_id_input))
store_id_emb = Flatten()(Embedding(10, 1 )(store_id_input))
cat_id_emb = Flatten()(Embedding(3, 1 )(cat_id_input))
state_id_emb = Flatten()(Embedding(3, 1 )(state_id_input))
x = concatenate([dense_input, wday_emb, month_emb, year_emb,
event_name_1_emb, event_type_1_emb,
event_name_2_emb, event_type_2_emb,
item_id_emb, dept_id_emb, store_id_emb,
cat_id_emb, state_id_emb])
 x = Dense(150, activation="tanh" )(x)
 x = Dense(75, activation="tanh" )(x)
 x = Dense(10, activation="tanh" )(x)
 outputs = Dense(1, activation="linear", name='output' )(x)
 inputs = {"dense1": dense_input, "wday": wday_input, "month": month_input, "year": year_input,
"event_name_1": event_name_1_input, "event_type_1": event_type_1_input,
"event_name_2": event_name_2_input, "event_type_2": event_type_2_input,
"item_id": item_id_input, "dept_id": dept_id_input, "store_id": store_id_input,
"cat_id": cat_id_input, "state_id": state_id_input}
model = Model(inputs, outputs)
 model.compile(loss=keras.losses.mean_squared_error,
metrics=["mse"],
optimizer=keras.optimizers.RMSprop(learning_rate=lr))
return model<train_model>
n=14
commits_df.loc[n,'commit_num'] = 20
commits_df.loc[n,'Dropout_model'] = 0.37
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8092
history = model.fit(X_train,
y_train,
batch_size=10000,
epochs=30,
shuffle=True,
validation_data=valid )<save_model>
n=15
commits_df.loc[n,'commit_num'] = 21
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.19
commits_df.loc[n,'LB_score'] = -6.8093
model.save('model.h5' )<predict_on_test>
n=16
commits_df.loc[n,'commit_num'] = 23
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8402
pred = model.predict(X_test, batch_size=10000 )<merge>
n=17
commits_df.loc[n,'commit_num'] = 26
commits_df.loc[n,'Dropout_model'] = 0.385
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8092
test["demand"] = pred.clip(0)
 submission = test.pivot(index="id", columns="F", values="demand" ).reset_index() [sample_submission.columns]
submission = sample_submission[["id"]].merge(submission, how="left", on="id")
 submission.head()<save_to_csv>
n=18
commits_df.loc[n,'commit_num'] = 27
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.21
commits_df.loc[n,'LB_score'] = -6.8091
submission.to_csv("submission.csv", index=False )<save_to_csv>
n=19
commits_df.loc[n,'commit_num'] = 28
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8090
submission.to_csv("submission.csv", index=False )<set_options>
n=20
commits_df.loc[n,'commit_num'] = 29
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'GaussianNoise_stddev'] = 0.15
commits_df.loc[n,'LB_score'] = -6.8092
%pylab inline 
<import_modules>
n=21
commits_df.loc[n,'commit_num'] = 31
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'GaussianNoise_stddev'] = 0.21
commits_df.loc[n,'LB_score'] = -6.8093
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder<import_modules>
n=22
commits_df.loc[n,'commit_num'] = 32
commits_df.loc[n,'Dropout_model'] = 0.24
commits_df.loc[n,'FVC_weight'] = 0.14
commits_df.loc[n,'GaussianNoise_stddev'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8106
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical<set_options>
commits_df['LB_score'] = pd.to_numeric(commits_df['LB_score'])
 commits_df['max'] = 0
commits_df.loc[commits_df['LB_score'].idxmax() , 'max'] = 1
rcParams['figure.figsize'] = 10,10<load_from_csv>
commits_df['seed'] = 42
commits_df.loc[commits_df['commit_num'] == 23, 'seed'] = 0
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )<categorify>
commits_df['GaussianNoise_stddev'] = 0.2
commits_df.loc[commits_df['commit_num'] == 28, 'GaussianNoise_stddev'] = 0.25
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )<normalization>
commits_df.sort_values(by=['LB_score'], ascending = False )
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )<categorify>
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )
y_cat = to_categorical(y)
 print(y_cat.shape )<choose_model_class>
def get_tab(df):
vector = [(df.Age.values[0] - 30)/ 30]
if df.Sex.values[0] == 'male':
vector.append(0)
 else:
vector.append(1)
 if df.SmokingStatus.values[0] == 'Never smoked':
vector.extend([0,0])
 elif df.SmokingStatus.values[0] == 'Ex-smoker':
vector.extend([1,1])
 elif df.SmokingStatus.values[0] == 'Currently smokes':
vector.extend([0,1])
 else:
vector.extend([1,0])
 return np.array(vector )
model = Sequential()
model.add(Dense(128, input_dim=192, init='uniform', activation='relu'))
model.add(Dense(24, init='normal', activation='sigmoid'))
model.add(Dense(99, activation='softmax'))<choose_model_class>
A = {}
TAB = {}
P = []
for i, p in tqdm(enumerate(train.Patient.unique())) :
sub = train.loc[train.Patient == p, :]
fvc = sub.FVC.values
weeks = sub.Weeks.values
c = np.vstack([weeks, np.ones(len(weeks)) ] ).T
a, b = np.linalg.lstsq(c, fvc)[0]
A[p] = a
TAB[p] = get_tab(sub)
 P.append(p )
model = Sequential()
model.add(Dense(2048,input_dim=192, init='uniform', activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1024, activation='tanh'))
model.add(Dropout(0.3))
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.3))
model.add(Dense(99, activation='softmax'))<choose_model_class>
class IGenerator(Sequence):
BAD_ID = ['ID00011637202177653955184', 'ID00052637202186188008618']
def __init__(self, keys, a, tab, batch_size=32):
self.keys = [k for k in keys if k not in self.BAD_ID]
self.a = a
self.tab = tab
self.batch_size = batch_size
self.train_data = {}
for p in train.Patient.values:
self.train_data[p] = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 def __len__(self):
return 1000
def __getitem__(self, idx):
x = []
a, tab = [], []
keys = np.random.choice(self.keys, size = self.batch_size)
 for k in keys:
try:
i = np.random.choice(self.train_data[k], size=1)[0]
img = get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{k}/{i}')
 x.append(img)
 a.append(self.a[k])
 tab.append(self.tab[k])
 except:
print(k, i)
 x,a,tab = np.array(x), np.array(a), np.array(tab)
 x = np.expand_dims(x, axis=-1)
 return [x, tab] , a
model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"] )<train_model>
Dense, Dropout, Activation, Flatten, Input, BatchNormalization, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 def get_efficientnet(model, shape):
models_dict = {
'b0': efn.EfficientNetB0(input_shape=shape,weights=None,include_top=False),
'b1': efn.EfficientNetB1(input_shape=shape,weights=None,include_top=False),
'b2': efn.EfficientNetB2(input_shape=shape,weights=None,include_top=False),
'b3': efn.EfficientNetB3(input_shape=shape,weights=None,include_top=False),
'b4': efn.EfficientNetB4(input_shape=shape,weights=None,include_top=False),
'b5': efn.EfficientNetB5(input_shape=shape,weights=None,include_top=False),
'b6': efn.EfficientNetB6(input_shape=shape,weights=None,include_top=False),
'b7': efn.EfficientNetB7(input_shape=shape,weights=None,include_top=False)
 }
return models_dict[model]
def build_model(shape=(512, 512, 1), model_class=None):
inp = Input(shape=shape)
 base = get_efficientnet(model_class, shape)
 x = base(inp)
 x = GlobalAveragePooling2D()(x)
 inp2 = Input(shape=(4,))
x2 = tf.keras.layers.GaussianNoise(GaussianNoise_stddev )(inp2)
 x = Concatenate()([x, x2])
 x = Dropout(Dropout_model )(x)
 x = Dense(1 )(x)
 model = Model([inp, inp2] , x)
 weights = [w for w in os.listdir('.. /input/osic-model-weights')if model_class in w][0]
model.load_weights('.. /input/osic-model-weights/' + weights)
 return model
model_classes = ['b5']
models = [build_model(shape=(512, 512, 1), model_class=m)for m in model_classes]
print('Number of models: ' + str(len(models)) )
history = model.fit(X,y_cat,batch_size=128,
nb_epoch=100,verbose=1, validation_split=0.1 )<load_from_csv>
tr_p, vl_p = train_test_split(P, shuffle=True, train_size = 0.8 )
test = pd.read_csv('.. /input/test.csv' )<drop_column>
def score(fvc_true, fvc_pred, sigma):
sigma_clip = np.maximum(sigma, 70)
 delta = np.abs(fvc_true - fvc_pred)
 delta = np.minimum(delta, 1000)
 sq2 = np.sqrt(2)
 metric =(delta / sigma_clip)*sq2 + np.log(sigma_clip* sq2)
 return np.mean(metric )
index = test.pop('id' )<normalization>
subs = []
for model in models:
metric = []
for q in tqdm(range(1, 10)) :
m = []
for p in vl_p:
x = []
tab = []
if p in ['ID00011637202177653955184', 'ID00052637202186188008618']:
continue
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/{i}'))
tab.append(get_tab(train.loc[train.Patient == p, :]))
if len(x)< 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q / 10)
 percent_true = train.Percent.values[train.Patient == p]
fvc_true = train.FVC.values[train.Patient == p]
weeks_true = train.Weeks.values[train.Patient == p]
fvc = a *(weeks_true - weeks_true[0])+ fvc_true[0]
percent = percent_true[0] - a * abs(weeks_true - weeks_true[0])
 m.append(score(fvc_true, fvc, percent))
print(np.mean(m))
metric.append(np.mean(m))
q =(np.argmin(metric)+ 1)/ 10
sub = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv')
 test = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 A_test, B_test, P_test,W, FVC= {}, {}, {},{},{}
STD, WEEK = {}, {}
for p in test.Patient.unique() :
x = []
tab = []
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/{i}'))
tab.append(get_tab(test.loc[test.Patient == p, :]))
if len(x)<= 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q)
 A_test[p] = a
B_test[p] = test.FVC.values[test.Patient == p] - a*test.Weeks.values[test.Patient == p]
P_test[p] = test.Percent.values[test.Patient == p]
WEEK[p] = test.Weeks.values[test.Patient == p]
for k in sub.Patient_Week.values:
p, w = k.split('_')
 w = int(w)
 fvc = A_test[p] * w + B_test[p]
sub.loc[sub.Patient_Week == k, 'FVC'] = fvc
sub.loc[sub.Patient_Week == k, 'Confidence'] =(
 P_test[p] - A_test[p] * abs(WEEK[p] - w)
)
 _sub = sub[["Patient_Week","FVC","Confidence"]].copy()
subs.append(_sub )
test = StandardScaler().fit(test ).transform(test )<predict_on_test>
N = len(subs)
 sub = subs[0].copy()
sub["FVC"] = 0
sub["Confidence"] = 0
for i in range(N):
sub["FVC"] += subs[0]["FVC"] *(1/N)
 sub["Confidence"] += subs[0]["Confidence"] *(1/N )
yPred = model.predict_proba(test )<create_dataframe>
sub[["Patient_Week","FVC","Confidence"]].to_csv("submission_img.csv", index=False )
yPred = pd.DataFrame(yPred,index=index,columns=sort(parent_data.species.unique()))<save_to_csv>
img_sub = sub[["Patient_Week","FVC","Confidence"]].copy()
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv() )<load_from_csv>
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE=128
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )
data = pd.read_csv('.. /input/train.csv')
 or_data = data.copy()
ID = data.pop('id')
 data.head()<import_modules>
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.model_selection import GridSearchCV<import_modules>
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique() )
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical
from keras.wrappers.scikit_learn import KerasClassifier<categorify>
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 y_cat = to_categorical(y)
 y_cat.shape, y.shape<normalization>
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )
X = StandardScaler().fit(data ).transform(data)
 X.shape<choose_model_class>
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base
def create_model(dropout_rate_l1=0.1 , dropout_rate_l2=0.1):
model = Sequential()
model.add(Dense(2048,input_dim=192, init='uniform', activation='relu'))
model.add(Dropout(dropout_rate_l1))
model.add(Dense(1536, activation='relu'))
model.add(Dropout(dropout_rate_l2))
model.add(Dense(99, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"])
 return model<train_model>
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )
dropout_rate_l1 = [0.3]
dropout_rate_l2 = [0.3]
models = []
for i in dropout_rate_l1:
for j in dropout_rate_l2:
models.append([i,j])
 histories = []
for i in models:
print(i)
 model = create_model(i[0],i[1])
 history = model.fit(X,y_cat,batch_size=192,
nb_epoch=10,verbose=0, validation_split=0.1)
 histories.append(history )<find_best_params>
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']
min_val_loss, param = 1,[]
for history,model in zip(histories,models):
if min(history.history['val_loss'])< min_val_loss :
min_val_loss = min(history.history['val_loss'])
 param = model
print(model)
 print(history.history.keys())
 print('val_loss: ',min(history.history['val_loss']))
print('loss: ',min(history.history['loss']))
print('
')
 print(min_val_loss, param )<train_model>
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data
model = create_model(param[0],param[1])
 model.fit(X,y_cat,batch_size=192,nb_epoch=10,verbose=0, validation_split=0.1 )<load_from_csv>
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model(nh):
z = L.Input(( nh,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.65), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model
test = pd.read_csv('.. /input/test.csv')
 index = test.pop('id')
 test = StandardScaler().fit(test ).transform(test )<predict_on_test>
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
nh = z.shape[1]
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
yPred = model.predict_proba(test )<create_dataframe>
NFOLD = 5
kf = KFold(n_splits=NFOLD )
yPred = pd.DataFrame(yPred, index=index, columns=sort(or_data.species.unique()))
yPred 
<save_to_csv>
%%time
cnt = 0
EPOCHS = 800
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model(nh)
 net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
yPred.to_csv('result.csv', index=True )<define_variables>
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )
root = '.. /input'
np.random.seed(2016)
 split_random_state = 7
split =.9
def load_numeric_training(standardize=True):

 data = pd.read_csv(os.path.join(root, 'train.csv'))
ID = data.pop('id')
 y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 X = StandardScaler().fit(data ).transform(data)if standardize else data.values
return ID, X, y
def load_numeric_test(standardize=True):

 test = pd.read_csv(os.path.join(root, 'test.csv'))
ID = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)if standardize else test.values
return ID, test
def resize_img(img, max_dim=96):

 max_ax = max(( 0, 1), key=lambda i: img.size[i])
 scale = max_dim / float(img.size[max_ax])
 return img.resize(( int(img.size[0] * scale), int(img.size[1] * scale)))
 def load_image_data(ids, max_dim=96, center=True):

 X = np.empty(( len(ids), max_dim, max_dim, 1))
for i, idee in enumerate(ids):
x = resize_img(load_img(os.path.join(root, 'images', str(idee)+ '.jpg'), grayscale=True), max_dim=max_dim)
 x = img_to_array(x)
 length = x.shape[0]
width = x.shape[1]
if center:
h1 = int(( max_dim - length)/ 2)
 h2 = h1 + length
w1 = int(( max_dim - width)/ 2)
 w2 = w1 + width
else:
h1, w1 = 0, 0
h2, w2 =(length, width)
 X[i, h1:h2, w1:w2, 0:1] = x
return np.around(X / 255.0)
 def load_train_data(split=split, random_state=None):

 ID, X_num_tr, y = load_numeric_training()
X_img_tr = load_image_data(ID)
 sss = StratifiedShuffleSplit(n_splits=1, train_size=split, random_state=random_state)
 train_ind, test_ind = next(sss.split(X_num_tr, y))
X_num_val, X_img_val, y_val = X_num_tr[test_ind], X_img_tr[test_ind], y[test_ind]
X_num_tr, X_img_tr, y_tr = X_num_tr[train_ind], X_img_tr[train_ind], y[train_ind]
return(X_num_tr, X_img_tr, y_tr),(X_num_val, X_img_val, y_val)
 def load_test_data() :

 ID, X_num_te = load_numeric_test()
X_img_te = load_image_data(ID)
 return ID, X_num_te, X_img_te
print('Loading the training data...')
(X_num_tr, X_img_tr, y_tr),(X_num_val, X_img_val, y_val)= load_train_data(random_state=split_random_state)
 y_tr_cat = to_categorical(y_tr)
 y_val_cat = to_categorical(y_val)
 print('Training data loaded!' )<concatenate>
sub['FVC1'] = 1.*pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
subm.loc[~subm.FVC1.isnull() ].head(10 )
class ImageDataGenerator2(ImageDataGenerator):
def flow(self, X, y=None, batch_size=32, shuffle=True, seed=None,
save_to_dir=None, save_prefix='', save_format='jpeg'):
return NumpyArrayIterator2(
 X, y, self,
batch_size=batch_size, shuffle=shuffle, seed=seed,
dim_ordering=self.dim_ordering,
save_to_dir=save_to_dir, save_prefix=save_prefix, save_format=save_format)
 class NumpyArrayIterator2(NumpyArrayIterator):
def next(self):
with self.lock:
self.index_array, current_index, current_batch_size = next(self.index_generator)
 batch_x = np.zeros(tuple([current_batch_size] + list(self.X.shape)[1:]))
for i, j in enumerate(self.index_array):
x = self.X[j]
x = self.image_data_generator.random_transform(x.astype('float32'))
x = self.image_data_generator.standardize(x)
 batch_x[i] = x
if self.save_to_dir:
for i in range(current_batch_size):
img = array_to_img(batch_x[i], self.dim_ordering, scale=True)
 fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
index=current_index + i,
hash=np.random.randint(1e4),
format=self.save_format)
 img.save(os.path.join(self.save_to_dir, fname))
if self.y is None:
return batch_x
batch_y = self.y[self.index_array]
return batch_x, batch_y
print('Creating Data Augmenter...')
 imgen = ImageDataGenerator2(
 rotation_range=20,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 imgen_train = imgen.flow(X_img_tr, y_tr_cat, seed=np.random.randint(1, 10000))
print('Finished making data augmenter...' )<choose_model_class>
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']
def combined_model() :
image = Input(shape=(96, 96, 1), name='image')
 x = Convolution2D(8, 5, 5, input_shape=(96, 96, 1), border_mode='same' )(image)
 x =(Activation('relu'))(x)
 x =(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) )(x)
 x =(Convolution2D(32, 5, 5, border_mode='same'))(x)
 x =(Activation('relu'))(x)
 x =(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) )(x)
 x = Flatten()(x)
 numerical = Input(shape=(192,), name='numerical')
 concatenated = merge([x, numerical], mode='concat')
 x = Dense(100, activation='relu' )(concatenated)
 x = Dropout (.5 )(x)
 out = Dense(99, activation='softmax' )(x)
 model = Model(input=[image, numerical], output=out)
 model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
 return model
print('Creating the model...')
 model = combined_model()
print('Model created!' )<prepare_x_and_y>
otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 for i in range(len(otest)) :
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1
def combined_generator(imgen, X):

 while True:
for i in range(X.shape[0]):
batch_img, batch_y = next(imgen)
 x = X[imgen.index_array]
yield [batch_img, x], batch_y
best_model_file = "leafnet.h5"
best_model = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=1, save_best_only=True)
 print('Training model...')
 history = model.fit_generator(combined_generator(imgen_train, X_num_tr),
samples_per_epoch=X_num_tr.shape[0],
nb_epoch=89,
validation_data=([X_img_val, X_num_val], y_val_cat),
nb_val_samples=X_num_val.shape[0],
verbose=0,
callbacks=[best_model])
 print('Loading the best model...')
 model = load_model(best_model_file)
 print('Best Model loaded!' )<save_to_csv>
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission_regression.csv", index=False )
LABELS = sorted(pd.read_csv(os.path.join(root, 'train.csv')).species.unique())
 index, test, X_img_te = load_test_data()
yPred_proba = model.predict([X_img_te, test])
 yPred = pd.DataFrame(yPred_proba,index=index,columns=LABELS)
 print('Creating and writing submission...')
 fp = open('submit.csv', 'w')
 fp.write(yPred.to_csv())
 print('Finished writing submission')
 yPred.tail()<set_options>
reg_sub = subm[["Patient_Week","FVC","Confidence"]].copy()
%pylab inline 
<import_modules>
df1 = img_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True)
 df2 = reg_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True )
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder<import_modules>
df = df1[['Patient_Week']].copy()
df['FVC'] = FVC_weight*df1['FVC'] +(1-FVC_weight)*df2['FVC']
df['Confidence'] = Confidence_weight*df1['Confidence'] +(1-Confidence_weight)*df2['Confidence']
df.head()
<load_from_csv><EOS> 
df.to_csv('submission.csv', index=False )