Kaggle比赛之『旧金山犯罪分类预测』 demo

Posted hotsnow

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kaggle比赛之『旧金山犯罪分类预测』 demo相关的知识,希望对你有一定的参考价值。

import pandas as pd
import numpy as np

#用pandas载入csv训练数据,并解析第一列为日期格式
train=pd.read_csv(../input/train.csv, parse_dates = [Dates])
test=pd.read_csv(../input/test.csv, parse_dates = [Dates])
train

import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn import preprocessing

#用LabelEncoder对不同的犯罪类型编号
leCrime = preprocessing.LabelEncoder()
crime = leCrime.fit_transform(train.Category)

#因子化星期几,街区,小时等特征
days = pd.get_dummies(train.DayOfWeek)
district = pd.get_dummies(train.PdDistrict)
hour = train.Dates.dt.hour
hour = pd.get_dummies(hour) 

#组合特征
trainData = pd.concat([hour, days, district], axis=1)
trainData[crime]=crime

#对于测试数据做同样的处理
days = pd.get_dummies(test.DayOfWeek)
district = pd.get_dummies(test.PdDistrict)

hour = test.Dates.dt.hour
hour = pd.get_dummies(hour) 

testData = pd.concat([hour, days, district], axis=1)
trainData

from sklearn.cross_validation import train_test_split
from sklearn import preprocessing
from sklearn.metrics import log_loss
from sklearn.naive_bayes import BernoulliNB
from sklearn.linear_model import LogisticRegression
import time

# 只取星期几和街区作为分类器输入特征
features = [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, BAYVIEW, CENTRAL, INGLESIDE, MISSION,
 NORTHERN, PARK, RICHMOND, SOUTHERN, TARAVAL, TENDERLOIN]

# 分割训练集(3/5)和测试集(2/5)
training, validation = train_test_split(trainData, train_size=.60)

# 朴素贝叶斯建模,计算log_loss
model = BernoulliNB()
nbStart = time.time()
model.fit(training[features], training[crime])
nbCostTime = time.time() - nbStart
predicted = np.array(model.predict_proba(validation[features]))
print("朴素贝叶斯建模耗时 %f 秒" %(nbCostTime))
print("朴素贝叶斯log损失为 %f" %(log_loss(validation[crime], predicted)))

#逻辑回归建模,计算log_loss
model = LogisticRegression(C=.01)
lrStart= time.time()
model.fit(training[features], training[crime])
lrCostTime = time.time() - lrStart
predicted = np.array(model.predict_proba(validation[features]))
log_loss(validation[crime], predicted)
print("逻辑回归建模耗时 %f 秒" %(lrCostTime))
print("逻辑回归log损失为 %f" %(log_loss(validation[crime], predicted))) 

 

以上是关于Kaggle比赛之『旧金山犯罪分类预测』 demo的主要内容,如果未能解决你的问题,请参考以下文章

深度学习实战Kaggle比赛:房价预测(kaggle-house-price)

预告-分享Kaggle上的NFL比赛码数预测

超详解pytorch实战Kaggle比赛:房价预测

kaggle入门之Titanic生存预测

Kaggle系列之预测泰坦尼克号人员的幸存与死亡(随机森林模型)

kaggle Titanic心得