Kaggle competition: New York City Taxi Fare Prediction
def inference(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))
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
return bboxes, scores, classes 
<set_options>
df_train=pd.read_csv(".. /input/train.csv", sep=',', lineterminator='
',infer_datetime_format=True,nrows=50000000,
chunksize=1000000)
 df_test=pd.read_csv(".. /input/test.csv", sep=',', lineterminator='
')
 df_test.rename(columns={'passenger_count\r':'passenger_count'}, inplace=True)
%matplotlib inline
sns.set() 
<init_hyperparams>
def getDistance(lat1,lon1,lat2,lon2):
R = 6373.0
dlon = lon2 - lon1
dlat = lat2 - lat1
a =(np.sin(dlat/2)) **2 + np.cos(lat1)* np.cos(lat2)*(np.sin(dlon/2)) **2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
distance = R * c
return distance
def clean_df(df):
return df[(df.fare_amount > 0)& 
(df.pickup_longitude > -80)&(df.pickup_longitude < -70)& 
(df.pickup_latitude > 35)&(df.pickup_latitude < 45)& 
(df.dropoff_longitude > -80)&(df.dropoff_longitude < -70)& 
(df.dropoff_latitude > 35)&(df.dropoff_latitude < 45)& 
(df.passenger_count > 0)&(df.passenger_count < 10)]
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, agnostic=True)
 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
