机器学习案例详解的直播互动平台——
机器学习训练营(入群联系qq:2279055353)
下期直播案例预告:大数据预测商品的销售量波动趋势
案例背景
该案例是一个美国纽约市出租车的行程计费问题。给定乘客上、下车的位置信息,你要根据行程距离,建立模型预测出租车的车费。该模型可以扩展应用于出租车车费定价、阶梯价格设定等问题。
-
案例来源: Google云课程
-
代码实现: Python
数据描述
ID
- key 行识别符字符串。
特征
-
pickup_datetime 出租车行程开始时间
-
pickup_longitude 出租车行程开始经度坐标
-
pickup_latitude 出租车行程开始纬度坐标
-
dropoff_longitude 出租车行程结束经度坐标
-
dropoff_latitude 出租车行程结束纬度坐标
-
passenger_count 乘客数
目标变量
- fare_amount 车费
数据准备
我们想在训练集train.csv
上建立预测模型,所以首先加载训练数据。由于整个训练集特别庞大,共有55M行。根据内存容量大小,我们选择加载100万行的数据子集。
# Initial Python environment setup...
import numpy as np # linear algebra
import pandas as pd # CSV file I/O (e.g. pd.read_csv)
import os # reading the input files we have access to
train_df = pd.read_csv('../input/train.csv', nrows = 10_000_000)
print(train_df.dtypes)
我们想使用经度、纬度坐标定义一个行程向量。为此,我们产生两个新的特征,分别表示经度、纬度的距离。
# Given a dataframe, add two new features 'abs_diff_longitude' and
# 'abs_diff_latitude' reprensenting the "Manhattan vector" from
# the pickup location to the dropoff location.
def add_travel_vector_features(df):
df['abs_diff_longitude'] = (df.dropoff_longitude - df.pickup_longitude).abs()
df['abs_diff_latitude'] = (df.dropoff_latitude - df.pic