Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import xgboost as xgb
import lightgbm as lgb
import re
import shap
from sklearn import preprocessing
from category_encoders.one_hot import OneHotEncoder
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.model_selection import cross_val_score, KFold,GridSearchCV
from sklearn.feature_selection import SelectKBest,SelectFromModel
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline,make_pipeline
from sklearn.ensemble import RandomForestClassifier,VotingClassifier,BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import linear_model
from sklearn.linear_model import LogisticRegression,Perceptron,SGDClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
df_sample_sub['Season'] = df_sample_sub['ID'].apply(lambda x : int(x.split('_')[0]))
df_sample_sub['TeamID1'] = df_sample_sub['ID'].apply(lambda x : int(x.split('_')[1]))
df_sample_sub['TeamID2'] = df_sample_sub['ID'].apply(lambda x : int(x.split('_')[2]))<drop_column>
test = pd.read_csv(".. /input/titanic/test.csv")
 test['Survived'] = np.nan
train = pd.read_csv(".. /input/titanic/train.csv")
 df = pd.concat([test,train],sort=False)
 df.head()
winners = df_concat.rename(columns = { 'WTeamID' : 'TeamID1',
'LTeamID' : 'TeamID2',
'WScore' : 'Team1_Score',
'LScore' : 'Team2_Score'} ).drop(['WSeed', 'LSeed', 'WLoc'], axis = 1)
 winners['Result'] = 1.0
losers = df_concat.rename(columns = { 'WTeamID' : 'TeamID2',
'LTeamID' : 'TeamID1',
'WScore' : 'Team2_Score',
'LScore' : 'Team1_Score'} ).drop(['WSeed', 'LSeed', 'WLoc'], axis = 1)
 losers['Result'] = 0.0
train = pd.concat([winners, losers], axis = 0 ).reset_index(drop = True)
 train['Score_Ratio'] = train['Team1_Score'] / train['Team2_Score']
train['Score_Total'] = train['Team1_Score'] + train['Team2_Score']
train['Score_Pct'] = train['Team1_Score'] / train['Score_Total']<define_variables>
print(df.corr() ['Age'].sort_values(ascending=False))
df['Age'] = df.groupby("Pclass")['Age'].transform(lambda x: x.fillna(x.median()))
years = [2014, 2015, 2016, 2017]<merge>
print(df.corr() ['Fare'].sort_values(ascending=False))
df['Fare'] = df.groupby("Pclass")['Fare'].transform(lambda x: x.fillna(x.median()))
train_test_inner = pd.merge(train.loc[ train['Season'].isin(years), : ].reset_index(drop = True),
df_sample_sub.drop(['ID', 'Pred'], axis = 1),
on = ['Season', 'TeamID1', 'TeamID2'], how = 'inner' )<groupby>
df['Embarked'].fillna(df['Embarked'].mode() [0], inplace = True)
 df["Fsize"] = df["SibSp"] + df["Parch"] + 1
df['IsAlone'] = np.where(df["Fsize"]==1,1,0)
 df['Age_Class']= df['Age']* df['Pclass']
df['Age_cut'] = pd.qcut(df['Age'],5,duplicates='drop')
 df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr','Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Title']= df['Title'].fillna(0)
 df['Fare_Per_Person'] = df['Fare']/(df["Fsize"]+1)
 df['Fare_Per_Person'] = df['Fare_Per_Person'].astype(int)
 df['Fare_cut'] = pd.qcut(df['Fare'],5,duplicates='drop')
 df['Cabin_len'] = df['Cabin'].astype(str ).apply(lambda x : len(x))
df['Cabin'] = df['Cabin'].fillna("M")
 df['Cabin'] = df['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 df['Ticket']=df.Ticket.apply(lambda x : len(x))
df = df.drop(['Name'], axis = 1)
 df.head()
def geo_mean(x):
return np.exp(np.mean(np.log(x)))
 def harm_mean(x):
return np.mean(x ** -1.0)** -1.0
team1d_score_spread = train_test_inner.groupby(['Season', 'TeamID1'])[['Score_Ratio', 'Score_Pct']]\ 
.agg({ 'Score_Ratio': geo_mean, 'Score_Pct' : harm_mean} ).reset_index() \ 
.set_index('Season' ).rename(columns = {'Score_Ratio' : 'Score_Ratio1', 'Score_Pct' : 'Score_Pct1'})
 team2d_score_spread = train_test_inner.groupby(['Season', 'TeamID2'])[['Score_Ratio', 'Score_Pct']]\ 
.agg({ 'Score_Ratio': geo_mean, 'Score_Pct' : harm_mean} ).reset_index() \ 
.set_index('Season' ).rename(columns = {'Score_Ratio' : 'Score_Ratio2', 'Score_Pct' : 'Score_Pct2'})
 score_spread = team1d_score_spread.join(team2d_score_spread ).reset_index()
score_spread['Score_Ratio'] = score_spread[['Score_Ratio1', 'Score_Ratio2']].apply(lambda x :(x[0] *(x[1] ** -1.0)) , axis = 1)** 0.5
score_spread['Score_Pct'] = score_spread[['Score_Pct1', 'Score_Pct2']].apply(lambda x : 0.5*(x[0] ** -1.0)+ 0.5*(1.0 - x[1])** -1.0, axis = 1)** -1.0
score_spread.head()<merge>
scaler = preprocessing.StandardScaler().fit(df[['Age','Fare']])
 df[['Age','Fare']] = scaler.transform(df[['Age','Fare']] )
X_train = train_test_inner.loc[:, ['Season', 'NumOT', 'Score_Ratio', 'Score_Pct']]
train_labels = train_test_inner['Result']
train_test_outer = pd.merge(train.loc[ train['Season'].isin(years), : ].reset_index(drop = True),
df_sample_sub.drop(['ID', 'Pred'], axis = 1),
on = ['Season', 'TeamID1', 'TeamID2'], how = 'outer')
 train_test_outer = train_test_outer.loc[ train_test_outer['Result'].isnull() ,
['TeamID1', 'TeamID2', 'Season']]
train_test_missing = pd.merge(pd.merge(score_spread.loc[:, ['TeamID1', 'TeamID2', 'Season', 'Score_Ratio', 'Score_Pct']],
train_test_outer, on = ['TeamID1', 'TeamID2', 'Season']),
num_ot.loc[:, ['TeamID1', 'TeamID2', 'Season', 'NumOT']],
on = ['TeamID1', 'TeamID2', 'Season'] )<categorify>
df['Age_cut'] = LabelEncoder().fit_transform(df['Age_cut'])
 df['Fare_cut'] = LabelEncoder().fit_transform(df['Fare_cut'])
 titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
df['Title']= df['Title'].map(titles)
 groups = {"M": 1, "C": 2, "B": 3, "D": 4, "E": 5, "A": 6, "F": 7, "G": 8,'T':9}
df['Cabin'] = df['Cabin'].map(groups)
 ports = {"S": 0, "C": 1, "Q": 2}
df['Embarked'] = df['Embarked'].map(ports)
 df['Sex_female'] = np.where(df['Sex'] == 'female',1,0)
 df['Sex_male'] = np.where(df['Sex'] == 'male',1,0)
 df = df.drop(['Sex'],axis=1 )
X_test = train_test_missing.loc[:, ['Season', 'NumOT', 'Score_Ratio', 'Score_Pct']]
n = X_train.shape[0]
train_test_merge = pd.concat([X_train, X_test], axis = 0 ).reset_index(drop = True)
 train_test_merge = pd.concat([pd.get_dummies(train_test_merge['Season'].astype(object)) ,
train_test_merge.drop('Season', axis = 1)], axis = 1)
 train_test_merge = pd.concat([pd.get_dummies(train_test_merge['NumOT'].astype(object)) ,
train_test_merge.drop('NumOT', axis = 1)], axis = 1)
 X_train = train_test_merge.loc[:(n - 1), :].reset_index(drop = True)
 X_test = train_test_merge.loc[n:, :].reset_index(drop = True )<feature_engineering>
dummies = OneHotEncoder(cols= ['Pclass','Age_cut','Fare_cut','Embarked','Title'],use_cat_names=True)
 dummies.fit(df)
 df = dummies.transform(df )
x_max = X_train.max()
x_min = X_train.min()
X_train =(X_train - x_min)/(x_max - x_min + 1e-14)
 X_test =(X_test - x_min)/(x_max - x_min + 1e-14 )<train_model>
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
upper.head()
log_clf = LogisticRegressionCV(cv = 5)
 log_clf.fit(X_train, train_labels )<predict_on_test>
x_valid = df[df['Survived'].isna() ].drop(['Survived'], axis = 1)
 submission = x_valid['PassengerId'].to_frame()
x1 = df[df['Survived']==0]
x2 = df[df['Survived']==1]
x= pd.concat([x1,x2])
 y =x.loc[:,['Survived','PassengerId']]
x =x.drop(['Survived','PassengerId'], axis = 1 )
train_test_inner['Pred1'] = log_clf.predict_proba(X_train)[:,1]
train_test_missing['Pred1'] = log_clf.predict_proba(X_test)[:,1]<merge>
teste = pd.read_csv(".. /input/testey/full-tita.csv",sep=";")
 teste = teste.drop(['x'],axis=1)
 teste
x_valid = x_valid.merge(teste)
 x_valid = x_valid.drop(['Survived'],axis = 1)
 y_valid = teste['Survived']
sub = pd.merge(df_sample_sub,
pd.concat([train_test_missing.loc[:, ['Season', 'TeamID1', 'TeamID2', 'Pred1']],
train_test_inner.loc[:, ['Season', 'TeamID1', 'TeamID2', 'Pred1']] ],
axis = 0 ).reset_index(drop = True),
on = ['Season', 'TeamID1', 'TeamID2'], how = 'outer' )<groupby>
kf = KFold(n_splits=5, shuffle=True, random_state=42 )
team1_probs = sub.groupby('TeamID1')['Pred1'].apply(lambda x :(x ** -1.0 ).mean() ** -1.0 ).fillna(0.5 ).to_dict()
team2_probs = sub.groupby('TeamID2')['Pred1'].apply(lambda x :(x ** -1.0 ).mean() ** -1.0 ).fillna(0.5 ).to_dict()<feature_engineering>
lr = LogisticRegression(class_weight = 'balanced', solver = 'liblinear',penalty="l2")
 lr.fit(x,y['Survived'])
 print(cross_val_score(lr, x, y['Survived'], cv=kf ).mean() )
sub['Pred'] = sub[['TeamID1', 'TeamID2','Pred1']]\ 
.apply(lambda x : team1_probs.get(x[0])*(1 - team2_probs.get(x[1])) if np.isnan(x[2])else x[2],
axis = 1 )<save_to_csv>
lr = LogisticRegression(class_weight = 'balanced', solver = 'liblinear',penalty="l1")
 lr.fit(x,y['Survived'])
 print(cross_val_score(lr, x_valid, y_valid, cv=kf ).mean() )
sub[['ID', 'Pred']].to_csv('sub.csv', index = False )<set_options>
svm = SVC(gamma='auto',random_state=42)
 svm.fit(x,y['Survived'])
 print(cross_val_score(svm, x, y['Survived'], cv=kf ).mean() )
%matplotlib inline 
<load_from_csv>
svm = SVC(gamma='auto',random_state=42)
 svm.fit(x,y['Survived'])
 print(cross_val_score(svm, x_valid, y_valid, cv=kf ).mean() )
print(check_output(["ls", ".. /input"] ).decode("utf8"))
df = pd.read_csv('.. /input/data.csv')
 df.info()<compute_train_metric>
sgd = linear_model.SGDClassifier(max_iter=5, tol=None)
 sgd.fit(x,y['Survived'])
 print(cross_val_score(sgd, x, y['Survived'], cv=kf ).mean() )
data = df[['loc_x', 'loc_y', 'shot_made_flag']]
data = data.dropna()
def test_it(data):
clf = RandomForestClassifier(n_jobs=-1)
 return cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='roc_auc', cv=10 
)
 test_it(data ).mean()<categorify>
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(x,y['Survived'])
 print(cross_val_score(knn, x, y['Survived'], cv=kf ).mean() )
