Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<save_to_csv>
t_start = time.time()
result=pd.DataFrame({'ForecastId':submission.ForecastId, 'ConfirmedCases':np.round(y_pred_cc_1), 'Fatalities':np.round(y_pred_ft_1)})
 result.to_csv('/kaggle/working/submission.csv', index=False)
 data=pd.read_csv('/kaggle/working/submission.csv')
 data.head(50 )<set_options>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
warnings.filterwarnings("ignore" )<load_from_csv>
from fastai.vision import *
from fastai.metrics import error_rate
from fastai.callbacks import *
train=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<set_options>
from functools import partial
from sklearn import metrics
from collections import Counter
%matplotlib inline<prepare_x_and_y>
import os
import PIL
import cv2
X_train=train[['Id']]
test['Id']=test['ForecastId']
X_test=test[['Id']]
y_train_cc=train[['ConfirmedCases']]
y_train_ft=train[['Fatalities']]<split>
dat_path = Path('.. /input/aptos2019-blindness-detection')
 trn_path = Path('.. /input/aptos2019-blindness-detection/train_images')
(dat_path,trn_path )
X_tr=np.array_split(X_train,313)
 y_cc=np.array_split(y_train_cc,313)
 y_ft=np.array_split(y_train_ft,313)
 X_te=np.array_split(X_test,313 )<define_variables>
train_df = pd.read_csv(dat_path/'train.csv')
 train_df.shape
a=np.max(X_tr[0] ).values
b=a-71
b=b[0]<feature_engineering>
IMG_SIZE = 512
def _load_format(path, convert_mode, after_open)->Image:
image = cv2.imread(path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = crop_image_from_gray(image)
 image = cv2.resize(image,(IMG_SIZE, IMG_SIZE))
image=cv2.addWeighted(image,4, cv2.GaussianBlur(image ,(0,0), 10),-4 ,128)
 return Image(pil2tensor(image, np.float32 ).div_(255))
vision.data.open_image = _load_format
X_te[0]=X_te[0]+a
for i in range(312):
X_te[i+1]=X_te[0]<define_variables>
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=0.10, max_zoom=1.3, max_warp=0.0, max_lighting=0.2 )
for i in range(312):
X_tr[i+1]=X_tr[0]<predict_on_test>
bs = 64
poly=PolynomialFeatures(2)
 y_pred_cc=[]
for i in range(313):
X_tr[i]=poly.fit_transform(X_tr[i])
 X_te[i]=poly.fit_transform(X_te[i])
 model=Lasso()
model.fit(X_tr[i],y_cc[i]);
y_pr_cc=model.predict(X_te[i])
 y_cc[i]= y_cc[i][71:]
y_pr_cc=y_pr_cc[b:]
y_pr_cc=np.append(y_cc[i], y_pr_cc)
 y_pred_cc.append(y_pr_cc); 
<train_model>
data = ImageDataBunch.from_df(path=dat_path, folder='train_images', df=train_df, suffix='.png',
fn_col=0, label_col=1,valid_pct=0.2, size=256, ds_tfms=tfms ).normalize(imagenet_stats )
y_pred_ft=[]
for i in range(313):
model=Lasso()
model.fit(X_tr[i],y_ft[i]);
y_pr_ft=model.predict(X_te[i])
 y_ft[i]= y_ft[i][71:]
y_pr_ft=y_pr_ft[b:]
y_pr_ft=np.append(y_ft[i], y_pr_ft)
 y_pred_ft.append(y_pr_ft);<define_variables>
kappa = KappaScore()
kappa.weights = "quadratic"
y_pred_ft_1 = [item for sublist in y_pred_ft for item in sublist]
y_pred_cc_1 = [item for sublist in y_pred_cc for item in sublist]<save_to_csv>
learn = cnn_learner(data, models.resnet50, metrics=[error_rate, kappa] )
result=pd.DataFrame({'ForecastId':submission.ForecastId, 'ConfirmedCases':np.round(y_pred_cc_1), 'Fatalities':np.round(y_pred_ft_1)})
 result.to_csv('/kaggle/working/submission.csv', index=False)
 data=pd.read_csv('/kaggle/working/submission.csv')
 data.head(50 )<load_from_csv>
learn.fit_one_cycle(1 )
train_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission_csv = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<data_type_conversions>
learn.fit_one_cycle(1)
convert_dict = {'Province_State': str,'Country_Region':str,'ConfirmedCases':int,'Fatalities':int}
convert_dict_test = {'Province_State': str,'Country_Region':str}
train_data = train_data.astype(convert_dict)
 test_data = test_data.astype(convert_dict_test )<data_type_conversions>
learn.unfreeze()
learn.fit_one_cycle(9, max_lr=slice(1e-4,1e-3))
train_data['Date'] = pd.to_datetime(train_data['Date'], infer_datetime_format=True)
 test_data['Date'] = pd.to_datetime(test_data['Date'], infer_datetime_format=True )<data_type_conversions>
learn.model_dir = Path('/kaggle/working/' )
train_data.loc[:, 'Date'] = train_data.Date.dt.strftime('%m%d')
 train_data.loc[:, 'Date'] = train_data['Date'].astype(int)
 test_data.loc[:, 'Date'] = test_data.Date.dt.strftime('%m%d')
 test_data.loc[:, 'Date'] = test_data['Date'].astype(int )<feature_engineering>
learn.save('/kaggle/working/FastAI_APTOS_epoch_11' )
train_data['Country_Region'] = np.where(train_data['Province_State'] == 'nan',train_data['Country_Region'],train_data['Province_State']+' '+train_data['Country_Region'])
 test_data['Country_Region'] = np.where(test_data['Province_State'] == 'nan',test_data['Country_Region'],test_data['Province_State']+' '+test_data['Country_Region'])
<drop_column>
test_df = pd.read_csv(dat_path/'sample_submission.csv' )
train_data = train_data.drop(columns=['Province_State'])
 test_data = test_data.drop(columns=['Province_State'] )<define_variables>
learn.data.add_test(ImageList.from_df(test_df,dat_path,folder='test_images',suffix='.png'))
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index )<import_modules>
preds,y = learn.get_preds(DatasetType.Test )
from sklearn.preprocessing import LabelEncoder<categorify>
test_df.diagnosis = preds.argmax(1)
 test_df.head()
<define_variables><EOS> 
test_df.to_csv('submission.csv',index=False )