if __name__ == '__main__':
class opt:
weights = "/kaggle/input/yolo-new-ss/best_yolov5SS149_wheat.pt"
img_size = 1024
conf_thres = 0.3
iou_thres = 0.5
augment = 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>
df_test=pd.read_csv(".. /input/test.csv", sep=',', lineterminator='
')
 df_test.rename(columns={'passenger_count\r':'passenger_count'}, inplace=True)
 df_test_2=df_test
df_test_2["distance"]=getDistance(df_test_2.pickup_latitude,df_test_2.pickup_longitude,df_test_2.dropoff_latitude,df_test_2.dropoff_longitude)
 print("test distance completed")
 df_test_2['date_pickup']=pd.to_datetime(df_test_2['pickup_datetime'])
 print("test conversion completed")
 WeekValue_dict = {0:0,1: 0, 2: 0, 3: 0, 4: 0,5: 1, 6: 1}
df_test_2['WeekValue'] = df_test_2['date_pickup'].dt.dayofweek.map(WeekValue_dict)
 print("test WeekValue completed")
 df_test_2['year'] = df_test_2['date_pickup'].dt.year
df_test_2['month'] = df_test_2['date_pickup'].dt.month
df_test_2['day'] = df_test_2['date_pickup'].dt.day
df_test_2['hour'] = df_test_2['date_pickup'].dt.hour
print(" test dates completed")
 day_int_dict = {6:0,7:0,8:0,9:0,10:0,11:0,12:1,13:1,14:2,15:2,16:2,17:2,18:2,0:4,1:4,2:4,3:4,4:4,5:4,19:3,
20:3,21:3,22:3,23:3}
df_test_2['day_int'] = df_test_2['hour'].map(day_int_dict)
 df_test_2['day_name'] = df_test_2['date_pickup'].dt.day_name()
season_dict = {12:0,1: 0, 2: 0, 3: 1, 4: 1,5: 1, 6: 2, 7: 2, 8: 2,9: 3, 10: 3, 11: 3}
df_test_2['season'] = df_test_2['month'].map(season_dict)
 print("test 3rd completed")
 lb_make = LabelEncoder()
df_test_2["day_name_num"] = lb_make.fit_transform(df_test_2["day_name"])
 df_test_3=df_test_2[['passenger_count', 'distance', 'WeekValue', 'year','month','day_int','season','day_name_num',"key",'pickup_longitude',
'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude']]
test_df = pd.DataFrame(res, columns=['image_id', 'PredictionString'])
 print(test_df )<save_to_csv>
chunk_list = []
df_train_4 = pd.DataFrame(columns=['passenger_count', 'distance', 'WeekValue', 'year', 'day_int', 'season', 'day_name_num','pickup_longitude',
'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude','fare_amount'])
 params ={
'n_estimators':100,
'max_depth':8,
'eta':0.01,
}
model = xgb.XGBRegressor(params=params)
 chunk_idx = 0
for chunk in df_train:
chunk.rename(columns={'passenger_count\r':'passenger_count'}, inplace=True)
 have_pass = chunk['passenger_count']>0
have_money= chunk['fare_amount']>0
df_train_2 = chunk[have_pass & have_money]
df_train_2 = clean_df(df_train_2)
 print("Clean data completed")

df_train_2["distance"]=getDistance(df_train_2.pickup_latitude,df_train_2.pickup_longitude,df_train_2.dropoff_latitude,df_train_2.dropoff_longitude)
 print("train distance completed")
 df_train_2['date_pickup']=pd.to_datetime(df_train_2['pickup_datetime'],format='%Y-%m-%d %H:%M:%S UTC')
 print("train conversion completed")
 WeekValue_dict = {0:0,1: 0, 2: 0, 3: 0, 4: 0,5: 1, 6: 1}
df_train_2['WeekValue'] = df_train_2['date_pickup'].dt.dayofweek.map(WeekValue_dict)
 print("train WeekValue completed")
 df_train_2['year'] = df_train_2['date_pickup'].dt.year
df_train_2['month'] = df_train_2['date_pickup'].dt.month
df_train_2['day'] = df_train_2['date_pickup'].dt.day
df_train_2['hour'] = df_train_2['date_pickup'].dt.hour
print(" train dates completed")
 day_int_dict = {6:0,7:0,8:0,9:0,10:0,11:0,12:1,13:1,14:2,15:2,16:2,17:2,18:2,0:4,1:4,2:4,3:4,4:4,5:4,19:3,
20:3,21:3,22:3,23:3}
df_train_2['day_int'] = df_train_2['hour'].map(day_int_dict)
 df_train_2['day_name'] = df_train_2['date_pickup'].dt.day_name()
season_dict = {12:0,1: 0, 2: 0, 3: 1, 4: 1,5: 1, 6: 2, 7: 2, 8: 2,9: 3, 10: 3, 11: 3}
df_train_2['season'] = df_train_2['month'].map(season_dict)
 print("train 3rd completed")
 lb_make = LabelEncoder()
df_train_2["day_name_num"] = lb_make.fit_transform(df_train_2["day_name"])
 if chunk_idx > 0:
df_train_4.append(df_train_2)
 else:
df_train_4=df_train_2
lister_4=['passenger_count', 'distance', 'WeekValue', 'year', 'day_int', 'season', 'day_name_num','pickup_longitude',
'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude']
lister_5=['fare_amount']
X_train, X_test, y_train, y_test= train_test_split(df_train_4[lister_4],df_train_4[lister_5], test_size=0.1,
random_state=42)
 if chunk_idx > 0:
model.fit(X_train, y_train, xgb_model='model_1.model')
 model.save_model('model_1.model')
 else:
model.fit(X_train, y_train)
 model.save_model('model_1.model')
 chunk_idx = chunk_idx + 1
rmse = sqrt(mean_squared_error(y_test, model.predict(X_test)))
 print("RMSE RESULT ",rmse)
 accuracy = r2_score(y_test, model.predict(X_test))
print("Accuracy: %.2f%%" %(accuracy * 100.0))
print(1)
 predictions = model.predict(df_test_3[lister_4])
 my_submission = pd.DataFrame({ 'key': df_test_3.key,'fare_amount': predictions.round(2)})
 print("here")
test_df.to_csv("/kaggle/working/submission.csv",index=False )<init_hyperparams>
my_submission.to_csv('sample_submission.csv', index=False)
 print("Writing complete")