For loop take much time for completing iterations and in ML practise we have to optimize the time so we can use for loops. But then you must be wondering what to use then? Don’t worry we will discuss this in the below section.

### How to get rid from loop in Machine Learning or Neural Network?

The solution is Vectorization. Now the question arises what is vectorization? Vectorization is the process to convert algorithms from operating on a single value at a time to operate on a set of values (vector) at one time. Modern CPUs provide direct support for vector operations where a single instruction is applied to multiple data (SIMD).

### Why to use Vectorization over for loop ?

Vectorization is used to speed up the Python code . Using np.function can help in minimizing the running time of code efficiently. Let’s see the below example for better understanding.

**Example:**

`import` `numpy as np ` `import` `time ` ` ` ` ` `a ` `=` `np.array([` `1` `, ` `2` `, ` `3` `, ` `4` `]) ` ` ` `# there will be 100000 training data ` `a ` `=` `np.random.rand(` `100000` `) ` `b ` `=` `np.random.rand(` `100000` `) ` ` ` `# FOR VECTORIZTION ` `# Measure current time ` `time1 ` `=` `time.time() ` ` ` `# computing ` `c ` `=` `np.dot(a, b) ` ` ` `# Measure current time ` `time2 ` `=` `time.time() ` ` ` `# printing time taken for above calculation ` `print` `(` `"vectorized form time taken"` `+` `"\t"` `+` `str` `(` `1000` `*` `(time2` `-` `time1))` `+` `"ms"` `) ` ` ` `# FOR for loop ` `# measure current time ` `time3 ` `=` `time.time() ` `c ` `=` `0` ` ` `# computing ` `for` `i ` `in` `range` `(` `100000` `): ` ` ` `c` `+` `=` `a[i]` `*` `b[i] ` ` ` `# measure current time ` `time4 ` `=` `time.time() ` ` ` `# printing time taken for above calculation ` `print` `(` `"for loop time taken "` `+` `"\t\t"` `+` `str` `(` `1000` `*` `(time4` `-` `time3))` `+` `"ms"` `) ` |

*chevron_right*

*filter_none*

**Output:**

vectorized form time taken 0.43511390686035156ms for loop time taken 216.04394912719727ms

## Recommended Posts:

- PyQt5 QSpinBox - How to get the preferred height for the given width
- Implementing Artificial Neural Network training process in Python
- A single neuron neural network in Python
- Applying Convolutional Neural Network on mnist dataset
- Choose optimal number of epochs to train a neural network in Keras
- Implementation of Artificial Neural Network for AND Logic Gate with 2-bit Binary Input
- Implementation of Artificial Neural Network for OR Logic Gate with 2-bit Binary Input
- Implementation of Artificial Neural Network for NAND Logic Gate with 2-bit Binary Input
- Implementation of Artificial Neural Network for NOR Logic Gate with 2-bit Binary Input
- Implementation of Artificial Neural Network for XOR Logic Gate with 2-bit Binary Input
- Implementation of Artificial Neural Network for XNOR Logic Gate with 2-bit Binary Input
- Implementation of neural network from scratch using NumPy
- Deep Neural Network With L - Layers
- ANN - Implementation of Self Organizing Neural Network (SONN) from Scratch
- Deep Neural net with forward and back propagation from scratch - Python
- Recurrent Neural Networks Explanation
- ML | Transfer Learning with Convolutional Neural Networks
- Dropout in Neural Networks
- Multiple Labels Using Convolutional Neural Networks
- Neural Style Transfer with TensorFlow

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.