- 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

# Apply the ufunc outer() function to all pairs of a One-Dimensional Arrays in Numpy

We will apply the **ufunc outer()** function to all pairs of One-Dimensional arrays. The **numpy.ufunc** has functions that operate element by element on whole arrays. The ufuncs are written in C (for speed) and linked into Python with NumPy’s ufunc facility.

A universal function (or ufunc for short) is a function that operates on ndarrays in an element-byelement fashion, supporting array broadcasting, type casting, and several other standard features. That is, a ufunc is a "vectorized" wrapper for a function that takes a fixed number of specific inputs and produces a fixed number of specific outputs.

## Steps

At first, import the required library −

import numpy as np

The numpy.ufunc has functions that operate element by element on whole arrays. The ufuncs are written in C (for speed) and linked into Python with NumPy's ufunc facility −

Create two 1D arrays −

arr1 = np.array([5, 10, 15, 20, 25, 30, 35, 40]) arr2 = np.array([7, 14, 21, 28, 35])

Display the arrays −

print("Array 1...\n", arr1) print("\nArray 2...\n", arr2)

Get the type of the arrays −

print("\nOur Array 1 type...\n", arr1.dtype) print("\nOur Array 2 type...\n", arr2.dtype)

Get the dimensions of the Arrays −

print("\nOur Array 1 Dimensions...\n",arr1.ndim) print("\nOur Array 2 Dimensions...\n",arr2.ndim)

Get the shape of the Arrays −

print("\nOur Array 1 Shape...\n",arr1.shape) print("\nOur Array 2 Shape...\n",arr2.shape)

Apply the ufunc outer() function to all pairs of 1D arrays −

res = np.multiply.outer(arr1, arr2) print("\nResult...\n",res) print("\nShape...\n",res.shape)

## Example

import numpy as np # The numpy.ufunc has functions that operate element by element on whole arrays. # ufuncs are written in C (for speed) and linked into Python with NumPy's ufunc facility # Create two 1D arrays arr1 = np.array([5, 10, 15, 20, 25, 30, 35, 40]) arr2 = np.array([7, 14, 21, 28, 35]) # Display the arrays print("Array 1...\n", arr1) print("\nArray 2...\n", arr2) # Get the type of the arrays print("\nOur Array 1 type...\n", arr1.dtype) print("\nOur Array 2 type...\n", arr2.dtype) # Get the dimensions of the Arrays print("\nOur Array 1 Dimensions...\n",arr1.ndim) print("\nOur Array 2 Dimensions...\n",arr2.ndim) # Get the shape of the Arrays print("\nOur Array 1 Shape...\n",arr1.shape) print("\nOur Array 2 Shape...\n",arr2.shape) # Apply the ufunc outer() function to all pairs of 1D arrays res = np.multiply.outer(arr1, arr2) print("\nResult...\n",res) print("\nShape...\n",res.shape)

## Output

Array 1... [ 5 10 15 20 25 30 35 40] Array 2... [ 7 14 21 28 35] Our Array 1 type... int64 Our Array 2 type... int64 Our Array 1 Dimensions... 1 Our Array 2 Dimensions... 1 Our Array 1 Shape... (8,) Our Array 2 Shape... (5,) Result... [[ 35 70 105 140 175] [ 70 140 210 280 350] [ 105 210 315 420 525] [ 140 280 420 560 700] [ 175 350 525 700 875] [ 210 420 630 840 1050] [ 245 490 735 980 1225] [ 280 560 840 1120 1400]] Shape... (8, 5)

- Related Questions & Answers
- Apply the ufunc outer() function to all pairs of Two-Dimensional Array in Numpy
- Apply the ufunc outer() function to all pairs in Numpy
- Return the outer product of two masked One-Dimensional Numpy arrays
- Get the Outer product of two One-Dimensional arrays in Python
- Return the outer product of two masked Three-Dimensional Numpy arrays
- Return the inner product of two masked One-Dimensional arrays in Numpy
- Return the outer product of two masked arrays in Numpy
- Compute the bit-wise XOR of two One-Dimensional Numpy arrays element-wise
- Compute the bit-wise AND of two One-Dimensional Numpy arrays element-wise
- Compute the bit-wise OR of two One-Dimensional Numpy arrays element-wise
- Return the outer product of two masked arrays with different shapes in Numpy
- Get the Kronecker product of two One-Dimensional Arrays in Python
- Get the Inner product of two One-Dimensional arrays in Python
- Compute the bit-wise AND of a One Dimensional and a zero-dimensional array element-wise in Numpy
- Apply accumulate for a multi-dimensional array along an axis in Numpy