外卖配送时间预测项目

admin2024-04-03  0

注意:本文引用自专业人工智能社区Venus AI

更多AI知识请参考原站 ([www.aideeplearning.cn])

项目背景

  • 外卖服务的兴起: 随着互联网技术和移动应用的发展,外卖成为一种日益普及的餐饮服务方式。顾客通过餐厅、杂货店的网站或移动应用,或通过外卖订餐公司下单。
  • 配送方式的多样性: 根据地理位置的不同,配送方式包括汽车、自行车或电动滑板车。在大城市,由于餐馆和住宅的密集,非机动车辆成为更常见的配送方式。

项目目标

  • 优化配送效率: 研究不同区域和时间段的配送数据,以提高配送效率和客户满意度。
  • 提升用户体验: 分析用户订单习惯,优化点餐和配送流程,提升整体用户体验。
  • 预测配送时间: 预测外面的配送时间。

项目应用

  • 智能配送系统: 利用数据分析和机器学习技术,开发智能配送系统,自动优化配送路线和时间。
  • 客户行为分析: 分析客户的订单偏好和习惯,为餐厅和配送公司提供有价值的洞察,帮助他们更好地调整菜单和服务。

数据集

数据集主要特征
  • Delivery_person_ID (配送员ID): 配送员的唯一标识符。
  • Delivery_person_Age (配送员年龄): 配送员的年龄。
  • Delivery_person_Ratings (配送员评分): 配送员的服务评分,反映其配送效率和服务质量。
  • Restaurant_latitude, Restaurant_longitude (餐厅纬度, 餐厅经度): 餐厅的地理位置坐标。
  • Delivery_location_latitude, Delivery_location_longitude (配送地点经纬度): 订单配送地点的地理位置坐标。
  • Order_Date (订单日期): 下单的日期。
  • Time_Orderd (订单下单时间): 客户下单的具体时间。
  • Time_Order_picked (接单时间): 配送员接单的时间。
  • Weatherconditions (天气状况): 下单当天的天气状况,可能影响配送速度和安全。
  • Road_traffic_density (道路交通密度): 配送路线上的交通密度情况。
  • Vehicle_condition (交通工具状况): 配送员使用的交通工具的状况。
  • Type_of_order (订单类型): 订单的类型,如餐饮、杂货等。
  • Type_of_vehicle (交通工具类型): 配送员用于配送的交通工具类型,如汽车、自行车或电动滑板车。
  • multiple_deliveries (多重配送): 是否为一次性配送多个订单。
  • Festival (节日): 下单当天是否为节日,节日可能影响订单量和配送速度。
  • City (城市): 订单配送所在的城市。
  • Time_taken(min) (配送用时): 从接单到完成配送所需的时间,以分钟计。

模型和依赖库

Models:

  1. RandomForestRegressor
  2. Linear Regression
  3. Lasso Regression
  4. Ensemble learning

Libraries:

  1. Pandas
  2. Numpy
  3. Seaborn
  4. Matplotlib
  5. sk-learn
  6. haversine

代码实现

import pandas as pd
import numpy as np
import haversine as hs
from datetime import datetime
import matplotlib.pyplot as plt
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
data = pd.read_csv('train.csv')
data
IDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)
00x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 24
10xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 33
20x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 26
30x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 21
40x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 30
...............................................................
455880x7c09JAPRES04DEL01304.826.90232875.79425726.91232875.80425724-03-202211:35:0011:45:00conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 32
455890xd641AGRRES16DEL01214.60.0000000.0000000.0700000.07000016-02-202219:55:0020:10:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 36
455900x4f8dCHENRES08DEL03304.913.02239480.24243913.05239480.27243911-03-202223:50:0000:05:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 16
455910x5eeeCOIMBRES11DEL01204.711.00175376.98624111.04175377.02624107-03-202213:35:0013:40:00conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 26
455920x5fb2RANCHIRES09DEL02234.923.35105885.32573123.43105885.40573102-03-202217:10:0017:15:00conditions FogMedium2Snackscooter1NoMetropolitian(min) 36

45593 rows × 20 columns

data.replace({"NaN": np.nan}, regex=True, inplace = True)
data
IDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)
00x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 24
10xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 33
20x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 26
30x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 21
40x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 30
...............................................................
455880x7c09JAPRES04DEL01304.826.90232875.79425726.91232875.80425724-03-202211:35:0011:45:00conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 32
455890xd641AGRRES16DEL01214.60.0000000.0000000.0700000.07000016-02-202219:55:0020:10:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 36
455900x4f8dCHENRES08DEL03304.913.02239480.24243913.05239480.27243911-03-202223:50:0000:05:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 16
455910x5eeeCOIMBRES11DEL01204.711.00175376.98624111.04175377.02624107-03-202213:35:0013:40:00conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 26
455920x5fb2RANCHIRES09DEL02234.923.35105885.32573123.43105885.40573102-03-202217:10:0017:15:00conditions FogMedium2Snackscooter1NoMetropolitian(min) 36

45593 rows × 20 columns

