We can use the hstack() function from the numpy module to combine two or more NumPy arrays horizontally. For example, we can use the following Python code to combine three NumPy arrays horizontally.
import numpy A = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) B = numpy.array([[10], [11], [12]]) C = numpy.array([[13], [14], [15]]) D = numpy.hstack((A, B, C)) print("A: \n", A) print("B: \n", B) print("C: \n", C) print("D: \n", D)
Here, A, B, and C are three NumPy arrays. We are using the hstack() function to combine these arrays horizontally. Please note that all the arrays should have the same number of rows. The output of the given program will be:
A: [[1 2 3] [4 5 6] [7 8 9]] B: [[10] [11] [12]] C: [[13] [14] [15]] D: [[ 1 2 3 10 13] [ 4 5 6 11 14] [ 7 8 9 12 15]]
0 Comments