- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Get the Inner product of an array and a scalar in Python

To get the Inner product of an array and a scalar, use the numpy.inner() method in Python. Ordinary inner product of vectors for 1-D arrays, in higher dimensions a sum product over the last axes. The parameters are 1 and b, two vectors. If a and b are nonscalar, their last dimensions must match.

## Steps

At first, import the required libraries-

import numpy as np

Create an array using numpy.eye(). This method returns a 2-D array with ones on the diagonal and zeros elsewhere −

arr = np.eye(5)

The val is the scalar −

val = 2

Check the datatype −

print("\nDatatype of Array...\n",arr.dtype)

Check the Dimension −

print("\nDimensions of Array...\n",arr.ndim)

Check the Shape −

print("\nShape of Array...\n",arr.shape)

To get the Outer product of an array and a scalar, use the numpy.outer() method in Python −

print("\nResult (Outer Product)...\n",np.outer(arr, val))

To get the Inner product of an array and a scalar, use the numpy.inner() method in Python −

print("\nResult (Inner Product)...\n",np.inner(arr, val))

## Example

import numpy as np # Create an array using numpy.eye(). This method returns a 2-D array with ones on the diagonal and zeros elsewhere. arr = np.eye(5) # The val is the scalar val = 2 # Display the array print("Array...\n",arr) # Check the datatype print("\nDatatype of Array...\n",arr.dtype) # Check the Dimension print("\nDimensions of Array...\n",arr.ndim) # Check the Shape print("\nShape of Array...\n",arr.shape) # To get the Inner product of an array and a scalar, use the numpy.inner() method in Python print("\nResult (Inner Product)...\n",np.inner(arr, val))

## Output

Array... [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]] Datatype of Array... float64 Dimensions of Array... 2 Shape of Array... (5, 5) Result (Inner Product)... [[2. 0. 0. 0. 0.] [0. 2. 0. 0. 0.] [0. 0. 2. 0. 0.] [0. 0. 0. 2. 0.] [0. 0. 0. 0. 2.]]

- Related Questions & Answers
- Get the Outer product of an array and a scalar in Python
- Get the Inner product of a One-Dimensional and a Two-Dimensional array in Python
- Get the Inner product of two arrays in Python
- Get the Inner product of two multi-dimensional arrays in Python
- Get the Inner product of two One-Dimensional arrays in Python
- Get the Outer Product of an array with vector of letters in Python
- Copy an element of a masked array to a standard Python scalar and return it
- Get the fully-qualified name of an inner class in Java
- Return the inner product of two masked arrays in Numpy
- Vector inner product with Einstein summation convention in Python
- Program to find sign of the product of an array using Python
- Get the mod of every element of a masked Array with a scalar value in NumPy
- Get the mod of a scalar value with every element of a masked Array in NumPy
- AND every element of a masked array by a given scalar value in Python
- AND a given scalar value with every element of a masked array in Python

Advertisements