data.isna().sum()
ID                                0
Delivery_person_ID                0
Delivery_person_Age            1854
Delivery_person_Ratings        1908
Restaurant_latitude               0
Restaurant_longitude              0
Delivery_location_latitude        0
Delivery_location_longitude       0
Order_Date                        0
Time_Orderd                    1731
Time_Order_picked                 0
Weatherconditions               616
Road_traffic_density            601
Vehicle_condition                 0
Type_of_order                     0
Type_of_vehicle                   0
multiple_deliveries             993
Festival                        228
City                           1200
Time_taken(min)                   0
dtype: int64
data['Distance'] = 0
data
IDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)Distance
00x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 240
10xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 330
20x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 260
30x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 210
40x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 300
..................................................................
455880x7c09JAPRES04DEL01304.826.90232875.79425726.91232875.80425724-03-202211:35:0011:45:00conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 320
455890xd641AGRRES16DEL01214.60.0000000.0000000.0700000.07000016-02-202219:55:0020:10:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 360
455900x4f8dCHENRES08DEL03304.913.02239480.24243913.05239480.27243911-03-202223:50:0000:05:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 160
455910x5eeeCOIMBRES11DEL01204.711.00175376.98624111.04175377.02624107-03-202213:35:0013:40:00conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 260
455920x5fb2RANCHIRES09DEL02234.923.35105885.32573123.43105885.40573102-03-202217:10:0017:15:00conditions FogMedium2Snackscooter1NoMetropolitian(min) 360

45593 rows × 21 columns

haversine 是一个 Python 库,用于计算地球上两点之间的大圆距离。这个距离是指在地球表面两点之间的最短路径,考虑到地球的曲率。这个库在进行地理位置数据的处理和分析时特别有用,比如在确定两个地理坐标(经度和纬度)之间的直线距离时。

def calculate_distance(row):
    loc1 = (row['Restaurant_latitude'], row['Restaurant_longitude'])
    loc2 = (row['Delivery_location_latitude'], row['Delivery_location_longitude'])
    return hs.haversine(loc1, loc2)
# Apply the function to each row
data['Distance'] = data.apply(calculate_distance, axis=1)
data['Time_prep'] = 0
data
IDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prep
00x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 243.0251530
10xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 3320.1835580
20x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 261.5527600
30x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 217.7904120
40x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 306.2101470
.....................................................................
455880x7c09JAPRES04DEL01304.826.90232875.79425726.91232875.80425724-03-202211:35:0011:45:00conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 321.4898480
455890xd641AGRRES16DEL01214.60.0000000.0000000.0700000.07000016-02-202219:55:0020:10:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 3611.0077500
455900x4f8dCHENRES08DEL03304.913.02239480.24243913.05239480.27243911-03-202223:50:0000:05:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 164.6572020
455910x5eeeCOIMBRES11DEL01204.711.00175376.98624111.04175377.02624107-03-202213:35:0013:40:00conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 266.2324020
455920x5fb2RANCHIRES09DEL02234.923.35105885.32573123.43105885.40573102-03-202217:10:0017:15:00conditions FogMedium2Snackscooter1NoMetropolitian(min) 3612.0744120

45593 rows × 22 columns

data.dropna(axis = 0, how = 'all', inplace = True)
data
IDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prep
00x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 243.0251530
10xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 3320.1835580
20x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 261.5527600
30x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 217.7904120
40x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 306.2101470
.....................................................................
455880x7c09JAPRES04DEL01304.826.90232875.79425726.91232875.80425724-03-202211:35:0011:45:00conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 321.4898480
455890xd641AGRRES16DEL01214.60.0000000.0000000.0700000.07000016-02-202219:55:0020:10:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 3611.0077500
455900x4f8dCHENRES08DEL03304.913.02239480.24243913.05239480.27243911-03-202223:50:0000:05:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 164.6572020
455910x5eeeCOIMBRES11DEL01204.711.00175376.98624111.04175377.02624107-03-202213:35:0013:40:00conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 266.2324020
455920x5fb2RANCHIRES09DEL02234.923.35105885.32573123.43105885.40573102-03-202217:10:0017:15:00conditions FogMedium2Snackscooter1NoMetropolitian(min) 3612.0744120

45593 rows × 22 columns

