📜  R 编程中的深度学习

📅  最后修改于: 2022-05-13 01:55:09.316000             🧑  作者: Mango

R 编程中的深度学习

深度学习是一种人工智能或人工智能函数,它试图模仿或模仿人脑的工作原理,以进行数据处理和模式创建,以达到决策目的。它是人工智能中机器学习或机器学习的一个子集,拥有或拥有能够从未标记或非结构化数据中进行无监督学习的网络。深度学习也可以称为深度神经学习或深度神经网络。深度学习人工智能能够在没有人工监督的情况下从任何类型的数据中学习。作为机器学习的一个子集,深度学习利用分层的人工神经网络来执行机器学习程序或过程。在深度学习的帮助下,我们可以解开大量本质上非结构化的数据。对于人类来说,通常需要几十年的时间才能理解和处理它。

R中的实现

随着时间的推移, CRAN中的许多深度学习包都装饰了 R 语言。其中一些软件包如下:

R Package Name

Description

nnet

Used for feed-forward NN having a single hidden layer 

or for a multinomial log-linear model. 

neuralnetUsed in training of NN using back-propagation.
h2oIt is an R Scripting functionality for H2O
RSNNSAn interface to the Stuttgart NN Simulator.
tensorflowAn interface for the TensorFlow.
deepnetIt is a toolkit in R for deep learning.
darch

It is a package for the Deep Architectures and 

Restricted Boltzmann Machines.

rnnA package to implement Recurrent NN.
FCNN4RInterface for the FCNN library which allows user-extensible ANNs.
rcppdlUsed to implement machine learning.
deepr

Based on darch and deepnet, it is a package to 

enhance the streamline the training, predicting process

and fine-tuning for deep learning.

最近Keras、kerasR 和 keras也用于深度学习目的。在这里,我们将使用deepnet 包来实现深度学习。让我们继续执行的逐步过程。整个实现过程可以分为以下几个步骤:

第 1 步:安装和加载软件包

在继续实施之前,请安装所需的软件包。对于我们的实现,我们将需要deepnet 和 mlbench 包。要从 R 控制台安装这些包,请使用install.packages()命令。成功安装这些包后,使用library()命令将它们加载到 R 脚本中,如下所示:

R
# Deep Learning in R
# loading the required packages
library(mlbench)
library(deepnet)


R
# Deep learning in R
# loading the required packages
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
head(BreastCancer)
names(BreastCancer)


R
# Deep learning in R
# Loading the required packages
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
  
head(BreastCancer)
names(BreastCancer)
y = as.matrix(BreastCancer[, 11])
y[which(y == "benign")] = 0
y[which(y == "malignant")] = 1
y = as.numeric(y)
x = as.numeric(as.matrix(BreastCancer[, 2:10]))
x = matrix(as.numeric(x), ncol = 9)


R
# Deep learning in R
# Loading packages in R
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
  
head(BreastCancer)
names(BreastCancer)
y = as.matrix(BreastCancer[, 11])
y[which(y == "benign")] = 0
y[which(y == "malignant")] = 1
y = as.numeric(y)
x = as.numeric(as.matrix(BreastCancer[, 2:10]))
x = matrix(as.numeric(x), ncol = 9)
  
# Applying nn.train() function
nn <- nn.train(x, y, hidden = c(5))
yy = nn.predict(nn, x)
print(head(yy))


R
# Deep Learning in R
# Loading required packages
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
  
head(BreastCancer)
names(BreastCancer)
y = as.matrix(BreastCancer[, 11])
y[which(y == "benign")] = 0
y[which(y == "malignant")] = 1
y = as.numeric(y)
x = as.numeric(as.matrix(BreastCancer[, 2:10]))
x = matrix(as.numeric(x), ncol = 9)
  
# Applying nn.train() method
nn <- nn.train(x, y, hidden = c(5))
yy = nn.predict(nn, x)
print(head(yy))
yhat = matrix(0,length(yy), 1)
yhat[which(yy > mean(yy))] = 1
yhat[which(yy <= mean(yy))] = 0
  
