Original link:tecdat.cn/?p=18149
Original source:Tuo End number according to the tribe public number
CNN (convolutional neural network) models are very useful when we use them to train multidimensional types of data, such as images. We can also implement CNN model for regression data analysis. We previously used Python for CNN model regression, and in this article, we implement the same approach in R.
Video: R language implements CNN (Convolutional Neural Network) model for regression data analysis
We use a one-dimensional convolution function to apply the CNN model. We need the Keras R interface to use the Keras neural network API in R. If it is not available in the development environment, you need to install it first. This tutorial covers:
- To prepare data
- Define and fit the model
- Predict and visualize results
- The source code
Let’s start with the libraries we need to load this tutorial.
library(keras)
library(caret)
Copy the code
To prepare
Data In this tutorial, we use the Boston housing data set as target regression data. First, we’ll load the data set and divide it into training and test sets.
set.seed(123)
boston = MASS::Boston
indexes = createDataPartition(boston$medv, p = .85, list = F)
Copy the code
train = boston[indexes,]
test = boston[-indexes,]
Copy the code
Next, we separate the X input and Y output portions of the training and test data and convert them to matrix type. As you may know, “MEDV” is the y data output of the Boston housing data set, which is the last column. The remaining columns are x input data. Check dimensions.
dim(xtrain)
[1] 432 13
Copy the code
dim(ytrain)
[1] 432 1
Copy the code
Next, we will redefine the shape of the X input data by adding another dimension.
dim(xtrain)
[1] 432 13 1
Copy the code
dim(xtest)
[1] 74 13 1
Copy the code
Here, we can extract the input dimension of the Keras model.
print(in_dim)
[1] 13 1
Copy the code
Define and fit the model
We define the Keras model and add a one-dimensional convolution layer. The input shape changes to (13,1) as defined above. We added Flatten and Dense layers and compiled them using the “Adam” optimizer.
model %>% summary() ________________________________________________________________________ Layer (type) Output Shape Param # ======================================================================== conv1d_2 (Conv1D) (None, 12, 64) 192 ________________________________________________________________________ flatten_2 (Flatten) (None, 768) 0 ________________________________________________________________________ dense_3 (Dense) (None, 32) 24608 ________________________________________________________________________ dense_4 (Dense) (None, 1) 33 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 24833 Trainable params: 24833 Non - trainable params: 0 ________________________________________________________________________Copy the code
Next, we will use training data to fit the model.
Print (scores) loss of 24.20518Copy the code
Predict and visualize results
Now we can use the model of training to predict the test data.
predict(xtest)
Copy the code
We will check the accuracy of the forecast with RMSE metrics.
Cat ("RMSE:", RMSE(ytest, ypred)) RMSE: 4.935908Copy the code
Finally, we’ll visualize the results in the chart to check for errors.
x_axes = seq(1:length(ypred))
lines(x_axes, ypred, col = "red", type = "l", lwd = 2)
legend("topl
Copy the code
In this tutorial, we briefly learned how to use the Keras CNN model in R to fit and predict regression data.
Most welcome insight
1. Improved Nelson-Siegel model fitting yield curve analysis with r language using neural network
2. R language to achieve fitting neural network prediction and result visualization
3. Python uses genetic algorithm-neural network-fuzzy logic control algorithm for lottery analysis
4. Python for NLP: Classification using Keras’s multi-label text LSTM neural network
5. Use R language to realize the neural network to predict the stock example
6.R language deep learning image classification based on Keras small data set
7. An example of SEQ2SEQ model for NLP uses Keras for neural machine translation
8. Deep learning model analysis of sugar based on grid search algorithm optimization in Python
9. Matlab uses Bayesian optimization for deep learning