w3resource logo
Python Exercises

Python NumPy: Create an inner product of two arrays

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-13 with Solution

Write a Python program to create an inner product of two arrays.

Sample Solution:-

Python Code:

import numpy as np
x = np.arange(24).reshape((2,3,4))
print("Array x:")
print(x)
print("Array y:")
y = np.arange(4)
print(y)
print("Inner of x and y arrays:")
print(np.inner(x, y))

Sample Output:

Array x:                                                               
[[[ 0  1  2  3]                                                        
  [ 4  5  6  7]                                                        
  [ 8  9 10 11]]                                                       
                                                                       
 [[12 13 14 15]                                                        
  [16 17 18 19]                                                        
  [20 21 22 23]]]                                                      
Array y:                                                               
[0 1 2 3]                                                              
Inner of x and y arrays:                                               
[[ 14  38  62]                                                         
 [ 86 110 134]]

Python Code Editor:

Improve this sample solution and post your code through Disqus