# Applying table() function
cm = table(y, yhat)
print(cm)
print(sum(diag(cm))/sum(cm))


第 2 步:选择数据集

现在的任务是为实施选择合适的数据集。在这里,让我们使用mlbench 包下的乳腺癌数据集。在 R 脚本中包含数据集,如下所示:

R

# Deep learning in R
# loading the required packages
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
head(BreastCancer)
names(BreastCancer)

输出:

> head(BreastCancer)
       Id Cl.thickness Cell.size Cell.shape Marg.adhesion Epith.c.size Bare.nuclei Bl.cromatin Normal.nucleoli Mitoses     Class
1 1000025            5         1          1             1            2           1           3               1       1    benign
2 1002945            5         4          4             5            7          10           3               2       1    benign
3 1015425            3         1          1             1            2           2           3               1       1    benign
4 1016277            6         8          8             1            3           4           3               7       1    benign
5 1017023            4         1          1             3            2           1           3               1       1    benign
6 1017122            8        10         10             8            7          10           9               7       1 malignant
> names(BreastCancer)
 [1] "Id"              "Cl.thickness"    "Cell.size"       "Cell.shape"      "Marg.adhesion"   "Epith.c.size"    "Bare.nuclei"    
 [8] "Bl.cromatin"     "Normal.nucleoli" "Mitoses"         "Class" 

第 3 步:将deepnet 包应用于数据集

在所选数据集上应用深度学习包。在这里,为自变量创建一组特征,并创建因变量。

R

# Deep learning in R
# Loading the required packages
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
  
head(BreastCancer)
names(BreastCancer)
y = as.matrix(BreastCancer[, 11])
y[which(y == "benign")] = 0
y[which(y == "malignant")] = 1
y = as.numeric(y)
x = as.numeric(as.matrix(BreastCancer[, 2:10]))
x = matrix(as.numeric(x), ncol = 9)

第 4 步:建模 NN

应用deepnet包下的nn.train()函数对神经网络进行建模。

R

# Deep learning in R
# Loading packages in R
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
  
head(BreastCancer)
names(BreastCancer)
y = as.matrix(BreastCancer[, 11])
y[which(y == "benign")] = 0
y[which(y == "malignant")] = 1
y = as.numeric(y)
x = as.numeric(as.matrix(BreastCancer[, 2:10]))
x = matrix(as.numeric(x), ncol = 9)
  
# Applying nn.train() function
nn <- nn.train(x, y, hidden = c(5))
yy = nn.predict(nn, x)
print(head(yy))

输出:

> print(head(yy))
          [,1]
[1,] 0.2743838
[2,] 0.4130857
[3,] 0.2892783
[4,] 0.4232022
[5,] 0.2817078
[6,] 0.4526502

第 5 步:创建混淆矩阵

检索神经网络的输出,然后将其转换为类。为了创建混淆矩阵,请使用table()函数。还可以通过将对角元素的总和除以总计数或所有数字的总和来检查混淆矩阵的准确性。

R

# Deep Learning in R
# Loading required packages
library(mlbench)
library(deepnet)
data("BreastCancer")
  
# Clean off rows with missing data
BreastCancer = BreastCancer[which(complete.cases(BreastCancer)
                                  == TRUE),]
  
head(BreastCancer)
names(BreastCancer)
y = as.matrix(BreastCancer[, 11])
y[which(y == "benign")] = 0
y[which(y == "malignant")] = 1
y = as.numeric(y)
x = as.numeric(as.matrix(BreastCancer[, 2:10]))
x = matrix(as.numeric(x), ncol = 9)
  
# Applying nn.train() method
nn <- nn.train(x, y, hidden = c(5))
yy = nn.predict(nn, x)
print(head(yy))
yhat = matrix(0,length(yy), 1)
yhat[which(yy > mean(yy))] = 1
yhat[which(yy <= mean(yy))] = 0
  
# Applying table() function
cm = table(y, yhat)
print(cm)
print(sum(diag(cm))/sum(cm))

输出:

> print(cm)
   yhat
y     0   1
  0 425  19
  1   4 235
> print(sum(diag(cm))/sum(cm))
[1] 0.966325