data = data[data["Time_Orderd"].str.contains("NaN") == False].reset_index()
data = data[data["Time_Order_picked"].str.contains("NaN") == False].reset_index() 
data.head(50)
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prep
0000x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 243.0251530
1110xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 3320.1835580
2220x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 261.5527600
3330x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 217.7904120
4440x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 306.2101470
5550x9bb4HYDRES09DEL03224.817.43166878.40832117.46166878.43832111-03-202221:20:0021:30:00conditions CloudyJam0Buffetmotorcycle1NoUrban(min) 264.6103720
6660x95b4RANCHIRES15DEL01334.723.36974685.33982023.47974685.44982004-03-202219:15:0019:30:00conditions FogJam1Mealscooter1NoMetropolitian(min) 4016.6003840
7770x9eb2MYSRES15DEL02354.612.35205876.60665012.48205876.73665014-03-202217:25:0017:30:00conditions CloudyMedium2Mealmotorcycle1NoMetropolitian(min) 3220.2052810
8880x1102HYDRES05DEL02224.817.43380978.38674417.56380978.51674420-03-202220:55:0021:05:00conditions StormyJam0Buffetmotorcycle1NoMetropolitian(min) 3419.9755480
9990xcdcdDEHRES17DEL01364.230.32796878.04610630.39796878.11610612-02-202221:55:0022:10:00conditions FogJam2Snackmotorcycle3NoMetropolitian(min) 4610.2805960
1010100xd987KOCRES16DEL01214.710.00306476.30758910.04306476.34758913-02-202214:55:0015:05:00conditions StormyHigh1Mealmotorcycle1NoMetropolitian(min) 236.2423270
1111110x2784PUNERES13DEL03234.718.56245073.91661918.65245074.00661904-03-202217:30:0017:40:00conditions SandstormsMedium1Drinksscooter1NoMetropolitian(min) 2113.7878790
1212120xc8b6LUDHRES15DEL02344.330.89958475.80934630.91958475.82934613-02-202209:20:0009:30:00conditions SandstormsLow0Buffetmotorcycle0NoMetropolitian(min) 202.9302620
1313130xdb64KNPRES14DEL02244.726.46350480.37292926.59350480.50292914-02-202219:50:0020:05:00conditions FogJam1Snackscooter1NoMetropolitian(min) 4119.3966450
1414140x3af3MUMRES15DEL03294.519.17626972.83672119.26626972.92672102-04-202220:25:0020:35:00conditions SandstormsJam2Buffetelectric_scooter1NoMetropolitian(min) 2013.7639960
1515150x3aabMYSRES01DEL0135412.31107276.65487812.35107276.69487801-03-202214:55:0015:10:00conditions WindyHigh1Mealscooter1NoMetropolitian(min) 336.2180100
1616160x689bPUNERES20DEL01334.218.59271873.77357218.70271873.88357216-03-202220:30:0020:40:00conditions SandstormsJam2Snackmotorcycle1NoMetropolitian(min) 4016.8499630
1717170x6f67HYDRES14DEL01344.917.42622878.40749517.49622878.47749520-03-202220:40:0020:50:00conditions CloudyJam0SnackmotorcycleNaNNoMetropolitian(min) 4110.7571240
1818180xc9cfKOLRES15DEL03214.722.55267288.35288522.58267288.38288515-02-202221:15:0021:30:00conditions WindyJam0Mealmotorcycle1NoUrban(min) 154.5405810
1919190x36b8PUNERES19DEL02254.118.56393473.91536718.64393573.99536716-03-202220:20:0020:25:00conditions SandstormsJam0Snackmotorcycle2NoMetropolitian(min) 3612.2560930
2020200x5795RANCHIRES06DEL02314.723.35780485.32514623.48780485.45514610-03-202222:30:0022:45:00conditions SandstormsLow2Mealelectric_scooter0NoMetropolitian(min) 2619.6187660
2121210x6c6bCOIMBRES13DEL0137511.00366976.97649411.01366976.98649411-03-202208:15:0008:30:00conditions SandstormsLow1Snackmotorcycle1NoMetropolitian(min) 201.5581340
2222220xb816CHENRES19DEL02334.312.98604780.21811413.11604780.34811427-03-202219:30:0019:45:00conditions WindyJam2Mealscooter1NoMetropolitian(min) 3920.1806630
2323230x539bMUMRES02DEL0125419.22131572.86238119.26131572.90238126-03-202212:25:0012:30:00conditions CloudyHigh1Buffetmotorcycle1NoMetropolitian(min) 346.1169800
2424240xa1b2CHENRES01DEL01294.513.00580180.25074413.11580180.36074427-03-202218:35:0018:50:00conditions SunnyMedium2Mealelectric_scooter1NoMetropolitian(min) 1517.0756180
2525250x3231JAPRES16DEL0327526.84959675.80051226.87959675.83051205-04-202220:35:0020:40:00conditions StormyJam0Snackmotorcycle0NoUrban(min) 184.4702930
2626260x8bc0SURRES15DEL03354.321.16052272.77147721.25052272.86147712-03-202223:20:0023:30:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 3813.6820630
2727270x2288BANGRES09DEL0332412.93417977.61579713.02417977.70579716-03-202221:20:0021:35:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 4713.9732020
2828280x3c5ePUNERES04DEL01234.818.51421073.83842918.62421073.94842902-04-202223:35:0023:45:00conditions WindyLow2Buffetelectric_scooter0NoUrban(min) 1216.8536420
2929290x3e60COIMBRES02DEL03314.811.02247776.99566711.05247777.02566701-04-202222:35:0022:50:00conditions SandstormsLow2Drinksmotorcycle1NoMetropolitian(min) 264.6741790
3030300xbffSURRES16DEL02364.121.16043772.77420921.21043772.82420905-03-202222:35:0022:40:00conditions StormyLow0Drinksmotorcycle1NoUrban(min) 227.6016270
3131310xd936GOARES15DEL02264.315.51315073.78346015.56315073.83346011-02-202223:25:0023:35:00conditions SandstormsLow0Buffetmotorcycle0NoUrban(min) 217.7203350
3232320xd681GOARES07DEL01384.915.56129573.74947815.60129573.78947811-02-202213:35:0013:40:00conditions CloudyHigh1Drinksscooter1NoUrban(min) 256.1756450
3333330x2876RANCHIRES02DEL01323.50.0000000.0000000.1100000.11000008-03-202221:35:0021:45:00conditions StormyJam1Snackscooter0NoUrban(min) 3517.2978900
3434340x30c8PUNERES19DEL02324.618.56393473.91536718.69393574.04536702-04-202222:35:0022:45:00conditions CloudyLow2Drinksscooter1NoMetropolitian(min) 3019.9147140
3535350xb843PUNERES02DEL01334.918.55144073.80485518.62144073.87485508-03-202218:55:0019:10:00conditions SunnyMedium1Snackmotorcycle1NoMetropolitian(min) 2210.7245330
3636360xb3a0PUNERES18DEL01204.718.59348173.78590118.63348173.82590103-04-202214:15:0014:25:00conditions WindyHigh1Snackscooter0NoUrban(min) 106.1278440
3737370x6531SURRES08DEL01204.821.17334372.79273121.18334372.80273130-03-202211:00:0011:10:00conditions SandstormsLow2Mealscooter1NoMetropolitian(min) 191.5203590
3838380x4bdaHYDRES17DEL0235517.45197678.38588317.47197678.40588301-04-202209:45:0009:55:00conditions SunnyLow2Snackscooter1NoUrban(min) 113.0734580
3939390x9d26BANGRES17DEL02264.912.97253277.60817912.99253277.62817928-03-202208:40:0008:55:00conditions StormyLow2Buffetscooter0NoMetropolitian(min) 113.1051360
4040400x9b18BANGRES17DEL01224.812.97253277.60817913.04253277.67817918-03-202223:00:0023:10:00conditions FogLow1Snackmotorcycle1NoMetropolitian(min) 2810.8674420
4141410x5d99CHENRES11DEL01354.313.06418180.23644213.13418180.30644214-03-202217:25:0017:30:00conditions CloudyMedium1Snackmotorcycle1NoMetropolitian(min) 3310.8654800
4242430xb796SURRES17DEL03374.721.14956972.77269721.23956972.86269704-04-202219:45:0019:50:00conditions SandstormsJam0Snackmotorcycle3NoMetropolitian(min) 5213.6825350
4343440x85b4MUMRES07DEL01284.619.09145872.82780819.20145872.93780808-03-202219:10:0019:25:00conditions StormyJam1Snackscooter1NoMetropolitian(min) 2216.8262650
4444450xc644KOLRES16DEL02374.922.53912988.36550722.55912988.38550713-02-202210:55:0011:00:00conditions FogLow1Snackscooter1NoUrban(min) 163.0272410
4545460x6999MUMRES02DEL03234.919.22131572.86238119.28131572.92238124-03-202221:40:0021:45:00conditions SunnyJam2Drinkselectric_scooter0NoMetropolitian(min) 119.1752070
4646470x63b6BANGRES05DEL03274.912.97032477.64574813.03032477.70574819-03-202219:00:0019:15:00conditions WindyMedium1Drinksscooter1NoMetropolitian(min) 259.3150820
4747480xa30bCHENRES11DEL01374.813.06418180.23644213.10418180.27644209-03-202216:45:0016:55:00conditions CloudyMedium0Drinksmotorcycle1NoUrban(min) 266.2090300
4848490x3556SURRES09DEL0133521.17597572.79550321.18597572.80550319-03-202211:30:0011:40:00conditions SunnyHigh2Snackscooter1NoMetropolitian(min) 181.5203470
4949500x5554RANCHIRES15DEL01314.923.36974685.33982023.40974685.37982003-04-202215:10:0015:15:00conditions FogMedium1Snackmotorcycle1NoMetropolitian(min) 166.0372340
# 计算配送时间
for i in range(len(data)):
    t1 = datetime.strptime(data['Time_Orderd'][i], "%H:%M:%S")
    t2  =datetime.strptime(data['Time_Order_picked'][i], "%H:%M:%S")
 
    diff = t2-t1
    sec = diff.total_seconds()
    minute = sec/60
    data['Time_prep'][i] = minute

