Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<import_modules>
from fastai import *
from fastai.vision import *
import numpy as np
import scipy as sp
from sklearn import metrics
import cv2
import PIL
import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import mean_squared_error 
<normalization>
path = Path('/kaggle/input/aptos2019-blindness-detection' )
df_prescaled = df.copy()
df_scaled = df.drop(['fare_amount'], axis=1)
 df_scaled = scale(df_scaled)
 cols = df.columns.tolist()
cols.remove('fare_amount')
 df_scaled = pd.DataFrame(df_scaled, columns=cols, index=df.index)
 df_scaled = pd.concat([df_scaled, df['fare_amount']], axis=1)
 df = df_scaled.copy()<split>
path.ls()
X = df.loc[:, df.columns != 'fare_amount']
y = df.fare_amount
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2 )<choose_model_class>
df = pd.read_csv(path/'train.csv' )
model=Sequential()
model.add(Dense(128, activation= 'relu', input_dim=X_train.shape[1]))
model.add(Dense(64, activation= 'relu'))
model.add(Dense(32, activation= 'relu'))
model.add(Dense(8, activation= 'relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
 model.fit(X_train, y_train, epochs=16)
 train_pred = model.predict(X_train)
 train_rmse = np.sqrt(mean_squared_error(y_train, train_pred))
test_pred = model.predict(X_test)
 test_rmse = np.sqrt(mean_squared_error(y_test, test_pred))
print("Train RMSE: {:0.2f}".format(train_rmse))
print("Test RMSE: {:0.2f}".format(test_rmse))
print('------------------------' )<normalization>
df.diagnosis.value_counts()
test_scaled = scale(test )<predict_on_test>
tfms = get_transforms(do_flip=True, flip_vert=True,max_warp=0., xtra_tfms =[crop_pad() ,symmetric_warp() ] )
pred= model.predict(test_scaled)
 pred.shape<save_to_csv>
data =(
 src.transform(tfms,size=128)
.databunch() 
.normalize(imagenet_stats)
 )
submission = pd.read_csv('/kaggle/input/new-york-city-taxi-fare-prediction/sample_submission.csv')
 submission['fare_amount'] = pred
submission.to_csv('submission.csv',index=False )<import_modules>
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0')
import pandas as pd<import_modules>
learn = cnn_learner(data,models.resnet152,metrics=[quadratic_kappa],model_dir='/kaggle',pretrained=True )
import pandas as pd<predict_on_test>
learn.lr_find()
melbourne_model = DecisionTreeRegressor(random_state=1)
 melbourne_model.fit(X,y)
 print("Making predictions for the following 5 houses:")
 print(X.head())
 print("The predictions are")
 print(melbourne_model.predict(X.head()))<find_best_params>
lr=1e-2
learn.fit_one_cycle(3,lr )
yy = iowa_data.SalePrice
iowa_features = ["LotArea", "YearBuilt", "1stFlrSF", "2ndFlrSF","FullBath", "BedroomAbvGr", "TotRmsAbvGrd"]
XX = iowa_data[iowa_features]
print(XX.describe())
 print(XX.head())
 iowa_model = DecisionTreeRegressor(random_state=1)
 iowa_model.fit(XX,yy)
 print("Making predictions for the following 5 houses:")
 print(XX.head())
 print("The predictions are")
 print(iowa_model.predict(XX.head()))<predict_on_test>
learn.data = data =(src.transform(tfms,size=256 ).databunch().normalize(imagenet_stats))
predicted_melbourne_prices = melbourne_model.predict(X)
 print(mean_absolute_error(y, predicted_melbourne_prices))<split>
learn.fit_one_cycle(5,max_lr=slice(1e-4))
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)
 melbourne_model = DecisionTreeRegressor()
melbourne_model.fit(train_X, train_y)
 val_predictions = melbourne_model.predict(val_X)
 print(mean_absolute_error(val_y, val_predictions))<split>
learn.lr_find()
train_X, val_X, train_y, val_y = train_test_split(XX, yy, random_state = 0)
 iowa_model = DecisionTreeRegressor()
iowa_model.fit(train_X, train_y)
 val_predictions = iowa_model.predict(val_X)
 print(mean_absolute_error(val_y, val_predictions))<load_from_csv>
learn.fit_one_cycle(5,max_lr=9e-04 )
def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
 model.fit(train_X, train_y)
 preds_val = model.predict(val_X)
 mae = mean_absolute_error(val_y, preds_val)
 return mae
melbourne_file_path = '.. /input/melbourne-housing-snapshot/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path)
 filtered_melbourne_data = melbourne_data.dropna(axis=0)
 y = filtered_melbourne_data.Price
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'BuildingArea', 'YearBuilt', 'Lattitude', 'Longtitude']
X = filtered_melbourne_data[melbourne_features]
train_X, val_X, train_y, val_y = train_test_split(X, y,random_state = 0 )<compute_test_metric>
valid_preds = learn.get_preds(ds_type = DatasetType.Valid )
for max_leaf_nodes in [5, 50, 500, 5000]:
my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
 print("Max leaf nodes: %d \t\t Mean Absolute Error: %d" %(max_leaf_nodes, my_mae))<train_model>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
