2.Machine Learning with R
Last updated
Was this helpful?
Last updated
Was this helpful?
在本节中,我们将利用glmnet package,caret和pROC三个R package来实现一个简单的二分类任务。
我们这里用到的还是BreastCancer数据集。R的mlbench package提供了该数据集,大家也可以从下载。
文件包括11个列,第1列为样本编号,第2-10列为特征,11列为标签(benign为良性,malignant为恶性)
数据集有458个良性(benign)样本和241个恶性(malignant)样本
数据集有Cl.thickness,Cell.size,Cell.shape,Marg.adhesion,Epith.c.size,Bare.nuclei,Bl.cromatin,Normal.nucleoli,Mitoses9个特征,均为取值在0-10之间的整数。
glmnet: 实现了一系列带正则化的广义线性模型,我们用到的logistic regression对应binomial glm的情形
caret: 对多种分类器进行了封装,提供统一的接口,为机器学习中的调参,特征选择等常见的任务提供了便于使用的接口
pROC: 实现了一系列用于性能评估的常用函数
mlbench: 提供了一些常用的toy dataset,包括我们用到的BreastCancer
我们这里用均值补全缺失值,用Z-score对特征进行scaling
我们用caret提供的createDataPartition
方法划分训练集和测试集:
我们这里使用caret实现的递归特征消除(recursive feature elimination)方法
我们先考虑一个基于随机森林的方法:
可见RFE最终保留了4个特征
我们还可以用rfe.results$results
打印出交叉验证的结果
如果把rfFuncs
参数换成lrFuncs
,我们就可以进行基于logistic regression的特征选择:
我们使用caret提供的train函数,以glmnet package实现的带正则化的logistic regression为例通过交叉验证调参
我们可以查看交叉验证得到的最好的一组超参数组合
也可以通过cv.fitted$results
属性查看交叉验证的结果等
获取测试集上的预测结果
pROC实现了对分类性能指标置信区间的估计:
数据预处理
用均值或中位数补全空缺值
对数据进行scaling
数据可视化
用PCA对数据进行可视化
数据集划分:预留20%数据用于评估模型泛化能力,剩下的用于模型拟合
模型选择和模型拟合
任选一种分类器即可
特征选择: 用RFE或其他方式均可,特征数量不限
调参: 根据所选的分类器对相应的超参数进行搜索
模型评估
计算预留数据集上的AUROC
绘制ROC曲线
请提交代码,必要的文字解释和ROC曲线
请大家查阅资料,回答以下两个关于随机森林的问题:
随机森林中树的数量是不是一个需要通过交叉验证调整的超参数?为什么?
请问什么是随机森林的out-of-bag (OOB) error?它和bootstrapping有什么关系?
randomForest.R
: Random Forest
logistic_regression.R
: Logistic Regression
svm.R
: SVM
plot_result.R
: Plot your training and testing performance
more R packages for machine learning
通过改变train
函数的method
参数,我们就可以很容易的拟合其他分类器,caret封装了大量可选的分类和回归模型,请大家参考
我们提供了一个qPCR数据集,第1列为sample id,第2-12列为特征(11个基因的表达量),第13列为样本标签(负例为健康人:NC,正例为肝癌病人:HCC)。请大家用R语言完成以下任务:
其他机器学习模型相关:
Functions for latent class analysis, short time Fourier transform, fuzzy clustering, support vector machines, shortest path computation, bagged clustering, naive Bayes classifier etc (142479 downloads)
Recursive Partitioning and Regression Trees. (135390)
A collection of network analysis tools. (122930)
Feed-forward Neural Networks and Multinomial Log-Linear Models. (108298)
Breiman and Cutler's random forests for classification and regression. (105375)
package (short for Classification And REgression Training) is a set of functions that attempt to streamline the process for creating predictive models. (87151)
Kernel-based Machine Learning Lab. (62064)
Lasso and elastic-net regularized generalized linear models. (56948)
Visualizing the performance of scoring classifiers. (51323)
Generalized Boosted Regression Models. (44760)
A Laboratory for Recursive Partitioning. (43290)
Mining Association Rules and Frequent Itemsets. (39654)
Classification and regression trees. (27882)
Classification and visualization. (27828)
R/Weka interface. (26973)
Improved Predictors. (22358)
Least Angle Regression, Lasso and Forward Stagewise. (19691)
Multivariate Adaptive Regression Spline Models. (15901)
Classification, regression, feature evaluation and ordinal evaluation. (13856)
Model-Based Boosting. (13078)