Bike Sharing Demand (Kaggle)

Forecast use of a city bikeshare system

Bike sharing systems are a means of renting bicycles where the process of obtaining membership, rental, and bike return is automated via a network of kiosk locations throughout a city. Using these systems, people are able rent a bike from a one location and return it to a different place on an as-needed basis. Currently, there are over 500 bike-sharing programs around the world.

The data generated by these systems makes them attractive for researchers because the duration of travel, departure location, arrival location, and time elapsed is explicitly recorded. Bike sharing systems therefore function as a sensor network, which can be used for studying mobility in a city. In this competition, participants are asked to combine historical usage patterns with weather data in order to forecast bike rental demand in the Capital Bikeshare program in Washington, D.C.

Bikes


给出一个城市的自行车租借系统的历史租借数据,要求预测自行车租借数量。

Data Fields

datetime - hourly date + timestamp  
season -  1 = spring, 2 = summer, 3 = fall, 4 = winter 
holiday - whether the day is considered a holiday
workingday - whether the day is neither a weekend nor holiday
weather - 1: Clear, Few clouds, Partly cloudy, Partly cloudy 
2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist 
3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds 
4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog 
temp - temperature in Celsius
atemp - "feels like" temperature in Celsius
humidity - relative humidity
windspeed - wind speed
casual - number of non-registered user rentals initiated
registered - number of registered user rentals initiated
count - number of total rentals


主要的特征工程在于datetime上,很容易想到凌晨2点和下午两点的租借数是很不一样的。用的是RandomForestRegressor,很容易的得到了一个排名在1/3位置的结果。

import csv
import pandas as pd
from math import log, sqrt
from sklearn.ensemble import RandomForestRegressor
from sklearn.cross_validation import train_test_split

data_file = pd.read_csv("train.csv", header=0)
test_file = pd.read_csv("test.csv", header=0)

data_file["hour"] = pd.factorize([i.split()[1][:2] for i in data_file["datetime"]])[0]
data_file = data_file.drop("casual", axis=1)
data_file = data_file.drop("registered", axis=1)
data_file["rent"] = data_file["count"]
data_file = data_file.drop("count", axis=1)
test_file["hour"] = pd.factorize([i.split()[1][:2] for i in test_file["datetime"]])[0]

train_x, test_x, train_y, test_y = train_test_split(
    data_file.values[:,1:-1], data_file.values[:, -1], test_size=.01, random_state=0)
test_data = test_file.values[:, 1:]
ids = test_file.values[:, 0]

def rmsle(pred, real):
    n = len(pred)
    return sqrt(sum([(log(pred[i]+1)-log(real[i]+1))**2 for i in range(n)])/n)

reg = RandomForestRegressor(n_estimators=100, random_state=0)
reg.fit(train_x, train_y)
print reg.feature_importances_
print rmsle(reg.predict(test_x), test_y)
print rmsle(reg.predict(train_x), train_y)
output = reg.predict(test_data).astype(int)

predictions_file = open("RandomForestRegssor.csv", "wb")
open_file_object = csv.writer(predictions_file)
open_file_object.writerow(["datetime","count"])
open_file_object.writerows(zip(ids, output))
predictions_file.close()
print "done."


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值