Kaggle competition: Predicting a Biological Response
<SOS> metric: LogLoss Kaggle data source: predicting-a-biological-response<feature_engineering>
%matplotlib inline
warnings.filterwarnings('ignore')
whole_data['FamilyId'] = 0
x = 1
for tix in FamilyWithChild:
whole_data.loc[whole_data.SurTix==tix, ['FamilyId']] = x
x += 1<prepare_x_and_y>
data = pd.read_csv(".. /input/bioresponse/train.csv")
 print(data.head(10))
X_train = train_data.drop(['Age', 'Embarked', 'Fare', 'Name', 'Parch', 'PassengerId', 'Sex', 'SibSp', 'Survived', 'Ticket', 'Title', 'AgeBin_5', 'FareBin_5', 'FamilySize', 'Surname', 'TixPref', 'SurTix', 'IsFamily', 'Child', 'FamilyId'], axis=1)
 y_train = train_data['Survived']<choose_model_class>
print("Missing Values",data.isnull().sum().sum())
 print("Column names",data.columns )
model = RandomForestClassifier(n_estimators=200, random_state=2 )<define_variables>
Scaleddata = MinMaxScaler().fit_transform(data)
 data = pd.DataFrame(Scaleddata,columns=data.columns )
final = ['Title_Code', 'Sex_Code', 'ConnectedSurvival', 'Pclass', 'FareBin_Code_5']<train_on_grid>
train = pd.read_csv(".. /input/bioresponse/train.csv")
 test = pd.read_csv(".. /input/bioresponse/test.csv")
 train['label'] = 0
test['label'] = 1
training = train.drop('Activity',axis=1)
 combine = training.append(test)
 y =combine['label']
combine.drop(columns = ['label'],inplace=True)
 model = LGBMClassifier(n_estimators = 50)
 drop_list = []
for col in combine.columns:
score = cross_val_score(model,pd.DataFrame(combine[col]),y,cv=2,scoring='roc_auc')
 if(np.mean(score)> 0.7):
drop_list.append(col)
 print("Column with covariate shift:", col )
grid_param = {
'n_estimators': [100, 200, 300],
'criterion':['gini', 'entropy'],
'min_samples_split': [2, 10, 20],
'min_samples_leaf': [1, 5],
'bootstrap': [True, False],
}
gd_sr = GridSearchCV(estimator=model,
param_grid=grid_param,
scoring='accuracy',
cv=5,
n_jobs=-1)
 gd_sr.fit(X_train[final], y_train)
 best_parameters = gd_sr.best_params_
print(best_parameters )<choose_model_class>
if len(drop_list)==0:
print("No presence of covariate shift" )
model1 = RandomForestClassifier(n_estimators=300, bootstrap=True, criterion= 'entropy', min_samples_leaf=5, min_samples_split=2, random_state=2 )<compute_train_metric>
data = pd.read_csv(".. /input/bioresponse/train.csv")
 X = data
X = X.drop(columns='Activity')
 y = data['Activity']
y = y.values
y = y.reshape(( len(y), 1))
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=32)
 columns = X_train.columns
for col in columns:
MinMax = MinMaxScaler()
X_train_arr = X_train[col].astype(float ).values
X_test_arr = X_test[col].astype(float ).values
X_train_arr = MinMax.fit_transform(X_train_arr.reshape(-1,1))
X_test_arr = MinMax.transform(X_test_arr.reshape(-1,1))
X_train[col] = X_train_arr
X_test[col] = X_test_arr
all_accuracies = cross_val_score(estimator=model1, X=X_train, y=y_train, cv=5)
 all_accuracies
all_accuracies.mean()<save_to_csv>
X_test = test_data[final]
model.fit(X_train[final],y_train)
 prediction = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': prediction.astype(int)})
 output.to_csv('my_submission.csv', index=False )<load_from_csv>
model = LGBMClassifier(random_state=314, silent=True, n_jobs=2,subsample_freq = 20, subsample = 0.9,
reg_lambda = 1.2, reg_alpha = 1.1,num_leaves= 200, n_estimators = 700,
min_split_gain = 0.4, max_depth = 15, colsample_bytree = 0.8 )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_data.head(5)
<categorify>
model.fit(X_train, y_train)
 pred = model.predict_proba(X_test)
 print("Log Loss Probability: ",log_loss(y_test,pred))
def outlier_detection(dataframe,features, critical=1.5):
ind = []
for column in features:
q1 = np.percentile(dataframe[column],25)
 q3 = np.percentile(dataframe[column],75)
 res = q3 - q1
