Neural networks also known as neural nets is a type of algorithm in machine learning and artificial intelligence that works the same as the human brain operates. The artificial neurons in the neural network depict the same behavior of neurons in the human brain. Neural networks are used in risk analysis of business, forecasting the sales, and many more. Neural networks are adaptable to changing inputs so that there is no need for designing the algorithm again based on the inputs. In this article, we’ll discuss single-layered neural network with its syntax and implementation of the

function in R programming. Following function requires **neuralnet()**

package.**neuralnet**

#### Types of Neural Networks

Neural Networks can be classified into multiple types based on their depth activation filters, Structure, Neurons used, Neuron density, data flow, and so on. The types of Neural Networks are as follows:

- Perceptron
- Feed Forward Neural Networks
- Convolutional Neural Networks
- Radial Basis Function Neural Networks
- Recurrent Neural Networks
- Sequence to Sequence Model
- Modular Neural Network

Depending upon the number of layers, there are two types of neural networks:

**Single Layered Neural Network:**A single layer neural network contains input and output layer. The input layer receives the input signals and the output layer generates the output signals accordingly.**Multilayer Neural Network:**Multilayer neural network contains input, output and one or more than one hidden layer. The hidden layers perform intermediate computations before directing the input to the output layer.

#### Single Layered Neural Network

A single-layered neural network often called perceptrons is a type of feed-forward neural network made up of input and output layers. Inputs provided are multi-dimensional. Perceptrons are acyclic in nature. The sum of the product of weights and the inputs is calculated in each node. The input layer transmits the signals to the output layer. The output layer performs computations. Perceptron can learn only a linear function and requires less training output. The output can be represented in one or two values(0 or 1).

#### Implementation in R

R language provides

function which is available in **neuralnet()**

package to perform single layered neural network.**neuralnet**

Syntax:

neuralnet(formula, data, hidden)

Parameters:

formula:represents formula on which model has to be fitted

data:represents dataframe

hidden:represents number of neurons in hidden layersTo know about more optional parameters of the function, use below command in console: help(“neuralnet”)

**Example 1:**

In this example, let us create the single-layered neural network or perceptron of iris plant species of setosa and versicolor based on sepal length and sepal width.

**Step 1:** Install the required package

`# Install the required package ` `install.packages` `(` `"neuralnet"` `) ` |

*chevron_right*

*filter_none*

**Step 2:** Load the package

`# Load the package ` `library` `(neuralnet) ` |

*chevron_right*

*filter_none*

**Step 3:** Load the dataset

`# Load dataset ` `df <- iris[1:100, ] ` |

*chevron_right*

*filter_none*

**Step 4:** Fitting neural network

`nn = ` `neuralnet` `(Species ~ Sepal.Length ` ` ` `+ Sepal.Width, data = df, ` ` ` `hidden = 0, linear.output = ` `TRUE` `) ` |

*chevron_right*

*filter_none*

**Step 5:** Plot neural network

`# Output to be present as PNG file ` `png` `(file = ` `"neuralNetworkGFG.png"` `) ` ` ` `# Plot ` `plot` `(nn) ` ` ` `# Saving the file ` `dev.off` `() ` |

*chevron_right*

*filter_none*

**Output:**

**Example 2:**

In this example, let us create more reliable neural network using multi-layer neural network and make predictions based on the dataset.

**Step 1:** Install the required package

`# Install the required package ` `install.packages` `(` `"neuralnet"` `) ` |

*chevron_right*

*filter_none*

**Step 2:** Load the package

`# Load the package ` `library` `(neuralnet) ` |

*chevron_right*

*filter_none*

**Step 3:** Load the dataset

`# Load dataset ` `df <- mtcars ` |

*chevron_right*

*filter_none*

**Step 4:** Fitting neural network

`nn <- ` `neuralnet` `(am ~ vs + cyl + disp + hp + gear ` ` ` `+ carb + wt + drat, data = df, ` ` ` `hidden = 3, linear.output = ` `TRUE` `) ` |

*chevron_right*

*filter_none*

**Step 5:** Plot neural network

`# Output to be present as PNG file ` `png` `(file = ` `"neuralNetwork2GFG.png"` `) ` ` ` `# Plot ` `plot` `(nn) ` ` ` `# Saving the file ` `dev.off` `() ` |

*chevron_right*

*filter_none*

**Step 6:** Create test dataset

`# Create test dataset ` `vs = ` `c` `(0, 1, 1) ` `cyl =` `c` `(6, 8, 8) ` `disp = ` `c` `(170, 250, 350) ` `hp = ` `c` `(120, 240, 300) ` `gear = ` `c` `(4, 5, 4) ` `carb = ` `c` `(4, 3, 3) ` `wt = ` `c` `(2.780, 3.210, 3.425) ` `drat = ` `c` `(3.05, 4.02, 3.95) ` ` ` `test <- ` `data.frame` `(vs, cyl, disp, hp, ` ` ` `gear, carb, wt, drat) ` |

*chevron_right*

*filter_none*

**Step 7:** Make prediction of test dataset

`Predict <- ` `compute` `(nn, test) ` ` ` `cat` `(` `"Predicted values:\n"` `) ` `print` `(Predict$net.result) ` |

*chevron_right*

*filter_none*

**Step 8:** Convert prediction into binary values

`probability <- Predict$net.result ` `pred <- ` `ifelse` `(probability > 0.5, 1, 0) ` `cat` `(` `"Result in binary values:\n"` `) ` `print` `(pred) ` |

*chevron_right*

*filter_none*

**Output:**

Predicted values: [,1] [1,] 0.3681382 [2,] 0.9909768 [3,] 0.9909768 Result in binary values: [,1] [1,] 0 [2,] 1 [3,] 1

**Explanation:**

In above output, “am” value for each row of test dataset is predicted using multi-layer neural network. As in neural network created by the function, predicted values greater than 0.49 makes “am” value of car to be 1.

#### Advantages of Single-layered Neural Network

- Single layer neural networks are easy to set up and train them as there is absence of hidden layers
- It has explicit links to statistical models

#### Disadvantages of Single-layered Neural Network

- It can work better only for linearly separable data.
- Single layer neural network has low accuracy as compared to multi-layer neural network.

## Recommended Posts:

- Multi Layered Neural Networks in R Programming
- How Neural Networks are used for Classification in R Programming
- Activation functions in Neural Networks
- Neural Networks | A beginners guide
- Recurrent Neural Networks Explanation
- ML | Transfer Learning with Convolutional Neural Networks
- Capsule Neural Networks | ML
- Dropout in Neural Networks
- DeepPose: Human Pose Estimation via Deep Neural Networks
- Multiple Labels Using Convolutional Neural Networks
- Activation functions in Neural Networks | Set2
- Building a Simple Neural Network in R Programming
- Introduction to Artificial Neural Network | Set 2
- Applying Convolutional Neural Network on mnist dataset
- Introduction to Recurrent Neural Network
- Importance of Convolutional Neural Network | ML
- Deep Neural net with forward and back propagation from scratch - Python
- Neural Logic Reinforcement Learning - An Introduction
- Neural Network Advances
- ML - Neural Network Implementation in C++ From Scratch

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.