w3resource

NumPy: Concatenate two given arrays of shape (2, 2) and (2,1)

NumPy: Array Object Exercise-197 with Solution

Write a NumPy program to create to concatenate two given arrays of shape (2, 2) and (2,1).

Sample Solution:

Python Code:

import numpy as np 
nums1 = np.array([[4.5, 3.5],
                 [5.1, 2.3]])
nums2 = np.array([[1],
                  [2]])
print("Original arrays:")
print(nums1)
print(nums2)
print("\nConcatenating the said two arrays:")
print(np.concatenate((nums1, nums2), axis=1))

Sample Output:

Original arrays:
[[4.5 3.5]
 [5.1 2.3]]
[[1]
 [2]]

Concatenating the said two arrays:
[[4.5 3.5 1. ]
 [5.1 2.3 2. ]]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to create a 12x12x4 array with random values and extract any array of shape(6,6,3) from the said array.
Next: Write a NumPy program to create a 10x4 array filled with random floating point number values with and set the array values with specified precision.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}