melbourne_forest_model = RandomForestRegressor(random_state=1)
 melbourne_forest_model.fit(train_X, train_y)
 melbourne_forest_preds = melbourne_forest_model.predict(val_X)
 print(mean_absolute_error(val_y, melbourne_forest_preds))<train_model>
optR = OptimizedRounder()
optR.fit(valid_preds[0],valid_preds[1] )
iowa_file_path = '.. /input/house-prices-advanced-regression-techniques/train.csv'
home_data = pd.read_csv(iowa_file_path)
 y = home_data.SalePrice
features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd', 'OverallQual', 'OverallCond']
X = home_data[features]
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
 iowa_model = DecisionTreeRegressor(random_state=1)
 iowa_model.fit(train_X, train_y)
 val_predictions = iowa_model.predict(val_X)
 val_mae = mean_absolute_error(val_predictions, val_y)
 print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
 iowa_model.fit(train_X, train_y)
 val_predictions = iowa_model.predict(val_X)
 val_mae = mean_absolute_error(val_predictions, val_y)
 print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
rf_model = RandomForestRegressor(random_state=1)
 rf_model.fit(train_X, train_y)
 rf_val_predictions = rf_model.predict(val_X)
 rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)
 print("Validation MAE for Random Forest Model: {:,.0f}".format(rf_val_mae))
rf_model_on_full_data = RandomForestRegressor(random_state=1)
 rf_model_on_full_data.fit(X,y)
 test_data_path = '.. /input/house-prices-advanced-regression-techniques/test.csv'
test_data = pd.read_csv(test_data_path)
 test_X = test_data[features]
test_preds = rf_model_on_full_data.predict(test_X)
 output = pd.DataFrame({'Id': test_data.Id,'SalePrice': test_preds})
 output.to_csv('submission.csv', index=False )<import_modules>
sample_df = pd.read_csv(path/'sample_submission.csv' )
import numpy as np
import pandas as pd
import os
import math
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor<define_variables>
learn.data.add_test(ImageList.from_df(sample_df,path,folder='test_images',suffix='.png'))
train_path = '.. /input/new-york-city-taxi-fare-prediction/train.csv'
test_path = '.. /input/new-york-city-taxi-fare-prediction/test.csv'<load_from_csv>
preds,y = learn.get_preds(DatasetType.Test )
train_data = pd.read_csv(train_path, nrows=5000000)
 train_data.head()<data_type_conversions>
test_predictions = optR.predict(preds,coefficients )
train_data['pickup_datetime'] = pd.to_datetime(train_data['pickup_datetime'],format='%Y-%m-%d %H:%M:%S %Z')
 train_data.head()<feature_engineering>
sample_df.diagnosis = test_predictions.astype(int)
 sample_df.head()
train_data['pickup_year'] = train_data['pickup_datetime'].dt.year
train_data['pickup_quarter'] = train_data['pickup_datetime'].dt.quarter
train_data['pickup_month'] = train_data['pickup_datetime'].dt.month
train_data['pickup_day'] = train_data['pickup_datetime'].dt.day
train_data['pickup_hour'] = train_data['pickup_datetime'].dt.hour
train_data.head()<drop_column>
sample_df.to_csv('submission.csv',index = False )