data = df[['loc_y', 'shot_made_flag']]
data = data.dropna()
test_it(data ).mean()<categorify>
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(x,y['Survived'])
 print(cross_val_score(knn, x_valid, y_valid, cv=kf ).mean() )
data = df[['shot_distance', 'shot_made_flag']]
data = data.dropna()
test_it(data ).mean()<data_type_conversions>
gaussian = GaussianNB()
gaussian.fit(x,y['Survived'])
 print(cross_val_score(gaussian, x, y['Survived'], cv=kf ).mean() )
print(df.season.unique())
 df['season_start_year'] = df.season.str.split('-' ).str[0]
df['season_start_year'] = df['season_start_year'].astype(int)
 get_acc(df, 'season_start_year' )<categorify>
rf_with_standardScaler=make_pipeline(StandardScaler() ,RandomForestClassifier())
 rf_with_standardScaler.fit(x,y['Survived'])
 print(cross_val_score(rf_with_standardScaler, x, y['Survived'], cv=kf ).mean() )
action_map = {action: i for i, action in enumerate(df.action_type.unique())}
df['action_type_enumerated'] = df.action_type.map(action_map)
 get_acc(df, 'action_type_enumerated' )<categorify>
rf_with_standardScaler=make_pipeline(StandardScaler() ,RandomForestClassifier())
 rf_with_standardScaler.fit(x,y['Survived'])
 print(cross_val_score(rf_with_standardScaler, x_valid, y_valid, cv=kf ).mean() )
def sort_encode(df, field):
ct = pd.crosstab(df.shot_made_flag, df[field] ).apply(lambda x:x/x.sum() , axis=0)
 temp = list(zip(ct.values[1, :], ct.columns))
temp.sort()
new_map = {}
for index,(acc, old_number)in enumerate(temp):
new_map[old_number] = index
new_field = field + '_sort_enumerated'
df[new_field] = df[field].map(new_map)
 get_acc(df, new_field)
 sort_encode(df, 'action_type_enumerated' )<prepare_x_and_y>
rf=RandomForestClassifier(n_estimators=100, oob_score = True)
 rf.fit(x,y['Survived'])
 print(cross_val_score(rf, x, y['Survived'], cv=kf ).mean())
 parametros = pd.DataFrame({'feature':x.columns,'Parameters':np.round(rf.feature_importances_,3)})
 parametros = parametros.sort_values('Parameters',ascending=False ).set_index('feature')
 parametros
data = df[['action_type_enumerated', 'shot_distance', 'shot_made_flag']].dropna()
x = test_it(data)
 data = df[['action_type_enumerated_sort_enumerated', 'shot_distance', 'shot_made_flag']].dropna()
y = test_it(data)
 print(x.mean() , y.mean() )<categorify>
rf=RandomForestClassifier(n_estimators=100)
 rf.fit(x,y['Survived'])
 print(cross_val_score(rf, x_valid, y_valid, cv=kf ).mean())
opponent_map = {opp: i for i, opp in enumerate(df.opponent.unique())}
df['opponent_enumerated'] = df.opponent.map(opponent_map)
 sort_encode(df, 'opponent_enumerated' )<feature_engineering>
bagging = BaggingClassifier(bootstrap=True,n_jobs = -1,n_estimators=100)
 bagging.fit(x,y['Survived'])
 print(cross_val_score(bagging, x, y['Survived'], cv=kf ).mean() )
df['away'] = df.matchup.str.contains('@')
 data = df[['action_type_enumerated', 'shot_distance',
'shot_made_flag', 'away']].dropna()
test_it(data ).mean()<find_best_model_class>
bagging = BaggingClassifier(bootstrap=True,n_jobs = -1,n_estimators=100)
 bagging.fit(x,y['Survived'])
 print(cross_val_score(bagging, x_valid, y_valid, cv=kf ).mean() )
data = df[['action_type_enumerated', 'shot_distance',
'shot_made_flag', 'away']].dropna()
estimators, scores = list(range(1, 100, 5)) , []
for i in estimators:
clf = RandomForestClassifier(n_jobs=-1, n_estimators=i, random_state=2016)
 x = cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='roc_auc', cv=10)
 scores.append(x)
 x = [i for i in estimators for j in range(10)]
sns.boxplot(x, np.array(scores ).flatten() )<choose_model_class>
xgboost = xgb.XGBClassifier(objective ='reg:logistic'
, colsample_bytree = 0.7
, learning_rate = 0.01
,max_depth = 6
, n_estimators = 100
,random_state=42
,max_features= 0.8
,min_samples_leaf =0.5
,min_child_weight= 3)
 xgboost.fit(x,y['Survived'])
 print(cross_val_score(xgboost, x, y['Survived'], cv=kf ).mean() )
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 train = df.loc[~df.shot_made_flag.isnull() , ['action_type_enumerated_sort_enumerated',
'shot_distance', 'shot_made_flag', 'away']]
test = df.loc[df.shot_made_flag.isnull() , ['action_type_enumerated_sort_enumerated',
'shot_distance', 'shot_id', 'away']]
mode = test.action_type_enumerated_sort_enumerated.mode() [0]
test.action_type_enumerated_sort_enumerated.fillna(mode, inplace=True)
 clf.fit(train.drop('shot_made_flag', 1), train.shot_made_flag)
 predictions = clf.predict_proba(test.drop('shot_id', 1))<save_to_csv>
xgboost = xgb.XGBClassifier(colsample_bytree = 0.8
, learning_rate = 0.05
,max_depth = 6
, n_estimators = 100
,random_state=42 
)
 xgboost.fit(x,y['Survived'])
 print(cross_val_score(xgboost, x_valid, y_valid, cv=kf ).mean() )
submission = pd.DataFrame({'shot_id': test.shot_id,
'shot_made_flag': predictions[:, 1]})
 submission[['shot_id', 'shot_made_flag']].to_csv('submission.csv', index=False )<set_options>
lgbm=lgb.LGBMClassifier()
lgbm.fit(x,y['Survived'])
 print(cross_val_score(lgbm, x, y['Survived'], cv=kf ).mean() )
%matplotlib inline
sns.set() 
<load_pretrained>
lgbm=lgb.LGBMClassifier(colsample_bytree = 0.8
, learning_rate = 0.015
,max_depth = 5
, n_estimators = 100
,random_state=42)
 lgbm.fit(x,y['Survived'])
 print(cross_val_score(lgbm, x_valid, y_valid, cv=kf ).mean() )
def detect(save_img=False):
weights, imgsz = opt.weights,opt.img_size
source = '/kaggle/input/global-wheat-detection/test'
device = torch_utils.select_device(opt.device)
 half = device.type != 'cpu'
model = torch.load(weights, map_location=device)['model'].to(device ).eval()
if half:
model.half()
dataset = LoadImages(source, img_size=1024)
 t0 = time.time()
img = torch.zeros(( 1, 3, imgsz, imgsz), device=device)
 results=[]
