Articles

Joining numpy Arrays

Joining numpy Arrays


In this numpy tutorial,we will see how to join numpy arrays.

 

Introduction

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python. 

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.

Array

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.

Method -1 : Using concatenate()

This is used to join two or more arrays.

Syntax:

numpy.concatenate((array1,array2))

where,

1. array1 is the first input numpy array

2. array2 is the second iinput numpy array.

Example:

In this example, we will concatenate two numpy array of 10 integer elements each.

#import numpy module
import numpy

#create first  numpy array with 10 elements 
array_input1=numpy.array([12,34,4,5,6,7,8,4,3,5])

#display array
print(array_input1)

print()

#create first  numpy array with 10 elements 
array_input2=numpy.array([12,34,4,53,6,7,8,4,33,65])

#display array
print(array_input2)

print()


#join two numpy arrays
print(numpy.concatenate((array_input1,array_input2)))

Output:

We are displaying both the arrays along with joined array.

[12 34  4  5  6  7  8  4  3  5]

[12 34  4 53  6  7  8  4 33 65]

[12 34  4  5  6  7  8  4  3  5 12 34  4 53  6  7  8  4 33 65]

Method -2 : Using stack()

This is used to join two or more arrays vertically.

Syntax:

numpy.stack((array1,array2))

where,

1. array1 is the first input numpy array

2. array2 is the second iinput numpy array.

Example:

In this example, we will join two numpy array of 10 integer elements each.

#import numpy module
import numpy

#create first  numpy array with 10 elements 
array_input1=numpy.array([12,34,4,5,6,7,8,4,3,5])

#display array
print(array_input1)

print()

#create first  numpy array with 10 elements 
array_input2=numpy.array([12,34,4,53,6,7,8,4,33,65])

#display array
print(array_input2)

print()


#join two numpy arrays
print(numpy.stack((array_input1,array_input2)))

Output:

We are displaying both the arrays along with joined array.

[12 34  4  5  6  7  8  4  3  5]

[12 34  4 53  6  7  8  4 33 65]

[[12 34  4  5  6  7  8  4  3  5]
 [12 34  4 53  6  7  8  4 33 65]]

Method - 3 : Using vstack()

This is used to join two or more arrays vertically.

Syntax:

numpy.vstack((array1,array2))

where,

1. array1 is the first input numpy array

2. array2 is the second iinput numpy array.

Example:

In this example, we will join two numpy arrays of 10 integer elements each.

#import numpy module
import numpy

#create first  numpy array with 10 elements 
array_input1=numpy.array([12,34,4,5,6,7,8,4,3,5])

#display array
print(array_input1)

print()

#create first  numpy array with 10 elements 
array_input2=numpy.array([12,34,4,53,6,7,8,4,33,65])

#display array
print(array_input2)

print()


#join two numpy arrays
print(numpy.vstack((array_input1,array_input2)))

Output:

We are displaying both the arrays along with joined array.

[12 34  4  5  6  7  8  4  3  5]

[12 34  4 53  6  7  8  4 33 65]

[[12 34  4  5  6  7  8  4  3  5]
 [12 34  4 53  6  7  8  4 33 65]]

Method - 4 : Using hstack()

This is used to join two or more arrays horizontally.

Syntax:

numpy.hstack((array1,array2))

where,

1. array1 is the first input numpy array

2. array2 is the second iinput numpy array.

Example:

In this example, we will join two numpy arrays of 10 integer elements each.

#import numpy module
import numpy

#create first  numpy array with 10 elements 
array_input1=numpy.array([12,34,4,5,6,7,8,4,3,5])

#display array
print(array_input1)

print()

#create first  numpy array with 10 elements 
array_input2=numpy.array([12,34,4,53,6,7,8,4,33,65])

#display array
print(array_input2)

print()


#join two numpy arrays
print(numpy.hstack((array_input1,array_input2)))

Output:

We are displaying both the arrays along with joined array.

[12 34  4  5  6  7  8  4  3  5]

[12 34  4 53  6  7  8  4 33 65]

[12 34  4  5  6  7  8  4  3  5 12 34  4 53  6  7  8  4 33 65]

Method - 4 : Using hstack()

This is used to join two or more arrays along height (in-depth). The height will be equal to size of the total number of elements

Syntax:

numpy.dstack((array1,array2))

where,

1. array1 is the first input numpy array

2. array2 is the second iinput numpy array.

Example:

In this example, we will join two numpy arrays of 10 integer elements each.

#import numpy module
import numpy

#create first  numpy array with 10 elements 
array_input1=numpy.array([12,34,4,5,6,7,8,4,3,5])

#display array
print(array_input1)

print()

#create first  numpy array with 10 elements 
array_input2=numpy.array([12,34,4,53,6,7,8,4,33,65])

#display array
print(array_input2)

print()


#join two numpy arrays
print(numpy.dstack((array_input1,array_input2)))

Output:

We are displaying both the arrays along with joined array.

[12 34  4  5  6  7  8  4  3  5]

[12 34  4 53  6  7  8  4 33 65]

[[[12 12]
  [34 34]
  [ 4  4]
  [ 5 53]
  [ 6  6]
  [ 7  7]
  [ 8  8]
  [ 4  4]
  [ 3 33]
  [ 5 65]]]

 


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!