new = critical * res
out = dataframe[(dataframe[column] < q1 - new)|(dataframe[column] > q3 + new)].index
ind.extend(out)
 ind = Counter(ind)
 val = list(s for s,i in ind.items())
 return val
ott = outlier_detection(train_data,["Fare"])
 train_data.iloc[ott]<define_variables>
X_train = pd.read_csv(".. /input/bioresponse/train.csv")
 X_test = pd.read_csv(".. /input/bioresponse/test.csv")
 y_train = X_train["Activity"]
X_train.drop(columns=["Activity"],inplace=True)
 columns = X_train.columns
for col in columns:
MinMax = MinMaxScaler()
X_train_arr = X_train[col].astype(float ).values
X_test_arr = X_test[col].astype(float ).values
X_train_arr = MinMax.fit_transform(X_train_arr.reshape(-1,1))
X_test_arr = MinMax.transform(X_test_arr.reshape(-1,1))
X_train[col] = X_train_arr
X_test[col] = X_test_arr
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("%.3f of women who survived" % rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("%.3f of men who survived" % rate_men )<data_type_conversions>
model = LGBMClassifier(random_state=314, silent=True, n_jobs=2,subsample_freq = 20, subsample = 0.9,
reg_lambda = 1.2, reg_alpha = 1.1,num_leaves= 200, n_estimators = 700,
min_split_gain = 0.4, max_depth = 15, colsample_bytree = 0.8 )
features = ["PassengerId", "Name", "Pclass", "Sex", "Age", "SibSp", "Parch", "Ticket", "Fare", "Cabin", "Embarked"]
num_train, num_test = len(train_data), len(test_data)
 print(num_train, num_test)
 merged_data = pd.concat([train_data[features], test_data[features]], axis=0, ignore_index=True)
 merged_data['Age'] = merged_data['Age'].fillna(merged_data['Age'].mean())
 merged_data['Fare'] = merged_data['Fare'].fillna(merged_data['Fare'].mean())
 merged_data[['Cabin','Embarked']] = merged_data[['Cabin','Embarked']].fillna(value='Unknown')
 merged_data<categorify>
model.fit(X_train,y_train )
y = train_data["Survived"]
winsorized_X = pd.get_dummies(merged_data[["Pclass", "Name", "Sex", "Age", "SibSp", "Parch", "Ticket", "Fare", "Embarked"]])
 winsorized_X.info()<split>
predicted_prob = model.predict_proba(X_test )
X_train = winsorized_X.iloc[0:num_train]
X_test = winsorized_X.iloc[num_train:]
ros = RandomOverSampler(random_state=1)
 X_ros, y_ros = ros.fit_resample(X_train, y)
 x_train, x_valid, y_train, y_valid = train_test_split(X_ros, y_ros, test_size=0.2, random_state=1)
 d_train = xgb.DMatrix(x_train, label=y_train)
 d_valid = xgb.DMatrix(x_valid, label=y_valid)
 d_test = xgb.DMatrix(X_test)
 params = {
'objective':'binary:hinge',
'max_depth':12,
'learning_rate':0.02,
'eval_metric':'error',
'min_child_weight':1,
'subsample':1,
'colsample_bytree':0.4,
'seed':29,
'reg_lambda':2.8,
'reg_alpha':0,
'gamma':0,
'nthread':-1
}
watchlist = [(d_train, 'train'),(d_valid, 'valid')]
nrounds=5000
model = xgb.train(params, d_train, nrounds, watchlist, early_stopping_rounds=600, maximize=False, verbose_eval=30)
<compute_test_metric>
Probability = predicted_prob[:,1]
MoleculeId = np.array(range(1,len(X_test)+1))
y_pred = model.predict(d_valid)
 print('Accuracy :{0:0.5f}'.format(metrics.accuracy_score(y_valid, y_pred)))
 print('AUC : {0:0.5f}'.format(metrics.roc_auc_score(y_valid, y_pred)))
 print('Precision : {0:0.5f}'.format(metrics.precision_score(y_valid, y_pred)))
 print('Recall : {0:0.5f}'.format(metrics.recall_score(y_valid, y_pred)))
 print('F1 : {0:0.5f}'.format(metrics.f1_score(y_valid, y_pred)) )<save_to_csv>
submission = pd.DataFrame()
submission["MoleculeId"] = MoleculeId
submission['PredictedProbability'] = Probability
<import_modules><EOS> 
submission.to_csv('submission.csv',index=None )