_ = model(img.half() if half else img)if device.type != 'cpu' else None
for path, img, im0s, vid_cap in dataset:
print(im0s.shape)
 im0s = cv2.cvtColor(im0s, cv2.COLOR_BGR2RGB)
 image_id = path.split("/")[-1].split(".")[0]
img = torch.from_numpy(img ).to(device)
 img = img.half() if half else img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 t1 = torch_utils.time_synchronized()
if True:
pred = model(img, augment=opt.augment)[0]
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=None, merge=opt.merge)
 t2 = torch_utils.time_synchronized()
box_scores = []
for i, det in enumerate(pred):
p, s, im0 = path, '', im0s
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape ).round()
for c in det[:, -1].unique() :
n =(det[:, -1] == c ).sum()
for *xyxy, conf, cls in det:
if True:
xywh =(xyxy2xywh(torch.tensor(xyxy ).view(1, 4)) / gn ).view(-1 ).tolist()
c1, c2 =(int(xyxy[0]), int(xyxy[1])) ,(int(xyxy[2])- int(xyxy[0]), int(xyxy[3])- int(xyxy[1]))
box_scores.append(f"{round(conf.item() ,2)} {c1[0]} {c1[1]} {c2[0]} {c2[1]}")
 cv2.putText(im0s, '{:.2}'.format(conf.item()),(c1[0],c1[1]-10), cv2.FONT_HERSHEY_SIMPLEX ,1,(255,255,255), 2, cv2.LINE_AA)
 cv2.rectangle(im0s, c1,(c2[0]+c1[0], c2[1]+c1[1]),(255,255,255),2)
 result={'image_id': image_id, "PredictionString": " ".join(box_scores)}
fig = plt.figure(figsize=(50, 50))
a = fig.add_subplot(1, 4, 1)
 imgplot = plt.imshow(im0s)
 results.append(result)
 return results<init_hyperparams>
stacking = VotingClassifier(estimators=[ 
('lr',lr),('svm',svm),('rf', rf)], voting='hard')
 stacking.fit(x,y['Survived'])
 print(cross_val_score(stacking, x, y['Survived'], cv=kf ).mean() )
class opt:
weights = ".. /input/20200704-1-yolov5-ricardo/20200704_1.pt"
img_size = 1024
conf_thres = 0.45
iou_thres = 0.45
augment = True
merge = True
device = '0'
classes=None
agnostic_nms = True
opt.img_size = check_img_size(opt.img_size)
 print(opt)
 with torch.no_grad() :
res = detect()<create_dataframe>
stacking = VotingClassifier(estimators=[ 
('lr',rf),('svm',knn),('xgboost', xgboost)], voting='hard')
 stacking.fit(x,y['Survived'])
 print(cross_val_score(stacking, x_valid, y_valid, cv=kf ).mean() )
test_df = pd.DataFrame(res, columns=['image_id', 'PredictionString'])
 print(test_df )<save_to_csv>
parameters = {
'max_features': [0.8, 0.9],
'min_samples_leaf' :[0.5,0.9],
'colsample_bytree' :[0.8,0.9],
'learning_rate' : [0.01,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 6)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x,y['Survived'])
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
test_df.to_csv("/kaggle/working/submission.csv",index=False )<load_from_csv>
parametros = pd.DataFrame({'feature':x.columns,'Parameters':np.round(xgboost.feature_importances_,3)})
 parametros = parametros.sort_values('Parameters',ascending=False ).set_index('feature')
 drop = list(parametros.tail(30 ).index)
 print(drop)
 x = x.drop(drop,axis=1)
 x_valid = x_valid.drop(drop,axis=1 )
!cat submission.csv<install_modules>
parameters = {
'max_features': [0.7,0.8, 0.9],
'min_samples_leaf' :[0.6,0.7,0.9],
'colsample_bytree' :[0.6,0.8,0.9],
'learning_rate' : [0.01,0.03,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 7)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x,y['Survived'])
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<categorify>
parameters = {
'max_features': [0.7,0.8, 0.9],
'min_samples_leaf' :[0.6,0.7,0.9],
'colsample_bytree' :[0.6,0.8,0.9],
'learning_rate' : [0.01,0.03,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 6)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x_valid,y_valid)
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
def get_valid_transforms() :
return A.Compose([
A.Resize(height=1024, width=1024, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
svc=make_pipeline(StandardScaler() ,SVC(random_state=1))
r=[0.0001,0.001,0.1,1,10,50,100]
PSVM=[{'svc__C':r, 'svc__kernel':['linear']},
{'svc__C':r, 'svc__gamma':r, 'svc__kernel':['rbf']}]
GSSVM=GridSearchCV(estimator=svc, param_grid=PSVM, scoring='accuracy', cv=2)
 scores_svm=cross_val_score(GSSVM, x_valid, y_valid,scoring='accuracy', cv=5)
 np.mean(scores_svm )
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<load_pretrained>
Survived = grid_search2.best_estimator_.predict(x_valid)
 submission['Survived'] = Survived
submission['Survived'] = submission['Survived'].astype(int)
 submission.to_csv('submission.csv' , index=False )
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=4,
shuffle=False,
num_workers=2,
drop_last=False,
collate_fn=collate_fn
)<train_on_grid>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import xgboost as xgb
import lightgbm as lgb
import re
import shap
from sklearn import preprocessing
from category_encoders.one_hot import OneHotEncoder
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.model_selection import cross_val_score, KFold,GridSearchCV
from sklearn.feature_selection import SelectKBest,SelectFromModel
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline,make_pipeline
from sklearn.ensemble import RandomForestClassifier,VotingClassifier,BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import linear_model
from sklearn.linear_model import LogisticRegression,Perceptron,SGDClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d6')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=1024
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
net = load_net('.. /input/wheat-effdet5-fold0-best-checkpoint/fold0-best-all-states.bin')
 try:
checkpoint = torch.load(".. /input/ef61024/EF6")
 net.load_state_dict(checkpoint)
 except Exception as exc:
print(traceback.format_exc())
 print(exc)
<categorify>
test = pd.read_csv(".. /input/titanic/test.csv")
 test['Survived'] = np.nan
train = pd.read_csv(".. /input/titanic/train.csv")
 df = pd.concat([test,train],sort=False)
 df.head()
class BaseWheatTTA:

 image_size = 1024
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(1)
 def batch_augment(self, images):
return images.flip(2)
 def deaugment_boxes(self, boxes):
boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(2)
 def batch_augment(self, images):
return images.flip(3)
 def deaugment_boxes(self, boxes):
boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 1,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = self.image_size - boxes[:, [1,3]]
res_boxes[:, [1,3]] = boxes[:, [2,0]]
return res_boxes
class TTARotate180(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 2,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 2,(2, 3))
def deaugment_boxes(self, boxes):
boxes[:, [0,1,2,3]] = self.image_size - boxes[:, [2,3,0,1]]
return boxes
class TTARotate270(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 3,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 3,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = boxes[:, [1,3]]
res_boxes[:, [1,3]] = self.image_size - boxes[:, [2,0]]
return res_boxes
class TTACompose(BaseWheatTTA):

 def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
 return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
 return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)
 result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)
 result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)
 result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)
 return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
 return self.prepare_boxes(boxes )<categorify>
print(df.corr() ['Age'].sort_values(ascending=False))
df['Age'] = df.groupby("Pclass")['Age'].transform(lambda x: x.fillna(x.median()))
def process_det(index, det, score_threshold=0.25):
boxes = det[index].detach().cpu().numpy() [:,:4]
scores = det[index].detach().cpu().numpy() [:,4]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes =(boxes ).clip(min=0, max=511 ).astype(int)
 indexes = np.where(scores>score_threshold)
 boxes = boxes[indexes]
scores = scores[indexes]
return boxes, scores<concatenate>
print(df.corr() ['Fare'].sort_values(ascending=False))
df['Fare'] = df.groupby("Pclass")['Fare'].transform(lambda x: x.fillna(x.median()))
tta_transforms = []
for tta_combination in product([TTAHorizontalFlip() , None],
[TTAVerticalFlip() , None],
[TTARotate90() , TTARotate180() , TTARotate270() , None]):
tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))<categorify>
df['Embarked'].fillna(df['Embarked'].mode() [0], inplace = True)
 df["Fsize"] = df["SibSp"] + df["Parch"] + 1
df['IsAlone'] = np.where(df["Fsize"]==1,1,0)
 df['Age_Class']= df['Age']* df['Pclass']
df['Age_cut'] = pd.qcut(df['Age'],5,duplicates='drop')
 df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr','Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Title']= df['Title'].fillna(0)
 df['Fare_Per_Person'] = df['Fare']/(df["Fsize"]+1)
 df['Fare_Per_Person'] = df['Fare_Per_Person'].astype(int)
 df['Fare_cut'] = pd.qcut(df['Fare'],5,duplicates='drop')
 df['Cabin_len'] = df['Cabin'].astype(str ).apply(lambda x : len(x))
