Skip to main content

Splitting arrays in NumPy

Splitting arrays in NumPy
Hello guys. Welcome to another exciting session on this platform. If you are new, please do check out our previous articles on NumPy and other programming languages of choice. Also check out YouTube channel to get the full video tutorials.

In the previous article, we saw how we could join two arrays in NumPy, but now we are going to see how to split an array into multiple sub-arrays. NumPy has built-in function called

array_split which is used to spit array. With this function, we can have as many arrays as we want.

Example 

import numpy as np
arr = np.array(['a', 'b', 'c', 'd'])
newarr = np.array_split(arr, 2)
print(newarr)

We pass the array we want to split to the function while specifying the number to which we want spilt the array. After this the compiler will execute the program.

OUTPUT

Splitting arrays in NumPy

In a case where the arrays have less elements than required elements, the compiler arranges the elements at end. 

Example

import numpy as np
arr = np.array(['a', 'b', 'c', 'd', 'e'])
newarr = np.array_split(arr, 3)
print(newarr)
OUTPUT

Splitting arrays in NumPy

Splitting 2-D Arrays

We are still going to apply the same function

Example 

import numpy as np
arr = np.array([['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h']])
newarr = np.array_split(arr,2)
print(newarr)

The compiler splits the input array into 3 sub-different arrays given to it by array.split

2-D arrays 

OUTPUT

Splitting arrays in NumPy

We can spit arrays vertically, horizontal or depth-wise. We do this using any of these functions below: hsplit(), vsplit() and dsplit(). These functions are found in NumPy The position is always specified after the spilt has occur.

Vertically spiting: we are going to use the function vsplit(). This functions splits array row-wise. After the spitting has occur, the resulting arrays is arranged in order row order.

Example

import numpy as np
a = np.arange(9).reshape(3, 3)
print("The array {} gets splitted \
vertically to form {} ".format(a, np.vsplit(a, 3)))

OUTPUT

Splitting arrays in NumPy

Horizontal spiting: We are going use the hsplit(). The functions spits array column wise that is horizontally. After the splitting occurs, the resulting array is arranged in column order.

Example

import numpy as np
a = np.arange(9).reshape(3, 3)
print("The array {} gets splitted \
horizontally to form {} ".format(a, np.hsplit(a, 3)))

The array is passed to the spit function. After the spitting has occur, the position (orientation of the array) is then specified for the compiler to execute the program.

OUTPUT

Splitting arrays in NumPy

Depth wise: we are going to spilt an array into sub-arrays along depth. 

import numpy as np
a = np.arange(9).reshape(3, 3)
print("The array {} gets splitted \
depth-wise to form {} ".format(a, np.hsplit(a, 3)))

OUTPUT

Splitting arrays in NumPy

We have come to the end of this session. I hope you found this guide useful. If so, do share it with others who are willing to learn NumPy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...