data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRestaurant_latitudeRestaurant_longitudeDelivery_location_latitudeDelivery_location_longitudeOrder_DateTime_OrderdTime_Order_pickedWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prep
0000x4607INDORES13DEL02374.922.74504975.89247122.76504975.91247119-03-202211:30:0011:45:00conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 243.02515315
1110xb379BANGRES18DEL02344.512.91304177.68323713.04304177.81323725-03-202219:45:0019:50:00conditions StormyJam2Snackscooter1NoMetropolitian(min) 3320.1835585
2220x5d6dBANGRES19DEL01234.412.91426477.67840012.92426477.68840019-03-202208:30:0008:45:00conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 261.55276015
3330x7a6aCOIMBRES13DEL02384.711.00366976.97649411.05366977.02649405-04-202218:00:0018:10:00conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 217.79041210
4440x70a2CHENRES12DEL01324.612.97279380.24998213.01279380.28998226-03-202213:30:0013:45:00conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 306.21014715
...........................................................................
4385743857455880x7c09JAPRES04DEL01304.826.90232875.79425726.91232875.80425724-03-202211:35:0011:45:00conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 321.48984810
4385843858455890xd641AGRRES16DEL01214.60.0000000.0000000.0700000.07000016-02-202219:55:0020:10:00conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 3611.00775015
4385943859455900x4f8dCHENRES08DEL03304.913.02239480.24243913.05239480.27243911-03-202223:50:0000:05:00conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 164.657202-1425
4386043860455910x5eeeCOIMBRES11DEL01204.711.00175376.98624111.04175377.02624107-03-202213:35:0013:40:00conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 266.2324025
4386143861455920x5fb2RANCHIRES09DEL02234.923.35105885.32573123.43105885.40573102-03-202217:10:0017:15:00conditions FogMedium2Snackscooter1NoMetropolitian(min) 3612.0744125

