Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the Outer Product of an array with vector of letters in Python
The outer product of two vectors creates a matrix where each element is the product of corresponding elements from the input vectors. When working with letters and numbers, NumPy treats the operation as string repetition.
Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is ?
[[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . [ ... . [aM*b0 aM*bN ]]
Syntax
To get the outer product of an array with vector of letters, use the numpy.outer() method ?
numpy.outer(a, b, out=None)
Parameters
- a ? First input vector (flattened if not 1-dimensional)
- b ? Second input vector (flattened if not 1-dimensional)
- out ? Optional location to store the result
Example
Let's create an outer product using a vector of letters and integers ?
import numpy as np
# Creating two numpy arrays
# First array is a vector of letters
# Second array contains integers
arr1 = np.array(['p', 'q', 'r', 's'], dtype=object)
arr2 = np.array([2, 3, 1, 3])
# Display the arrays
print("Array1...")
print(arr1)
print("\nArray2...")
print(arr2)
# Check dimensions and shape
print("\nDimensions of Array1:", arr1.ndim)
print("Dimensions of Array2:", arr2.ndim)
print("\nShape of Array1:", arr1.shape)
print("Shape of Array2:", arr2.shape)
# Get the outer product
print("\nResult (Outer Product)...")
print(np.outer(arr1, arr2))
Array1... ['p' 'q' 'r' 's'] Array2... [2 3 1 3] Dimensions of Array1: 1 Dimensions of Array2: 1 Shape of Array1: (4,) Shape of Array2: (4,) Result (Outer Product)... [['pp' 'ppp' 'p' 'ppp'] ['qq' 'qqq' 'q' 'qqq'] ['rr' 'rrr' 'r' 'rrr'] ['ss' 'sss' 's' 'sss']]
How It Works
When NumPy performs the outer product with strings and integers, it treats the multiplication as string repetition. Each letter from the first array is repeated according to the corresponding number from the second array.
Additional Example with Different Data Types
Here's another example using different letters and numbers ?
import numpy as np
# Create arrays with different values
letters = np.array(['a', 'b', 'c'], dtype=object)
numbers = np.array([1, 4, 2])
print("Letters array:", letters)
print("Numbers array:", numbers)
# Calculate outer product
result = np.outer(letters, numbers)
print("\nOuter Product:")
print(result)
Letters array: ['a' 'b' 'c'] Numbers array: [1 4 2] Outer Product: [['a' 'aaaa' 'aa'] ['b' 'bbbb' 'bb'] ['c' 'cccc' 'cc']]
Key Points
- The
dtype=objectparameter is required when working with string arrays - The result matrix has shape (m, n) where m and n are the lengths of input vectors
- String multiplication in Python repeats the string the specified number of times
- Both input arrays are automatically flattened if they are not 1-dimensional
Conclusion
The numpy.outer() method creates an outer product matrix from two vectors. When using letters with numbers, it performs string repetition to create interesting patterns in the resulting matrix.
