Numpy Tutorial

Creating NumPy Array

NumPy Array Manipulation

Matrix in NumPy

Operations on NumPy Array

Reshaping NumPy Array

Indexing NumPy Array

Arithmetic operations on NumPy Array

Linear Algebra in NumPy Array

NumPy and Random Data

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

Split an array into multiple sub-arrays horizontally in Numpy

In NumPy, splitting an array into multiple sub-arrays horizontally means dividing the array column-wise. The function we use for this purpose is numpy.hsplit(). Let's delve into its usage.

1. Introduction:

The numpy.hsplit() function is used to split an array into multiple sub-arrays horizontally (column-wise).

2. Basic Setup:

Installation:

If you haven't installed NumPy, you can do so with:

pip install numpy

Importing:

Start by importing NumPy:

import numpy as np

3. Using the hsplit() function:

Basic Horizontal Split:

Given a 2D array, we can split it into multiple sub-arrays horizontally using hsplit():

arr = np.array([[1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]])

# Split it into 2 arrays
sub_arrays = np.hsplit(arr, 2)

for sub in sub_arrays:
    print(sub)
    print("------")

Output:

[[1 2]
 [5 6]
 [9 10]]
------
[[ 3  4]
 [ 7  8]
 [11 12]]

Specify Number of Equally Sized Chunks:

You can also specify an integer n as the second argument to split the array into n equally sized chunks:

# Split it into 4 equally sized chunks
sub_arrays = np.hsplit(arr, 4)

for sub in sub_arrays:
    print(sub)
    print("------")

Please note: In the above code, the original array has 4 columns, so splitting into 4 sub-arrays gives 1 column per array. If the array cannot be divided equally by the specified number, it will raise an error.

Specify Split Indices:

You can specify at which columns to split the array:

# Split the array after the 1st and 3rd column
sub_arrays = np.hsplit(arr, [1, 3])

for sub in sub_arrays:
    print(sub)
    print("------")

Output:

[[1]
 [5]
 [9]]
------
[[ 2  3]
 [ 6  7]
 [10 11]]
------
[[ 4]
 [ 8]
 [12]]

4. Alternative Using array_split:

You can also use the array_split function with the axis argument:

sub_arrays = np.array_split(arr, 2, axis=1)

5. Conclusion:

The hsplit() function in NumPy provides a powerful and efficient way to split arrays horizontally. Whether you're dividing arrays based on the number of desired chunks or specific column indices, hsplit() offers flexibility in handling your data splitting needs.

1. How to split array into columns in NumPy:

Description: You can split a NumPy array into columns using functions like numpy.hsplit or array slicing.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Split the array into columns
columns = np.hsplit(array_2d, array_2d.shape[1])

print("Original Array:")
print(array_2d)
print("Split into Columns:")
for column in columns:
    print(column)

2. Splitting NumPy array horizontally along axis:

Description: The numpy.hsplit function is used to split a NumPy array horizontally along the specified axis.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Split the array horizontally along axis 1
columns = np.hsplit(array_2d, axis=1)

print("Original Array:")
print(array_2d)
print("Split into Columns:")
for column in columns:
    print(column)

3. Python NumPy hsplit function examples:

Description: The numpy.hsplit function is specifically designed to split arrays horizontally.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Split the array horizontally
columns = np.hsplit(array_2d, 3)

print("Original Array:")
print(array_2d)
print("Split into Columns:")
for column in columns:
    print(column)

4. Horizontal splitting of arrays in NumPy:

Description: Horizontal splitting of arrays in NumPy is achieved using the numpy.hsplit function.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Split the array horizontally
columns = np.hsplit(array_2d, 3)

print("Original Array:")
print(array_2d)
print("Split into Columns:")
for column in columns:
    print(column)

5. NumPy split array into sub-arrays by columns:

Description: You can split a NumPy array into sub-arrays by columns using numpy.hsplit.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Split the array into sub-arrays by columns
columns = np.hsplit(array_2d, 3)

print("Original Array:")
print(array_2d)
print("Split into Sub-arrays by Columns:")
for column in columns:
    print(column)

6. Splitting 2D array horizontally using NumPy:

Description: Splitting a 2D array horizontally in NumPy can be achieved with the numpy.hsplit function.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Split the 2D array horizontally
columns = np.hsplit(array_2d, 3)

print("Original 2D Array:")
print(array_2d)
print("Split into Columns:")
for column in columns:
    print(column)

7. NumPy array horizontal partitioning:

Description: Horizontal partitioning of a NumPy array is typically performed using the numpy.hsplit function.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Horizontal partitioning using hsplit
columns = np.hsplit(array_2d, 3)

print("Original Array:")
print(array_2d)
print("Horizontal Partitioning:")
for column in columns:
    print(column)

8. Efficient ways to split arrays horizontally in NumPy:

Description: Efficient horizontal splitting of arrays in NumPy is achieved using the optimized numpy.hsplit function.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Efficient horizontal splitting
columns = np.hsplit(array_2d, 3)

print("Original Array:")
print(array_2d)
print("Efficient Horizontal Splitting:")
for column in columns:
    print(column)

9. Concatenating sub-arrays horizontally in NumPy:

Description: After splitting, you may want to concatenate sub-arrays horizontally using numpy.concatenate or numpy.hstack.

Code:

import numpy as np

# Create a 2D NumPy array
array_2d = np.array([[1, 2],
                     [3, 4],
                     [5, 6]])

# Split the array horizontally
columns = np.hsplit(array_2d, 2)

# Concatenate sub-arrays horizontally
concatenated_array = np.concatenate(columns, axis=1)

print("Original 2D Array:")
print(array_2d)
print("Concatenated Horizontally:")
print(concatenated_array)