43862 rows × 24 columns

data = data.drop(['Restaurant_latitude', 'Restaurant_longitude', 'Delivery_location_latitude', 'Delivery_location_longitude'], axis=1)
data = data.drop(['Time_Orderd', 'Time_Order_picked'], axis=1)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsOrder_DateWeatherconditionsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prep
0000x4607INDORES13DEL02374.919-03-2022conditions SunnyHigh2Snackmotorcycle0NoUrban(min) 243.02515315
1110xb379BANGRES18DEL02344.525-03-2022conditions StormyJam2Snackscooter1NoMetropolitian(min) 3320.1835585
2220x5d6dBANGRES19DEL01234.419-03-2022conditions SandstormsLow0Drinksmotorcycle1NoUrban(min) 261.55276015
3330x7a6aCOIMBRES13DEL02384.705-04-2022conditions SunnyMedium0Buffetmotorcycle1NoMetropolitian(min) 217.79041210
4440x70a2CHENRES12DEL01324.626-03-2022conditions CloudyHigh1Snackscooter1NoMetropolitian(min) 306.21014715
.........................................................
4385743857455880x7c09JAPRES04DEL01304.824-03-2022conditions WindyHigh1Mealmotorcycle0NoMetropolitian(min) 321.48984810
4385843858455890xd641AGRRES16DEL01214.616-02-2022conditions WindyJam0Buffetmotorcycle1NoMetropolitian(min) 3611.00775015
4385943859455900x4f8dCHENRES08DEL03304.911-03-2022conditions CloudyLow1Drinksscooter0NoMetropolitian(min) 164.657202-1425
4386043860455910x5eeeCOIMBRES11DEL01204.707-03-2022conditions CloudyHigh0Snackmotorcycle1NoMetropolitian(min) 266.2324025
4386143861455920x5fb2RANCHIRES09DEL02234.902-03-2022conditions FogMedium2Snackscooter1NoMetropolitian(min) 3612.0744125

43862 rows × 18 columns

# 将一个名为data的数据框(DataFrame)中的Weatherconditions列的值进行处理,然后将处理后的结果存储到一个名为Weather的新列中,最后删除原始的Weatherconditions列。
data['Weather'] = 0
for i in range(len(data)):
    str1 = data['Weatherconditions'][i]
    cond = str1.split(' ')
    data['Weather'][i] = cond[1]

data = data.drop(['Weatherconditions'], axis=1)
data = data.drop(['Order_Date'], axis=1)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prepWeather
0000x4607INDORES13DEL02374.9High2Snackmotorcycle0NoUrban(min) 243.02515315Sunny
1110xb379BANGRES18DEL02344.5Jam2Snackscooter1NoMetropolitian(min) 3320.1835585Stormy
2220x5d6dBANGRES19DEL01234.4Low0Drinksmotorcycle1NoUrban(min) 261.55276015Sandstorms
3330x7a6aCOIMBRES13DEL02384.7Medium0Buffetmotorcycle1NoMetropolitian(min) 217.79041210Sunny
4440x70a2CHENRES12DEL01324.6High1Snackscooter1NoMetropolitian(min) 306.21014715Cloudy
......................................................
4385743857455880x7c09JAPRES04DEL01304.8High1Mealmotorcycle0NoMetropolitian(min) 321.48984810Windy
4385843858455890xd641AGRRES16DEL01214.6Jam0Buffetmotorcycle1NoMetropolitian(min) 3611.00775015Windy
4385943859455900x4f8dCHENRES08DEL03304.9Low1Drinksscooter0NoMetropolitian(min) 164.657202-1425Cloudy
4386043860455910x5eeeCOIMBRES11DEL01204.7High0Snackmotorcycle1NoMetropolitian(min) 266.2324025Cloudy
4386143861455920x5fb2RANCHIRES09DEL02234.9Medium2Snackscooter1NoMetropolitian(min) 3612.0744125Fog

43862 rows × 17 columns