df['Cabin'] = df['Cabin'].fillna("M")
 df['Cabin'] = df['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 df['Ticket']=df.Ticket.apply(lambda x : len(x))
df = df.drop(['Name'], axis = 1)
 df.head()
def make_tta_predictions(images, score_threshold=0.25):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def run_wbf(predictions, image_index, image_size=1024, iou_thr=0.44, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
scaler = preprocessing.StandardScaler().fit(df[['Age','Fare']])
 df[['Age','Fare']] = scaler.transform(df[['Age','Fare']] )
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<categorify>
df['Age_cut'] = LabelEncoder().fit_transform(df['Age_cut'])
 df['Fare_cut'] = LabelEncoder().fit_transform(df['Fare_cut'])
 titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
df['Title']= df['Title'].map(titles)
 groups = {"M": 1, "C": 2, "B": 3, "D": 4, "E": 5, "A": 6, "F": 7, "G": 8,'T':9}
df['Cabin'] = df['Cabin'].map(groups)
 ports = {"S": 0, "C": 1, "Q": 2}
df['Embarked'] = df['Embarked'].map(ports)
 df['Sex_female'] = np.where(df['Sex'] == 'female',1,0)
 df['Sex_male'] = np.where(df['Sex'] == 'male',1,0)
 df = df.drop(['Sex'],axis=1 )
results = []
for images, image_ids in data_loader:
predictions = make_tta_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).round().astype(np.int32 ).clip(min=0, max=511)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result )<save_to_csv>
dummies = OneHotEncoder(cols= ['Pclass','Age_cut','Fare_cut','Embarked','Title'],use_cat_names=True)
 dummies.fit(df)
 df = dummies.transform(df )
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<load_pretrained>
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
upper.head()
def load_image(path):
with open(path, "rb")as f:
buf = f.read()
return mx.image.imdecode(buf)
<load_pretrained>
x_valid = df[df['Survived'].isna() ].drop(['Survived'], axis = 1)
 submission = x_valid['PassengerId'].to_frame()
x1 = df[df['Survived']==0]
x2 = df[df['Survived']==1]
x= pd.concat([x1,x2])
 y =x.loc[:,['Survived','PassengerId']]
x =x.drop(['Survived','PassengerId'], axis = 1 )
def load_model(path, ctx=mx.cpu()):
net = gcv.model_zoo.yolo3_darknet53_custom(["wheat"], pretrained_base=False)
 net.set_nms(post_nms=150)
 net.load_parameters(path, ctx=ctx)
 return net 
<load_pretrained>
teste = pd.read_csv(".. /input/testey/full-tita.csv",sep=";")
 teste = teste.drop(['x'],axis=1)
 teste
x_valid = x_valid.merge(teste)
 x_valid = x_valid.drop(['Survived'],axis = 1)
 y_valid = teste['Survived']
folds = 5
threshold = 0.1
img_s = 512
context = mx.gpu()
print("Loading test images...")
 images = [ 
(os.path.join(dirname, filename), os.path.splitext(filename)[0])
 for dirname, _, filenames in os.walk('/kaggle/input/global-wheat-detection/test')for filename in filenames
]
print("Loading models...")
 models = [load_model("/kaggle/input/global-wheat-detection-private/global-wheat-yolo3-darknet53_fold%d.params" % i, ctx=context)for i in range(folds)]
print("Inference...")
 results = []
for path, image_id in images:
print(path)
 raw = load_image(path)
 rh, rw, _ = raw.shape
classes_list = []
scores_list = []
bboxes_list = []
for _ in range(5):
img, flips = gcv.data.transforms.image.random_flip(raw, px=0.5, py=0.5)
 x, _ = gcv.data.transforms.presets.yolo.transform_test(img, short=img_s)
 _, _, xh, xw = x.shape
rot = random.randint(0, 3)
 if rot > 0:
x = np.rot90(x.asnumpy() , k=rot, axes=(2, 3))
for model in models:
classes, scores, bboxes = model(mx.nd.array(x, ctx=context))
if rot > 0:
if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = xh - raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = raw_bboxes[0, :, [2, 0]]
elif rot == 2:
bboxes[0, :, [0, 1, 2, 3]] = mx.nd.array([[xw], [xh], [xw], [xh]], ctx=context)- bboxes[0, :, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = xw - raw_bboxes[0, :, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[0, :, 0] = raw_bboxes[0, :, [0, 2]].min(axis=0)
 bboxes[0, :, 1] = raw_bboxes[0, :, [1, 3]].min(axis=0)
 bboxes[0, :, 2] = raw_bboxes[0, :, [0, 2]].max(axis=0)
 bboxes[0, :, 3] = raw_bboxes[0, :, [1, 3]].max(axis=0)
 bboxes[0, :, :] = gcv.data.transforms.bbox.flip(bboxes[0, :, :],(xw, xh), flip_x=flips[0], flip_y=flips[1])
 bboxes[0, :, 0::2] =(bboxes[0, :, 0::2] /(xw - 1)).clip(0.0, 1.0)
 bboxes[0, :, 1::2] =(bboxes[0, :, 1::2] /(xh - 1)).clip(0.0, 1.0)
 classes_list.append([
int(classes[0, i].asscalar())for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 scores_list.append([
scores[0, i].asscalar() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes_list.append([
bboxes[0, i].asnumpy().tolist() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes, scores, classes = weighted_boxes_fusion(bboxes_list, scores_list, classes_list)
 bboxes[:, 0::2] *= rw - 1
bboxes[:, 1::2] *= rh - 1
bboxes[:, 2:4] -= bboxes[:, 0:2]
results.append({
"image_id": image_id,
"PredictionString": " ".join([
" ".join([str(x)for x in [scores[i]] + [round(x)for x in bboxes[i].tolist() ]])
 for i in range(classes.shape[0])
 if model.classes[int(classes[i])] == "wheat" and scores[i] > threshold
])
 })
 pd.DataFrame(results, columns=['image_id', 'PredictionString'] ).to_csv('submission.csv', index=False)
<install_modules>
kf = KFold(n_splits=5, shuffle=True, random_state=42 )
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<init_hyperparams>
lr = LogisticRegression(class_weight = 'balanced', solver = 'liblinear',penalty="l2")
 lr.fit(x,y['Survived'])
 print(cross_val_score(lr, x, y['Survived'], cv=kf ).mean() )
Round_N = 1
class TrainGlobalConfig:
num_workers = 4
batch_size = 4
n_epochs = 20
lr = 0.0002
folder = 'effdet5-cutmix-augmix'
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08 
)
<set_options>
lr = LogisticRegression(class_weight = 'balanced', solver = 'liblinear',penalty="l1")
 lr.fit(x,y['Survived'])
 print(cross_val_score(lr, x_valid, y_valid, cv=kf ).mean() )
SEED = 42
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(SEED)
 def get_train_transforms() :
return A.Compose(
 [
A.RandomSizedCrop(min_max_height=(800, 800), height=1024, width=1024, p=0.5),
A.OneOf([
A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2,
val_shift_limit=0.2, p=0.9),
A.RandomBrightnessContrast(brightness_limit=0.2,
contrast_limit=0.2, p=0.9),
], p=0.9),
A.ToGray(p=0.01),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Resize(height=512, width=512, p=1),
A.Cutout(num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),
ToTensorV2(p=1.0),
],
p=1.0,
bbox_params=A.BboxParams(
 format='pascal_voc',
min_area=0,
min_visibility=0,
label_fields=['labels'] 
)
)
 def get_valid_transforms() :
return A.Compose(
 [
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
],
p=1.0,
bbox_params=A.BboxParams(
 format='pascal_voc',
min_area=0,
min_visibility=0,
label_fields=['labels'] 
)
)
 class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
warnings.filterwarnings("ignore")
 class Fitter:
def __init__(self, model, device, config):
self.config = config
self.epoch = 0
self.base_dir = f'./{config.folder}'
if not os.path.exists(self.base_dir):
os.makedirs(self.base_dir)
 self.log_path = f'{self.base_dir}/log.txt'
self.best_summary_loss = 10 ** 5
self.model = model
self.device = device
param_optimizer = list(self.model.named_parameters())
 no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
self.optimizer = torch.optim.AdamW(self.model.parameters() , lr=config.lr)
 self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)
 self.log(f'Fitter prepared.Device is {self.device}')
 def fit(self, train_loader, validation_loader):
for e in range(self.config.n_epochs):
if self.config.verbose:
lr = self.optimizer.param_groups[0]['lr']
timestamp = datetime.utcnow().isoformat()
self.log(f'
{timestamp}
LR: {lr}')
 t = time.time()
summary_loss = self.train_one_epoch(train_loader)
 self.log(
 f'[RESULT]: Train.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')
 self.save(f'{self.base_dir}/last-checkpoint.bin')
 t = time.time()
summary_loss = self.validation(validation_loader)
 self.log(
 f'[RESULT]: Val.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')
 if summary_loss.avg < self.best_summary_loss:
self.best_summary_loss = summary_loss.avg
self.model.eval()
print("better model find at epoch:{}".format(self.epoch))
best_weight=self.model.model.state_dict()
self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch ).zfill(3)}epoch.bin')
 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin')) [:-3]:
os.remove(path)
 if self.config.validation_scheduler:
self.scheduler.step(metrics=summary_loss.avg)
 self.epoch += 1
return best_weight
def validation(self, val_loader):
self.model.eval()
summary_loss = AverageMeter()
t = time.time()
for step,(images, targets, image_ids)in enumerate(val_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Val Step {step}/{len(val_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 with torch.no_grad() :
images = torch.stack(images)
 batch_size = images.shape[0]
images = images.to(self.device ).float()
boxes = [target['boxes'].to(self.device ).float() for target in targets]
labels = [target['labels'].to(self.device ).float() for target in targets]
loss, _, _ = self.model(images, boxes, labels)
 summary_loss.update(loss.detach().item() , batch_size)
 return summary_loss
def train_one_epoch(self, train_loader):
self.model.train()
summary_loss = AverageMeter()
t = time.time()
for step,(images, targets, image_ids)in enumerate(train_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Train Step {step}/{len(train_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 images = torch.stack(images)
 images = images.to(self.device ).float()
batch_size = images.shape[0]
boxes = [target['boxes'].to(self.device ).float() for target in targets]
labels = [target['labels'].to(self.device ).float() for target in targets]
self.optimizer.zero_grad()
loss, _, _ = self.model(images, boxes, labels)
 loss.backward()
summary_loss.update(loss.detach().item() , batch_size)
 self.optimizer.step()
if self.config.step_scheduler:
self.scheduler.step()
return summary_loss
def save(self, path):
self.model.eval()
torch.save({
'model_state_dict': self.model.model.state_dict() ,
'optimizer_state_dict': self.optimizer.state_dict() ,
'scheduler_state_dict': self.scheduler.state_dict() ,
'best_summary_loss': self.best_summary_loss,
'epoch': self.epoch,
}, path)
 def load(self, path):
checkpoint = torch.load(path)
 self.model.model.load_state_dict(checkpoint['model_state_dict'])
 self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
 self.best_summary_loss = checkpoint['best_summary_loss']
self.epoch = checkpoint['epoch'] + 1
def log(self, message):
if self.config.verbose:
print(message)
 with open(self.log_path, 'a+')as logger:
logger.write(f'{message}
')
 def collate_fn(batch):
return tuple(zip(*batch))
def run_training(train_dataset,validation_dataset,train_net=None):
device = torch.device('cuda:0')
 train_net.to(device)
 train_loader = torch.utils.data.DataLoader(
 train_dataset,
batch_size=TrainGlobalConfig.batch_size,
sampler=RandomSampler(train_dataset),
pin_memory=False,
drop_last=True,
num_workers=TrainGlobalConfig.num_workers,
collate_fn=collate_fn, 
)
 val_loader = torch.utils.data.DataLoader(
 validation_dataset,
batch_size=TrainGlobalConfig.batch_size,
num_workers=TrainGlobalConfig.num_workers,
shuffle=False,
sampler=SequentialSampler(validation_dataset),
pin_memory=False,
collate_fn=collate_fn, 
)
 fitter = Fitter(model=train_net, device=device, config=TrainGlobalConfig)
 best_weight = fitter.fit(train_loader, val_loader)
 return best_weight<define_variables>
svm = SVC(gamma='auto',random_state=42)
 svm.fit(x,y['Survived'])
 print(cross_val_score(svm, x, y['Survived'], cv=kf ).mean() )
TRAIN_ROOT_PATH = '.. /input/global-wheat-detection/train'
TEST_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, marking, image_ids, transforms=None, test=False):
super().__init__()
self.image_ids = image_ids
self.marking = marking
self.transforms = transforms
self.test = test
def __getitem__(self, index: int):
image_id = self.image_ids[index]
if self.test or random.random() > 0.5:
image, boxes = self.load_image_and_boxes(index)
 else:
image, boxes = self.load_cutmix_image_and_boxes(index)
 labels = torch.ones(( boxes.shape[0],), dtype=torch.int64)
 target = {}
target['boxes'] = boxes
target['labels'] = labels
target['image_id'] = torch.tensor([index])
 if self.transforms:
for i in range(10):
sample = self.transforms(**{
'image': image,
'bboxes': target['boxes'],
'labels': labels
})
 if len(sample['bboxes'])> 0:
image = sample['image']
target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)
 target['boxes'][:, [0, 1, 2, 3]] = target['boxes'][:, [1, 0, 3, 2]]
break
return image, target, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
def load_image_and_boxes(self, index):
image_id = self.image_ids[index]
if self.marking[self.marking['image_id'] == image_id]['source'].all() == 'BULLBULL':
image = cv2.imread(f'{TEST_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 else:
image = cv2.imread(f'{TRAIN_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
records = self.marking[self.marking['image_id'] == image_id]
boxes = records[['x', 'y', 'w', 'h']].values
boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
return image, boxes
def load_cutmix_image_and_boxes(self, index, imsize=1024):

 w, h = imsize, imsize
s = imsize // 2
xc, yc = [int(random.uniform(imsize * 0.25, imsize * 0.75)) for _ in range(2)]
indexes = [index] + [random.randint(0, self.image_ids.shape[0] - 1)for _ in range(3)]
result_image = np.full(( imsize, imsize, 3), 1, dtype=np.float32)
 result_boxes = []
for i, index in enumerate(indexes):
image, boxes = self.load_image_and_boxes(index)
 if i == 0:
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc
x1b, y1b, x2b, y2b = w -(x2a - x1a), h -(y2a - y1a), w, h
elif i == 1:
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h -(y2a - y1a), min(w, x2a - x1a), h
elif i == 2:
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
 x1b, y1b, x2b, y2b = w -(x2a - x1a), 0, max(xc, w), min(y2a - y1a, h)
 elif i == 3:
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
 x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
 result_image[y1a:y2a, x1a:x2a] = image[y1b:y2b, x1b:x2b]
padw = x1a - x1b
padh = y1a - y1b
boxes[:, 0] += padw
boxes[:, 1] += padh
boxes[:, 2] += padw
boxes[:, 3] += padh
result_boxes.append(boxes)
 result_boxes = np.concatenate(result_boxes, 0)
 np.clip(result_boxes[:, 0:], 0, 2 * s, out=result_boxes[:, 0:])
 result_boxes = result_boxes.astype(np.int32)
 result_boxes = result_boxes[
np.where(( result_boxes[:, 2] - result_boxes[:, 0])*(result_boxes[:, 3] - result_boxes[:, 1])> 0)]
return result_image, result_boxes<normalization>
svm = SVC(gamma='auto',random_state=42)
 svm.fit(x,y['Survived'])
 print(cross_val_score(svm, x_valid, y_valid, cv=kf ).mean() )
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever_test(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
dataset = DatasetRetriever_test(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
def get_valnet_file(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
def make_predictions(images=None, score_threshold=0.1, model=None):
images = torch.stack(images ).cuda().float()
predictions = []
with torch.no_grad() :
det = model(images, torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
 return [predictions]
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.55, skip_box_thr=0.45, weights=None):
boxes = [(prediction[image_index]['boxes'] /(image_size - 1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).tolist() for prediction in predictions]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr,
skip_box_thr=skip_box_thr)
 boxes = boxes *(image_size - 1)
 return boxes, scores, labels
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<predict_on_test>
sgd = linear_model.SGDClassifier(max_iter=5, tol=None)
 sgd.fit(x,y['Survived'])
 print(cross_val_score(sgd, x, y['Survived'], cv=kf ).mean() )
def predict_test(data_loader,net):
results = []
testdf_psuedo = []
for images, image_ids in data_loader:
predictions = make_predictions(images=images,model=net)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
for box in boxes:
result = {
'image_id': image_id,
'source': 'BULLBULL',
'width':1024,
'height':1024,
'x': box[0],
'y': box[1],
'w': box[2],
'h': box[3]
}
testdf_psuedo.append(result)
 test_df_pseudo = pd.DataFrame(testdf_psuedo, columns=['image_id', 'width', 'height', 'source', 'x', 'y', 'w', 'h'])
 return test_df_pseudo<load_from_csv>
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(x,y['Survived'])
 print(cross_val_score(knn, x, y['Survived'], cv=kf ).mean() )
def generate_train(test_df_pseudo):
train_df = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(train_df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
train_df[column] = bboxs[:, i]
train_df.drop(columns=['bbox'], inplace=True)
 frames = [train_df, test_df_pseudo]
train_df = pd.concat(frames)
 return train_df<data_type_conversions>
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(x,y['Survived'])
 print(cross_val_score(knn, x_valid, y_valid, cv=kf ).mean() )
def split_train_val(train_df):
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 df_folds = train_df[['image_id']].copy()
df_folds.loc[:, 'bbox_count'] = 1
df_folds = df_folds.groupby('image_id' ).count()
df_folds.loc[:, 'source'] = train_df[['image_id', 'source']].groupby('image_id' ).min() ['source']
df_folds.loc[:, 'stratify_group'] = np.char.add(
 df_folds['source'].values.astype(str),
df_folds['bbox_count'].apply(lambda x: f'_{x // 15}' ).values.astype(str)
)
 df_folds.loc[:, 'fold'] = 0
for fold_number,(train_index, val_index)in enumerate(skf.split(X=df_folds.index, y=df_folds['stratify_group'])) :
df_folds.loc[df_folds.iloc[val_index].index, 'fold'] = fold_number
train_dataset = DatasetRetriever(
 image_ids=df_folds[df_folds['fold'] != fold_number].index.values,
marking=train_df,
transforms=get_train_transforms() ,
test=False, 
)
 validation_dataset = DatasetRetriever(
 image_ids=df_folds[df_folds['fold'] == fold_number].index.values,
marking=train_df,
transforms=get_valid_transforms() ,
test=True, 
)
 return train_dataset,validation_dataset<choose_model_class>
gaussian = GaussianNB()
gaussian.fit(x,y['Survived'])
 print(cross_val_score(gaussian, x, y['Survived'], cv=kf ).mean() )
def get_train_net(eval_net):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
net.load_state_dict(eval_net.model.state_dict())
 gc.collect()
net = DetBenchTrain(net, config)
 net = net.train()
return net.cuda()
def get_test_net(best_weigth):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
net.load_state_dict(best_weigth)
 gc.collect()
net = DetBenchEval(net, config)
 net = net.train()
return net.cuda()<load_pretrained>
rf_with_standardScaler=make_pipeline(StandardScaler() ,RandomForestClassifier())
 rf_with_standardScaler.fit(x,y['Survived'])
 print(cross_val_score(rf_with_standardScaler, x, y['Survived'], cv=kf ).mean() )
test_loader = DataLoader(
 dataset,
batch_size=2,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn 
)
 test_net = get_valnet_file('.. /input/wheat-effdet5-fold0-best-checkpoint/fold0-best-all-states.bin' )<split>
rf_with_standardScaler=make_pipeline(StandardScaler() ,RandomForestClassifier())
 rf_with_standardScaler.fit(x,y['Survived'])
 print(cross_val_score(rf_with_standardScaler, x_valid, y_valid, cv=kf ).mean() )
for i in range(Round_N):
train_net = get_train_net(test_net)
 test_df_pseudo = predict_test(data_loader=test_loader,net=test_net)
 train_df = generate_train(test_df_pseudo)
 train_dataset,validation_dataset = split_train_val(train_df)
 best_weight = run_training(train_dataset=train_dataset,validation_dataset=validation_dataset,train_net=train_net)
 new_test_net = get_test_net(best_weight)
 test_net = new_test_net<predict_on_test>
rf=RandomForestClassifier(n_estimators=100, oob_score = True)
 rf.fit(x,y['Survived'])
 print(cross_val_score(rf, x, y['Survived'], cv=kf ).mean())
 parametros = pd.DataFrame({'feature':x.columns,'Parameters':np.round(rf.feature_importances_,3)})
 parametros = parametros.sort_values('Parameters',ascending=False ).set_index('feature')
 parametros
results = []
for images, image_ids in test_loader:
predictions = make_predictions(images,model=new_test_net)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes * 2 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result)
 test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<install_modules>
rf=RandomForestClassifier(n_estimators=100)
 rf.fit(x,y['Survived'])
 print(cross_val_score(rf, x_valid, y_valid, cv=kf ).mean())
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<categorify>
bagging = BaggingClassifier(bootstrap=True,n_jobs = -1,n_estimators=100)
 bagging.fit(x,y['Survived'])
 print(cross_val_score(bagging, x, y['Survived'], cv=kf ).mean() )
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
bagging = BaggingClassifier(bootstrap=True,n_jobs = -1,n_estimators=100)
 bagging.fit(x,y['Survived'])
 print(cross_val_score(bagging, x_valid, y_valid, cv=kf ).mean() )
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<load_pretrained>
xgboost = xgb.XGBClassifier(objective ='reg:logistic'
, colsample_bytree = 0.7
, learning_rate = 0.01
,max_depth = 6
, n_estimators = 100
,random_state=42
,max_features= 0.8
,min_samples_leaf =0.5
,min_child_weight= 3)
 xgboost.fit(x,y['Survived'])
 print(cross_val_score(xgboost, x, y['Survived'], cv=kf ).mean() )
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=2,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)<choose_model_class>
xgboost = xgb.XGBClassifier(colsample_bytree = 0.8
, learning_rate = 0.05
,max_depth = 6
, n_estimators = 100
,random_state=42 
)
 xgboost.fit(x,y['Survived'])
 print(cross_val_score(xgboost, x_valid, y_valid, cv=kf ).mean() )
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
net = load_net('.. /input/wheat-effdet5-fold0-best-checkpoint/fold0-best-all-states.bin' )<predict_on_test>
lgbm=lgb.LGBMClassifier()
lgbm.fit(x,y['Survived'])
 print(cross_val_score(lgbm, x, y['Survived'], cv=kf ).mean() )
def make_predictions(images, score_threshold=0.22):
images = torch.stack(images ).cuda().float()
predictions = []
with torch.no_grad() :
det = net(images, torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
 return [predictions]
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).tolist() for prediction in predictions]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
lgbm=lgb.LGBMClassifier(colsample_bytree = 0.8
, learning_rate = 0.015
,max_depth = 5
, n_estimators = 100
,random_state=42)
 lgbm.fit(x,y['Survived'])
 print(cross_val_score(lgbm, x_valid, y_valid, cv=kf ).mean() )
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<predict_on_test>
stacking = VotingClassifier(estimators=[ 
('lr',lr),('svm',svm),('rf', rf)], voting='hard')
 stacking.fit(x,y['Survived'])
 print(cross_val_score(stacking, x, y['Survived'], cv=kf ).mean() )
results = []
for images, image_ids in data_loader:
predictions = make_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result )<save_to_csv>
stacking = VotingClassifier(estimators=[ 
('lr',rf),('svm',knn),('xgboost', xgboost)], voting='hard')
 stacking.fit(x,y['Survived'])
 print(cross_val_score(stacking, x_valid, y_valid, cv=kf ).mean() )
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<install_modules>
parameters = {
'max_features': [0.8, 0.9],
'min_samples_leaf' :[0.5,0.9],
'colsample_bytree' :[0.8,0.9],
'learning_rate' : [0.01,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 6)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x,y['Survived'])
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<categorify>
parametros = pd.DataFrame({'feature':x.columns,'Parameters':np.round(xgboost.feature_importances_,3)})
 parametros = parametros.sort_values('Parameters',ascending=False ).set_index('feature')
 drop = list(parametros.tail(30 ).index)
 print(drop)
 x = x.drop(drop,axis=1)
 x_valid = x_valid.drop(drop,axis=1 )
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
parameters = {
'max_features': [0.7,0.8, 0.9],
'min_samples_leaf' :[0.6,0.7,0.9],
'colsample_bytree' :[0.6,0.8,0.9],
'learning_rate' : [0.01,0.03,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 7)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x,y['Survived'])
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<load_pretrained>
parameters = {
'max_features': [0.7,0.8, 0.9],
'min_samples_leaf' :[0.6,0.7,0.9],
'colsample_bytree' :[0.6,0.8,0.9],
'learning_rate' : [0.01,0.03,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 6)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x_valid,y_valid)
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=2,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)<choose_model_class>
svc=make_pipeline(StandardScaler() ,SVC(random_state=1))
r=[0.0001,0.001,0.1,1,10,50,100]
PSVM=[{'svc__C':r, 'svc__kernel':['linear']},
{'svc__C':r, 'svc__gamma':r, 'svc__kernel':['rbf']}]
GSSVM=GridSearchCV(estimator=svc, param_grid=PSVM, scoring='accuracy', cv=2)
 scores_svm=cross_val_score(GSSVM, x_valid, y_valid,scoring='accuracy', cv=5)
 np.mean(scores_svm )
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
net = load_net('.. /input/wheat-effdet5-fold0-best-checkpoint/fold0-best-all-states.bin' )<predict_on_test>
Survived = grid_search2.best_estimator_.predict(x_valid)
 submission['Survived'] = Survived
submission['Survived'] = submission['Survived'].astype(int)
 submission.to_csv('submission.csv' , index=False )
def make_predictions(images, score_threshold=0.22):
images = torch.stack(images ).cuda().float()
predictions = []
with torch.no_grad() :
det = net(images, torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
 return [predictions]
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).tolist() for prediction in predictions]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import xgboost as xgb
import lightgbm as lgb
import re
import shap
from sklearn import preprocessing
from category_encoders.one_hot import OneHotEncoder
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.model_selection import cross_val_score, KFold,GridSearchCV
from sklearn.feature_selection import SelectKBest,SelectFromModel
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline,make_pipeline
from sklearn.ensemble import RandomForestClassifier,VotingClassifier,BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import linear_model
from sklearn.linear_model import LogisticRegression,Perceptron,SGDClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<predict_on_test>
test = pd.read_csv(".. /input/titanic/test.csv")
 test['Survived'] = np.nan
train = pd.read_csv(".. /input/titanic/train.csv")
 df = pd.concat([test,train],sort=False)
 df.head()
results = []
for images, image_ids in data_loader:
predictions = make_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result )<save_to_csv>
print(df.corr() ['Age'].sort_values(ascending=False))
df['Age'] = df.groupby("Pclass")['Age'].transform(lambda x: x.fillna(x.median()))
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<load_from_csv>
print(df.corr() ['Fare'].sort_values(ascending=False))
df['Fare'] = df.groupby("Pclass")['Fare'].transform(lambda x: x.fillna(x.median()))
data = pd.read_csv(".. /input/data.csv")
 print(check_output(["ls", ".. /input"] ).decode("utf8"))<compute_train_metric>
df['Embarked'].fillna(df['Embarked'].mode() [0], inplace = True)
 df["Fsize"] = df["SibSp"] + df["Parch"] + 1
df['IsAlone'] = np.where(df["Fsize"]==1,1,0)
 df['Age_Class']= df['Age']* df['Pclass']
df['Age_cut'] = pd.qcut(df['Age'],5,duplicates='drop')
 df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr','Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Title']= df['Title'].fillna(0)
 df['Fare_Per_Person'] = df['Fare']/(df["Fsize"]+1)
 df['Fare_Per_Person'] = df['Fare_Per_Person'].astype(int)
 df['Fare_cut'] = pd.qcut(df['Fare'],5,duplicates='drop')
 df['Cabin_len'] = df['Cabin'].astype(str ).apply(lambda x : len(x))
df['Cabin'] = df['Cabin'].fillna("M")
 df['Cabin'] = df['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 df['Ticket']=df.Ticket.apply(lambda x : len(x))
df = df.drop(['Name'], axis = 1)
 df.head()
def test_it_GBC(data_test):
clf = GradientBoostingClassifier()
return cross_val_score(clf, data_test.drop('shot_made_flag', 1), data_test.shot_made_flag,
scoring='log_loss', cv=10 )<feature_engineering>
scaler = preprocessing.StandardScaler().fit(df[['Age','Fare']])
 df[['Age','Fare']] = scaler.transform(df[['Age','Fare']] )
def sort_encode(df, field):
ct = pd.crosstab(df.shot_made_flag, df[field] ).apply(lambda x:x/x.sum() , axis=0)
 temp = list(zip(ct.values[1, :], ct.columns))
temp.sort(reverse=True)
 new_map = {}
for index,(acc, old_number)in enumerate(temp):
new_map[old_number] = index
new_field = field + '_sort_enumerated'
df[new_field] = df[field].map(new_map)
 return new_field<sort_values>
df['Age_cut'] = LabelEncoder().fit_transform(df['Age_cut'])
 df['Fare_cut'] = LabelEncoder().fit_transform(df['Fare_cut'])
 titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
df['Title']= df['Title'].map(titles)
 groups = {"M": 1, "C": 2, "B": 3, "D": 4, "E": 5, "A": 6, "F": 7, "G": 8,'T':9}
df['Cabin'] = df['Cabin'].map(groups)
 ports = {"S": 0, "C": 1, "Q": 2}
df['Embarked'] = df['Embarked'].map(ports)
 df['Sex_female'] = np.where(df['Sex'] == 'female',1,0)
 df['Sex_male'] = np.where(df['Sex'] == 'male',1,0)
 df = df.drop(['Sex'],axis=1 )
sort_encode(data, 'action_type' )<sort_values>
dummies = OneHotEncoder(cols= ['Pclass','Age_cut','Fare_cut','Embarked','Title'],use_cat_names=True)
 dummies.fit(df)
 df = dummies.transform(df )
sort_encode(data, 'shot_zone_area' )<feature_engineering>
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
upper.head()
data['time'] = data['minutes_remaining']*60 + data['seconds_remaining']
data['time_new'] = 1000
data.loc[data.time>5, ['time_new']] = 0
data.loc[data.time<=5, ['time_new']] = 1<feature_engineering>
x_valid = df[df['Survived'].isna() ].drop(['Survived'], axis = 1)
 submission = x_valid['PassengerId'].to_frame()
x1 = df[df['Survived']==0]
x2 = df[df['Survived']==1]
x= pd.concat([x1,x2])
 y =x.loc[:,['Survived','PassengerId']]
x =x.drop(['Survived','PassengerId'], axis = 1 )
data['away'] = data.matchup.str.contains('@' )<filter>
teste = pd.read_csv(".. /input/testey/full-tita.csv",sep=";")
 teste = teste.drop(['x'],axis=1)
 teste
x_valid = x_valid.merge(teste)
 x_valid = x_valid.drop(['Survived'],axis = 1)
 y_valid = teste['Survived']
data_test = data.loc[~data.shot_made_flag.isnull() ,\
['action_type_sort_enumerated','shot_distance','time_new',\
'away','shot_made_flag']]<prepare_x_and_y>
kf = KFold(n_splits=5, shuffle=True, random_state=42 )
train = data.loc[~data.shot_made_flag.isnull() , ['action_type_sort_enumerated', \
'shot_distance', 'time_new', 'away', 'shot_made_flag']]
test = data.loc[data.shot_made_flag.isnull() , ['action_type_sort_enumerated', \
'shot_distance', 'time_new', 'away', 'shot_id']]<save_to_csv>
lr = LogisticRegression(class_weight = 'balanced', solver = 'liblinear',penalty="l2")
 lr.fit(x,y['Survived'])
 print(cross_val_score(lr, x, y['Survived'], cv=kf ).mean() )
clf = GradientBoostingClassifier()
clf.fit(train.drop('shot_made_flag',axis=1), train.shot_made_flag)
 predictions = clf.predict_proba(test.drop('shot_id', axis=1))
submission = pd.DataFrame({'shot_id': test.shot_id, 'shot_made_flag': predictions[:, 1]})
 submission[['shot_id', 'shot_made_flag']].to_csv('submission_GBC.csv', index=False )<set_options>
lr = LogisticRegression(class_weight = 'balanced', solver = 'liblinear',penalty="l1")
 lr.fit(x,y['Survived'])
 print(cross_val_score(lr, x_valid, y_valid, cv=kf ).mean() )
%matplotlib inline 
<load_from_csv>
svm = SVC(gamma='auto',random_state=42)
 svm.fit(x,y['Survived'])
 print(cross_val_score(svm, x, y['Survived'], cv=kf ).mean() )
print(check_output(["ls", ".. /input"] ).decode("utf8"))
df = pd.read_csv('.. /input/data.csv' )<find_best_params>
svm = SVC(gamma='auto',random_state=42)
 svm.fit(x,y['Survived'])
 print(cross_val_score(svm, x_valid, y_valid, cv=kf ).mean() )
def test_it(data):
clf = RandomForestClassifier(n_jobs=-1)
 return -cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='log_loss', cv=10 
)
 def best_r() :
score = []
ns = range(4,30,2)
 for n in ns:
x = df['loc_x'].values
y = df['loc_y'].values
r = np.sqrt(x**2 +y**2)/ n
r = r.astype(int)
 df['r'] = r
data = df[['r','shot_made_flag']]
data = data.dropna()
score.append(test_it(data ).mean())
 plt.figure(figsize=(7, 5))
plt.plot(ns, score, 'go')
 plt.xlabel('denominator')
 plt.ylabel('result')
 plt.show()
best_r()<feature_engineering>
sgd = linear_model.SGDClassifier(max_iter=5, tol=None)
 sgd.fit(x,y['Survived'])
 print(cross_val_score(sgd, x, y['Survived'], cv=kf ).mean() )
data = df[['shot_distance', 'shot_made_flag']]
df['shot_distance'] = df.apply(lambda row: 28 if row['shot_distance'] > 28 else row['shot_distance'], axis=1)
 data = data.dropna()
def test_it(data):
clf = RandomForestClassifier(n_jobs=-1)
 return -cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='log_loss', cv=10 
)
 test_it(data ).mean()<data_type_conversions>
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(x,y['Survived'])
 print(cross_val_score(knn, x, y['Survived'], cv=kf ).mean() )
x = df['loc_x'].values
y = df['loc_y'].values
r = np.sqrt(x**2 +y**2)/ 20.0
r = r.astype(int)
 angle = 5*np.abs(np.arctan(x/y))
angle = angle.astype(int)
 df['r'] = r
df['angle'] = angle
get_acc(df, 'r' )<categorify>
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(x,y['Survived'])
 print(cross_val_score(knn, x_valid, y_valid, cv=kf ).mean() )
data = df[['loc_y', 'shot_made_flag']]
data = data.dropna()
test_it(data ).mean()<categorify>
gaussian = GaussianNB()
gaussian.fit(x,y['Survived'])
 print(cross_val_score(gaussian, x, y['Survived'], cv=kf ).mean() )
data = df[['shot_distance', 'shot_made_flag']]
data = data.dropna()
test_it(data ).mean()<data_type_conversions>
rf_with_standardScaler=make_pipeline(StandardScaler() ,RandomForestClassifier())
 rf_with_standardScaler.fit(x,y['Survived'])
 print(cross_val_score(rf_with_standardScaler, x, y['Survived'], cv=kf ).mean() )
print(df.season.unique())
 df['season_start_year'] = df.season.str.split('-' ).str[0]
df['season_start_year'] = df['season_start_year'].astype(int)
 get_acc(df, 'season_start_year' )<categorify>
rf_with_standardScaler=make_pipeline(StandardScaler() ,RandomForestClassifier())
 rf_with_standardScaler.fit(x,y['Survived'])
 print(cross_val_score(rf_with_standardScaler, x_valid, y_valid, cv=kf ).mean() )
action_map = {action: i for i, action in enumerate(df.action_type.unique())}
df['action_type_enumerated'] = df.action_type.map(action_map)
 get_acc(df, 'action_type_enumerated' )<categorify>
rf=RandomForestClassifier(n_estimators=100, oob_score = True)
 rf.fit(x,y['Survived'])
 print(cross_val_score(rf, x, y['Survived'], cv=kf ).mean())
 parametros = pd.DataFrame({'feature':x.columns,'Parameters':np.round(rf.feature_importances_,3)})
 parametros = parametros.sort_values('Parameters',ascending=False ).set_index('feature')
 parametros
def sort_encode(df, field):
ct = pd.crosstab(df.shot_made_flag, df[field] ).apply(lambda x:x/x.sum() , axis=0)
 temp = list(zip(ct.values[1, :], ct.columns))
temp.sort()
new_map = {}
for index,(acc, old_number)in enumerate(temp):
new_map[old_number] = index
new_field = field + '_sort_enumerated'
df[new_field] = df[field].map(new_map)
 get_acc(df, new_field)
 sort_encode(df, 'action_type_enumerated' )<prepare_x_and_y>
rf=RandomForestClassifier(n_estimators=100)
 rf.fit(x,y['Survived'])
 print(cross_val_score(rf, x_valid, y_valid, cv=kf ).mean())
data = df[['action_type_enumerated', 'shot_distance', 'shot_made_flag']].dropna()
x = test_it(data)
 data = df[['action_type_enumerated_sort_enumerated', 'shot_distance', 'shot_made_flag']].dropna()
y = test_it(data)
 print(x.mean() , y.mean() )<categorify>
bagging = BaggingClassifier(bootstrap=True,n_jobs = -1,n_estimators=100)
 bagging.fit(x,y['Survived'])
 print(cross_val_score(bagging, x, y['Survived'], cv=kf ).mean() )
opponent_map = {opp: i for i, opp in enumerate(df.opponent.unique())}
df['opponent_enumerated'] = df.opponent.map(opponent_map)
 sort_encode(df, 'opponent_enumerated' )<feature_engineering>
bagging = BaggingClassifier(bootstrap=True,n_jobs = -1,n_estimators=100)
 bagging.fit(x,y['Survived'])
 print(cross_val_score(bagging, x_valid, y_valid, cv=kf ).mean() )
df['away'] = df.matchup.str.contains('@')
 data = df[['action_type_enumerated', 'shot_distance',
'shot_made_flag', 'away']].dropna()
test_it(data ).mean()<find_best_model_class>
xgboost = xgb.XGBClassifier(objective ='reg:logistic'
, colsample_bytree = 0.7
, learning_rate = 0.01
,max_depth = 6
, n_estimators = 100
,random_state=42
,max_features= 0.8
,min_samples_leaf =0.5
,min_child_weight= 3)
 xgboost.fit(x,y['Survived'])
 print(cross_val_score(xgboost, x, y['Survived'], cv=kf ).mean() )
data = df[['action_type_enumerated', 'shot_distance',
'shot_made_flag', 'away']].dropna()
estimators, scores = list(range(1, 100, 5)) , []
for i in estimators:
clf = RandomForestClassifier(n_jobs=-1, n_estimators=i, random_state=2016)
 x = cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='roc_auc', cv=10)
 scores.append(x)
 x = [i for i in estimators for j in range(10)]
sns.boxplot(x, np.array(scores ).flatten() )<choose_model_class>
xgboost = xgb.XGBClassifier(colsample_bytree = 0.8
, learning_rate = 0.05
,max_depth = 6
, n_estimators = 100
,random_state=42 
)
 xgboost.fit(x,y['Survived'])
 print(cross_val_score(xgboost, x_valid, y_valid, cv=kf ).mean() )
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 train = df.loc[~df.shot_made_flag.isnull() , ['action_type_enumerated_sort_enumerated',
'shot_distance', 'shot_made_flag', 'away']]
test = df.loc[df.shot_made_flag.isnull() , ['action_type_enumerated_sort_enumerated',
'shot_distance', 'shot_id', 'away']]
mode = test.action_type_enumerated_sort_enumerated.mode() [0]
test.action_type_enumerated_sort_enumerated.fillna(mode, inplace=True)
 clf.fit(( train.drop('shot_made_flag', 1)) [:21000], train.shot_made_flag[:21000])
 predictions_train = clf.predict_proba(train.drop('shot_made_flag', 1))
predictions_test = clf.predict_proba(test.drop('shot_id', 1))<save_to_csv>
lgbm=lgb.LGBMClassifier()
lgbm.fit(x,y['Survived'])
 print(cross_val_score(lgbm, x, y['Survived'], cv=kf ).mean() )
features_train = pd.DataFrame({'shot_made_flag': predictions_train[:, 1]})
 features_test = pd.DataFrame({'shot_id': test.shot_id,
'shot_made_flag': predictions_test[:, 1]})
 features_train[['shot_made_flag']].to_csv('features_train.csv', index=False)
 features_test[['shot_id','shot_made_flag']].to_csv('features_test.csv', index=False )<load_from_csv>
lgbm=lgb.LGBMClassifier(colsample_bytree = 0.8
, learning_rate = 0.015
,max_depth = 5
, n_estimators = 100
,random_state=42)
 lgbm.fit(x,y['Survived'])
 print(cross_val_score(lgbm, x_valid, y_valid, cv=kf ).mean() )
%matplotlib inline
warnings.filterwarnings('ignore')
 train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 PassengerId=test['PassengerId']
all_data = pd.concat([train, test], ignore_index = True )<feature_engineering>
stacking = VotingClassifier(estimators=[ 
('lr',lr),('svm',svm),('rf', rf)], voting='hard')
 stacking.fit(x,y['Survived'])
 print(cross_val_score(stacking, x, y['Survived'], cv=kf ).mean() )
all_data['Title'] = all_data['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())
 Title_Dict = {}
Title_Dict.update(dict.fromkeys(['Capt', 'Col', 'Major', 'Dr', 'Rev'], 'Officer'))
Title_Dict.update(dict.fromkeys(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty'))
Title_Dict.update(dict.fromkeys(['Mme', 'Ms', 'Mrs'], 'Mrs'))
Title_Dict.update(dict.fromkeys(['Mlle', 'Miss'], 'Miss'))
Title_Dict.update(dict.fromkeys(['Mr'], 'Mr'))
Title_Dict.update(dict.fromkeys(['Master','Jonkheer'], 'Master'))
all_data['Title'] = all_data['Title'].map(Title_Dict)
 sns.barplot(x='Title', y='Survived', data=all_data )<filter>
stacking = VotingClassifier(estimators=[ 
('lr',rf),('svm',knn),('xgboost', xgboost)], voting='hard')
 stacking.fit(x,y['Survived'])
 print(cross_val_score(stacking, x_valid, y_valid, cv=kf ).mean() )
all_data[all_data['Embarked'].isnull() ]<filter>
parameters = {
'max_features': [0.8, 0.9],
'min_samples_leaf' :[0.5,0.9],
'colsample_bytree' :[0.8,0.9],
'learning_rate' : [0.01,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 6)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x,y['Survived'])
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
all_data[all_data['Fare'].isnull() ]<data_type_conversions>
parametros = pd.DataFrame({'feature':x.columns,'Parameters':np.round(xgboost.feature_importances_,3)})
 parametros = parametros.sort_values('Parameters',ascending=False ).set_index('feature')
 drop = list(parametros.tail(30 ).index)
 print(drop)
 x = x.drop(drop,axis=1)
 x_valid = x_valid.drop(drop,axis=1 )
def info_fill(all_data):
all_data['Embarked'] = all_data['Embarked'].fillna('C')
 fare=all_data[(all_data['Embarked'] == "S")&(all_data['Pclass'] == 3)].Fare.median()
all_data['Fare']=all_data['Fare'].fillna(fare)
 age_df = all_data[['Age', 'Pclass', 'Sex', 'Title', 'SibSp', 'Parch']]
age_df=pd.get_dummies(age_df)
 known_age = age_df[age_df.Age.notnull() ].values
unknown_age = age_df[age_df.Age.isnull() ].values
y = known_age[:, 0]
X = known_age[:, 1:]
rfr = RandomForestRegressor(random_state=0, n_estimators=100, n_jobs=-1)
 rfr.fit(X, y)
 predictedAges = rfr.predict(unknown_age[:, 1::])
 all_data.loc[(all_data.Age.isnull()), 'Age' ] = predictedAges
return all_data<categorify>
parameters = {
'max_features': [0.7,0.8, 0.9],
'min_samples_leaf' :[0.6,0.7,0.9],
'colsample_bytree' :[0.6,0.8,0.9],
'learning_rate' : [0.01,0.03,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 7)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x,y['Survived'])
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
def TrainPreprocess(train):
cols=['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Title', 'Fare', 'Embarked']
train = train[cols]
train_linear = train[['Age', 'Fare']]
train['Sex'] = train['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 train['Embarked'] = train['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train['Title'] = train['Title'].map({'Officer': 0, 'Royalty': 1, 'Mrs': 2, 'Miss': 3,
'Mr': 4, 'Master':5} ).astype(int)
 train_non_linear = train[['Pclass', 'Sex', 'SibSp', 'Parch', 'Title', 'Embarked']]
train_non_linear = pd.get_dummies(train_non_linear)
 features = pd.concat([train_linear, train_non_linear], axis=1)
 features = features.values
mean = features.mean(axis=0)
 features -= mean
std = features.std(axis=0)
 features /= std
label = train['Survived'].values
return features, label<categorify>
parameters = {
'max_features': [0.7,0.8, 0.9],
'min_samples_leaf' :[0.6,0.7,0.9],
'colsample_bytree' :[0.6,0.8,0.9],
'learning_rate' : [0.01,0.03,0.1],
'min_child_weight': [3,5,7],
}
model2 = xgb.XGBClassifier(objective ='reg:logistic' ,random_state=42,n_estimators=100,max_depth = 6)
 grid_search2 = GridSearchCV(model2, parameters, cv=5,n_jobs=-1)
 grid_search2.fit(x_valid,y_valid)
 print(grid_search2.best_params_)
 print(grid_search2.best_score_ )
def TestPreprocess(test):
cols=['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Title', 'Fare', 'Embarked']
test = test[cols]
test_linear = test[['Age', 'Fare']]
test['Sex'] = test['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 test['Embarked'] = test['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test['Title'] = test['Title'].map({'Officer': 0, 'Royalty': 1, 'Mrs': 2, 'Miss': 3,
'Mr': 4, 'Master':5} ).astype(int)
 test_non_linear = test[['Pclass', 'Sex', 'SibSp', 'Parch', 'Title', 'Embarked']]
test_non_linear = pd.get_dummies(test_non_linear)
 test = pd.concat([test_linear, test_non_linear], axis=1)
 features = test.values
mean = features.mean(axis=0)
 features -= mean
std = features.std(axis=0)
 features /= std
return features<define_variables>
svc=make_pipeline(StandardScaler() ,SVC(random_state=1))
r=[0.0001,0.001,0.1,1,10,50,100]
PSVM=[{'svc__C':r, 'svc__kernel':['linear']},
{'svc__C':r, 'svc__gamma':r, 'svc__kernel':['rbf']}]
GSSVM=GridSearchCV(estimator=svc, param_grid=PSVM, scoring='accuracy', cv=2)
 scores_svm=cross_val_score(GSSVM, x_valid, y_valid,scoring='accuracy', cv=5)
 np.mean(scores_svm )
batchsz = 20
_lambda = 0.1<choose_model_class>
Survived = grid_search2.best_estimator_.predict(x_valid)
 submission['Survived'] = Survived
submission['Survived'] = submission['Survived'].astype(int)
 submission.to_csv('submission.csv' , index=False )