H2O with R 简明使用手记·上篇

来源:互联网 发布:电子商务的软件流程图 编辑:程序博客网 时间:2024/04/29 21:04

在看H2O,把阅读官方booklet和API了解到的一些东西随手记了下来,以为备忘,遂成此小文。

概述

  • H2O 是一款针对智能应用的机器学习和深度学习的开源程序集。PayPal/思科/Nielsen等等也是其使用者。
  • 它丧心病狂地提供了包括R / Python / Scala / Java / JavaScript等在内的诸多语言接口
  • 支持单机和分布式运行。
  • 算法包括:广义线性模型,朴素贝叶斯,PCA,K均值聚类等。

下载

  • 安装前需要先行安装JAVA。
  • h2o可以直接通过CRAN下载得到,但CRAN中的版本有时滞后,且在实际操作时速度慢,可以直接在官网下载安装包.
  • 既可以通过网页与H2O交互 也可以通过包括R在内的各种语言接口与H2O交互。

初始化

library(h2o)h2o.init(ip = 'localhost', port = 54321, nthreads = -1, max_men_size = '4g')
  • 此ip和port连接了本机的服务器,不填默认也为此。
  • nthread默认值-2,表示用2个CPU核心,-1表示主机上有多少用多少,正数表示一个特定数。
  • max_mem_size定义给h2o的内存空间,一般4倍的数据大小能够获得最好的运行效果。它的值必须是1024的整数倍,最小2M,默认值是1g(32bit JAVA版本),1/4的内存大小(64bit JAVA版本).
  • 对于分布式的系统,可以用h2o.clusterInfo()来查看集群信息。

备数据

  • H2O是个独立的程序,R接口只是H2O 的一层皮,当数据被传给H2O以后,所有的数据操作都是在H2O上进行的。
  • 用H2O可以处理数以千计的factor levels。
H2O:"R,你用table秀一下这些信息"R:"臣妾做不到啊……"//话说我得有多无聊,大晚上编这种段子。。。
  • 圈子不同,偏要强融
    • as.data.frame()H2O->R
    • as.h2o() R->H2O
    • 一定要记得确保转换的目标具有足够大的内存空间。
    • 可以用str.H2OFrame() 查看下其中元素信息确保转换正确。

模型

监督学习

  • 广义线性模型 GLM: 返回参数与误差分布,一统possion/linear/logistic以及众多l1,l2 规则化的统计模型。
  • 分布式随机森林 DRF:返回每个预测器的重要性权值,对噪声数据鲁棒性好。
  • 梯度提升GBM:三个臭皮匠顶个诸葛亮,如今十分强大的算法。
  • 深度学习:不一定是有监督的
  • 朴素贝叶斯:常用在文本分类上。

无监督学习

  • K均值
  • 异常检测:使用深度学习的自编码器
    合起来毕业论文写完一半……

模型构建

  • 网格搜索调参

一个例子

数据导入

# Import dataset and display summarylibrary(h2o)h2o.init()airlinesURL = "https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv"airlines.hex = h2o.importFile(path = airlinesURL, destination_frame = "airlines.hex")summary(airlines.hex)

数据清洗

# View quantiles and histograms#high_na_columns = h2o.ignoreColumns(data = airlines.hex)quantile(x = airlines.hex$ArrDelay, na.rm = TRUE)h2o.hist(airlines.hex$ArrDelay)# Find number of flights by airportoriginFlights = h2o.group_by(data = airlines.hex, by = "Origin", nrow("Origin"),gb.control=list(na.methods="rm"))originFlights.R = as.data.frame(originFlights)# Find number of flights per monthflightsByMonth = h2o.group_by(data = airlines.hex, by = "Month", nrow("Month"),gb.control=list(na.methods="rm"))flightsByMonth.R = as.data.frame(flightsByMonth)# Find months with the highest cancellation ratiowhich(colnames(airlines.hex)=="Cancelled")cancellationsByMonth = h2o.group_by(data = airlines.hex, by = "Month", sum("Cancelled"),gb.control=list(na.methods="rm"))cancellation_rate = cancellationsByMonth$sum_Cancelled/flightsByMonth$nrow_Monthrates_table = h2o.cbind(flightsByMonth$Month, cancellation_rate)rates_table.R = as.data.frame(rates_table)# Construct test and train sets using samplingairlines.split = h2o.splitFrame(data = airlines.hex,ratios = 0.85)airlines.train = airlines.split[[1]]airlines.test = airlines.split[[2]]# Display a summary using table-like functionsh2o.table(airlines.train$Cancelled)h2o.table(airlines.test$Cancelled)

模型训练

# Set predictor and response variablesY = "IsDepDelayed"X = c("Origin", "Dest", "DayofMonth", "Year", "UniqueCarrier", "DayOfWeek", "Month", "DepTime", "ArrTime", "Distance")# Define the data for the model and display the resultsairlines.glm <- h2o.glm(training_frame=airlines.train, x=X, y=Y, family = "binomial", alpha = 0.5)# View model information: training statistics, performance, important variablessummary(airlines.glm)

模型预测

# Predict using GLM modelpred = h2o.predict(object = airlines.glm, newdata = airlines.test)# Look at summary of predictions: probability of TRUE class (p1)summary(pred$p1)

下篇中将看心情介绍一些数据处理中的常用函数以及简单说明一些其他模型的用法。

饿了只好去睡觉……

0 0
原创粉丝点击