data['Time_taken'] = 0
for i in range(len(data)):
    list2 = data['Time_taken(min)'][i]
    str2 = list2.split(' ')
    data['Time_taken'][i] = int(str2[1])
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityTime_taken(min)DistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9High2Snackmotorcycle0NoUrban(min) 243.02515315Sunny24
1110xb379BANGRES18DEL02344.5Jam2Snackscooter1NoMetropolitian(min) 3320.1835585Stormy33
2220x5d6dBANGRES19DEL01234.4Low0Drinksmotorcycle1NoUrban(min) 261.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.7Medium0Buffetmotorcycle1NoMetropolitian(min) 217.79041210Sunny21
4440x70a2CHENRES12DEL01324.6High1Snackscooter1NoMetropolitian(min) 306.21014715Cloudy30
.........................................................
4385743857455880x7c09JAPRES04DEL01304.8High1Mealmotorcycle0NoMetropolitian(min) 321.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.6Jam0Buffetmotorcycle1NoMetropolitian(min) 3611.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.9Low1Drinksscooter0NoMetropolitian(min) 164.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.7High0Snackmotorcycle1NoMetropolitian(min) 266.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.9Medium2Snackscooter1NoMetropolitian(min) 3612.0744125Fog36

43862 rows × 18 columns

data = data.drop(['Time_taken(min)'], axis=1)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9High2Snackmotorcycle0NoUrban3.02515315Sunny24
1110xb379BANGRES18DEL02344.5Jam2Snackscooter1NoMetropolitian20.1835585Stormy33
2220x5d6dBANGRES19DEL01234.4Low0Drinksmotorcycle1NoUrban1.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.7Medium0Buffetmotorcycle1NoMetropolitian7.79041210Sunny21
4440x70a2CHENRES12DEL01324.6High1Snackscooter1NoMetropolitian6.21014715Cloudy30
......................................................
4385743857455880x7c09JAPRES04DEL01304.8High1Mealmotorcycle0NoMetropolitian1.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.6Jam0Buffetmotorcycle1NoMetropolitian11.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.9Low1Drinksscooter0NoMetropolitian4.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.7High0Snackmotorcycle1NoMetropolitian6.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.9Medium2Snackscooter1NoMetropolitian12.0744125Fog36

43862 rows × 17 columns

order_type = data['Type_of_order'].unique()
for i in range(len(order_type)):
    data['Type_of_order'] = data['Type_of_order'].replace(order_type[i], i)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9High20motorcycle0NoUrban3.02515315Sunny24
1110xb379BANGRES18DEL02344.5Jam20scooter1NoMetropolitian20.1835585Stormy33
2220x5d6dBANGRES19DEL01234.4Low01motorcycle1NoUrban1.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.7Medium02motorcycle1NoMetropolitian7.79041210Sunny21
4440x70a2CHENRES12DEL01324.6High10scooter1NoMetropolitian6.21014715Cloudy30
......................................................
4385743857455880x7c09JAPRES04DEL01304.8High13motorcycle0NoMetropolitian1.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.6Jam02motorcycle1NoMetropolitian11.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.9Low11scooter0NoMetropolitian4.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.7High00motorcycle1NoMetropolitian6.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.9Medium20scooter1NoMetropolitian12.0744125Fog36

43862 rows × 17 columns

data['Type_of_vehicle'] = data['Type_of_vehicle'].replace('motorcycle ', 0)
data['Type_of_vehicle'] = data['Type_of_vehicle'].replace('scooter ', 1)
data['Type_of_vehicle'] = data['Type_of_vehicle'].replace('electric_scooter ', 2)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9High2000NoUrban3.02515315Sunny24
1110xb379BANGRES18DEL02344.5Jam2011NoMetropolitian20.1835585Stormy33
2220x5d6dBANGRES19DEL01234.4Low0101NoUrban1.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.7Medium0201NoMetropolitian7.79041210Sunny21
4440x70a2CHENRES12DEL01324.6High1011NoMetropolitian6.21014715Cloudy30
......................................................
4385743857455880x7c09JAPRES04DEL01304.8High1300NoMetropolitian1.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.6Jam0201NoMetropolitian11.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.9Low1110NoMetropolitian4.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.7High0001NoMetropolitian6.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.9Medium2011NoMetropolitian12.0744125Fog36

43862 rows × 17 columns

road_traffic = data['Road_traffic_density'].unique()
for i in range(len(road_traffic)):
    data['Road_traffic_density'] = data['Road_traffic_density'].replace(road_traffic[i], i)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.902000NoUrban3.02515315Sunny24
1110xb379BANGRES18DEL02344.512011NoMetropolitian20.1835585Stormy33
2220x5d6dBANGRES19DEL01234.420101NoUrban1.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.730201NoMetropolitian7.79041210Sunny21
4440x70a2CHENRES12DEL01324.601011NoMetropolitian6.21014715Cloudy30
......................................................
4385743857455880x7c09JAPRES04DEL01304.801300NoMetropolitian1.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.610201NoMetropolitian11.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.921110NoMetropolitian4.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.700001NoMetropolitian6.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.932011NoMetropolitian12.0744125Fog36

43862 rows × 17 columns

festival = data['Festival'].unique()
for i in range(len(festival)):
    data['Festival'] = data['Festival'].replace(festival[i], i)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9020000.0Urban3.02515315Sunny24
1110xb379BANGRES18DEL02344.5120110.0Metropolitian20.1835585Stormy33
2220x5d6dBANGRES19DEL01234.4201010.0Urban1.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.7302010.0Metropolitian7.79041210Sunny21
4440x70a2CHENRES12DEL01324.6010110.0Metropolitian6.21014715Cloudy30
......................................................
4385743857455880x7c09JAPRES04DEL01304.8013000.0Metropolitian1.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.6102010.0Metropolitian11.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.9211100.0Metropolitian4.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.7000010.0Metropolitian6.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.9320110.0Metropolitian12.0744125Fog36

43862 rows × 17 columns

city = data['City'].unique()
for i in range(len(city)):
    data['City'] = data['City'].replace(city[i], i)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9020000.00.03.02515315Sunny24
1110xb379BANGRES18DEL02344.5120110.01.020.1835585Stormy33
2220x5d6dBANGRES19DEL01234.4201010.00.01.55276015Sandstorms26
3330x7a6aCOIMBRES13DEL02384.7302010.01.07.79041210Sunny21
4440x70a2CHENRES12DEL01324.6010110.01.06.21014715Cloudy30
......................................................
4385743857455880x7c09JAPRES04DEL01304.8013000.01.01.48984810Windy32
4385843858455890xd641AGRRES16DEL01214.6102010.01.011.00775015Windy36
4385943859455900x4f8dCHENRES08DEL03304.9211100.01.04.657202-1425Cloudy16
4386043860455910x5eeeCOIMBRES11DEL01204.7000010.01.06.2324025Cloudy26
4386143861455920x5fb2RANCHIRES09DEL02234.9320110.01.012.0744125Fog36

43862 rows × 17 columns

weather = data['Weather'].unique()
for i in range(len(weather)):
    data['Weather'] = data['Weather'].replace(weather[i], i)
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9020000.00.03.02515315024
1110xb379BANGRES18DEL02344.5120110.01.020.1835585133
2220x5d6dBANGRES19DEL01234.4201010.00.01.55276015226
3330x7a6aCOIMBRES13DEL02384.7302010.01.07.79041210021
4440x70a2CHENRES12DEL01324.6010110.01.06.21014715330
......................................................
4385743857455880x7c09JAPRES04DEL01304.8013000.01.01.48984810532
4385843858455890xd641AGRRES16DEL01214.6102010.01.011.00775015536
4385943859455900x4f8dCHENRES08DEL03304.9211100.01.04.657202-1425316
4386043860455910x5eeeCOIMBRES11DEL01204.7000010.01.06.2324025326
4386143861455920x5fb2RANCHIRES09DEL02234.9320110.01.012.0744125436

43862 rows × 17 columns

data = data[data['Time_prep']>0]
data = data[data['Delivery_person_Age']!="NaN "]
data = data[data['Delivery_person_Ratings']!="NaN "]
data = data[data['Road_traffic_density']!="NaN "]
data = data[data['Vehicle_condition']!="NaN"]
data = data[data['Type_of_order']!="NaN"]
data = data[data['Type_of_vehicle']!="NaN"]
data = data[data['multiple_deliveries']!="NaN"]
data = data[data['Festival']!="NaN"]
data = data[data['City']!="NaN"]
data = data[data['Distance']!="NaN"]
data = data[data['Time_prep']!="NaN"]
data = data[data['Weather']!="NaN"]
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL02374.9020000.00.03.02515315024
1110xb379BANGRES18DEL02344.5120110.01.020.1835585133
2220x5d6dBANGRES19DEL01234.4201010.00.01.55276015226
3330x7a6aCOIMBRES13DEL02384.7302010.01.07.79041210021
4440x70a2CHENRES12DEL01324.6010110.01.06.21014715330
......................................................
4385643856455870x1178RANCHIRES16DEL01354.2121010.01.016.60029510533
4385743857455880x7c09JAPRES04DEL01304.8013000.01.01.48984810532
4385843858455890xd641AGRRES16DEL01214.6102010.01.011.00775015536
4386043860455910x5eeeCOIMBRES11DEL01204.7000010.01.06.2324025326
4386143861455920x5fb2RANCHIRES09DEL02234.9320110.01.012.0744125436

43031 rows × 17 columns

data['Delivery_person_Age'] = pd.to_numeric(data['Delivery_person_Age'])
data['Delivery_person_Ratings'] = pd.to_numeric(data['Delivery_person_Ratings'])
data['multiple_deliveries'] = pd.to_numeric(data['multiple_deliveries'])
data
level_0indexIDDelivery_person_IDDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0000x4607INDORES13DEL0237.04.902000.00.00.03.02515315024
1110xb379BANGRES18DEL0234.04.512011.00.01.020.1835585133
2220x5d6dBANGRES19DEL0123.04.420101.00.00.01.55276015226
3330x7a6aCOIMBRES13DEL0238.04.730201.00.01.07.79041210021
4440x70a2CHENRES12DEL0132.04.601011.00.01.06.21014715330
......................................................
4385643856455870x1178RANCHIRES16DEL0135.04.212101.00.01.016.60029510533
4385743857455880x7c09JAPRES04DEL0130.04.801300.00.01.01.48984810532
4385843858455890xd641AGRRES16DEL0121.04.610201.00.01.011.00775015536
4386043860455910x5eeeCOIMBRES11DEL0120.04.700001.00.01.06.2324025326
4386143861455920x5fb2RANCHIRES09DEL0223.04.932011.00.01.012.0744125436

43031 rows × 17 columns

data = data.drop(['Delivery_person_ID', 'ID', 'index', 'level_0'], axis=1)
data
Delivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
037.04.902000.00.00.03.02515315024
134.04.512011.00.01.020.1835585133
223.04.420101.00.00.01.55276015226
338.04.730201.00.01.07.79041210021
432.04.601011.00.01.06.21014715330
..........................................
4385635.04.212101.00.01.016.60029510533
4385730.04.801300.00.01.01.48984810532
4385821.04.610201.00.01.011.00775015536
4386020.04.700001.00.01.06.2324025326
4386123.04.932011.00.01.012.0744125436

43031 rows × 13 columns

data['Distance'].describe()
data = data[data['Distance']<100]
data = data.reset_index()
data
indexDelivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeatherTime_taken
0037.04.902000.00.00.03.02515315024
1134.04.512011.00.01.020.1835585133
2223.04.420101.00.00.01.55276015226
3338.04.730201.00.01.07.79041210021
4432.04.601011.00.01.06.21014715330
.............................................
428724385635.04.212101.00.01.016.60029510533
428734385730.04.801300.00.01.01.48984810532
428744385821.04.610201.00.01.011.00775015536
428754386020.04.700001.00.01.06.2324025326
428764386123.04.932011.00.01.012.0744125436

42877 rows × 14 columns

data.isna().sum()
index                        0
Delivery_person_Age        211
Delivery_person_Ratings    257
Road_traffic_density         0
Vehicle_condition            0
Type_of_order                0
Type_of_vehicle              0
multiple_deliveries        927
Festival                     0
City                         0
Distance                     0
Time_prep                    0
Weather                      0
Time_taken                   0
dtype: int64
data['multiple_deliveries'].head(30)
0     0.0
1     1.0
2     1.0
3     1.0
4     1.0
5     1.0
6     1.0
7     1.0
8     1.0
9     3.0
10    1.0
11    1.0
12    0.0
13    1.0
14    1.0
15    1.0
16    1.0
17    NaN
18    1.0
19    2.0
20    0.0
21    1.0
22    1.0
23    1.0
24    1.0
25    0.0
26    0.0
27    1.0
28    0.0
29    1.0
Name: multiple_deliveries, dtype: float64
data['Delivery_person_Age'] = data['Delivery_person_Age'].fillna(data['Delivery_person_Age'].mean()) 
data['Delivery_person_Ratings'] = data['Delivery_person_Ratings'].fillna(data['Delivery_person_Ratings'].mean()) 
data['multiple_deliveries'].unique()
data['multiple_deliveries'].median()
data['multiple_deliveries'] = data['multiple_deliveries'].fillna(data['multiple_deliveries'].median())
data.isna().sum()
index                      0
Delivery_person_Age        0
Delivery_person_Ratings    0
Road_traffic_density       0
Vehicle_condition          0
Type_of_order              0
Type_of_vehicle            0
multiple_deliveries        0
Festival                   0
City                       0
Distance                   0
Time_prep                  0
Weather                    0
Time_taken                 0
dtype: int64
import seaborn as sns
plt.figure(figsize=(15,7))
ax = sns.heatmap(data.corr(), annot=True)

外卖配送时间预测项目,第1张

columns=data[['Delivery_person_Age','Delivery_person_Ratings','Road_traffic_density','Vehicle_condition','Type_of_order','Time_taken']]
sns.pairplot(columns)
plt.show()

外卖配送时间预测项目,第2张

from sklearn.model_selection import train_test_split
x = data.drop(['Time_taken', 'index'], axis=1)
x
Delivery_person_AgeDelivery_person_RatingsRoad_traffic_densityVehicle_conditionType_of_orderType_of_vehiclemultiple_deliveriesFestivalCityDistanceTime_prepWeather
037.04.902000.00.00.03.025153150
134.04.512011.00.01.020.18355851
223.04.420101.00.00.01.552760152
338.04.730201.00.01.07.790412100
432.04.601011.00.01.06.210147153
.......................................
4287235.04.212101.00.01.016.600295105
4287330.04.801300.00.01.01.489848105
4287421.04.610201.00.01.011.007750155
4287520.04.700001.00.01.06.23240253
4287623.04.932011.00.01.012.07441254

42877 rows × 12 columns

y = data[['Time_taken']]
y
Time_taken
024
133
226
321
430
......
4287233
4287332
4287436
4287526
4287636

42877 rows × 1 columns

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn import metrics
import warnings
warnings.filterwarnings('ignore')

随机森林回归

forest = RandomForestRegressor(n_estimators= 200, min_samples_split= 4, min_samples_leaf= 1)
forest.fit(x_train, y_train)

RandomForestRegressor

RandomForestRegressor(min_samples_split=4, n_estimators=200)
forest.score(x_test, y_test)
0.8366806837953505

线性回归

from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(x_train, y_train)
reg.score(x_test, y_test)
0.46966387660478515
from sklearn import linear_model
reg2=linear_model.LassoLars(alpha=.1, normalize=False)
reg2.fit(x_train,y_train)

LassoLars

LassoLars(alpha=0.1, normalize=False)
reg2.score(x_train, y_train)
0.4674006811969696

完整代码详见源网站:外卖配送时间预测项目-VenusAI (aideeplearning.cn)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!