Cloning row or column vectors
Here's an elegant, Pythonic way to do it:
>>> array([[1,2,3],]*3)
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
>>> array([[1,2,3],]*3).transpose()
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
the problem with [16]
seems to be that the transpose has no effect for an array. you're probably wanting a matrix instead:
>>> x = array([1,2,3])
>>> x
array([1, 2, 3])
>>> x.transpose()
array([1, 2, 3])
>>> matrix([1,2,3])
matrix([[1, 2, 3]])
>>> matrix([1,2,3]).transpose()
matrix([[1],
[2],
[3]])
Cloning row in numpy.tile
3
is the number of times he have to clone it horizontally
and 1
is the number of times he have to clone it vertically:
for example if you have:
import numpy as np
tile = np.tile(
np.array([
[1,2,3], #<- row 1 here
[1,2,3] #<- row 2 here
]),(2,2)
)
print(tile)
you would get the array, cloned 2 times vertically and 2 times horizontally:
[[1 2 3 1 2 3]
[1 2 3 1 2 3]
[1 2 3 1 2 3]
[1 2 3 1 2 3]]
column vector times row vector to form a matrix
You can try
numpy.matmul
Notes from the documentation
The behavior depends on the arguments in the following way.
- If both arguments are 2-D they are multiplied like conventional
matrices. - If either argument is N-D, N > 2, it is treated as a stack of
matrices residing in the last two indexes and broadcast accordingly. - If the first argument is 1-D, it is promoted to a matrix by
prepending a 1 to its dimensions. After matrix multiplication the
prepended 1 is removed. - If the second argument is 1-D, it is promoted to a matrix by
appending a 1 to its dimensions. After matrix multiplication the
appended 1 is removed.
Repeat a unidimensional array over columns
Try this code:
np.array([x] * 3).T
Here 3
is the number of times you want to repeat those values
How to broadcast a row to a column in Python NumPy?
R + C[:, numpy.newaxis]
Does the trick for me.
For example
import numpy as np
r = np.ones(5)
c = np.ones(4) * 2
r + c[:, np.newaxis]
gives
array([[ 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3.]])
How do you multiply a column vector with a row vector using @ in Python?
What you can see here, is a property of how numpy arrays and matrices work.
You've created numpy arrays, each 2 long, which if you transpose, you still get an array of length 2. You can see this if you do a.shape
. You have to create a 1x2 matrix, where it will work as you expected:
>>> a=np.array([[1,2]])
>>> b=np.array([[3,4]])
>>> a@b
Error
>>> a.T@b
array([[3, 4],
[6, 8]])
>>> a@b.T
array([[11]])
>>> a.T@b.T
Error
Related Topics
Converting a String to a List of Words
Read Excel Cell Value and Not the Formula Computing It -Openpyxl
Tkinter Gui Layout Using Frames and Grid
Making All Possible Combinations of a List
How to Use an Image for the Background in Tkinter
Pandas Groupby Multiple Fields Then Diff
Using Backslash in Python (Not to Escape)
Handling Variable Number of Columns with Pandas - Python
How Might I Remove Duplicate Lines from a File
Imread Returns None, Violating Assertion !_Src.Empty() in Function 'Cvtcolor' Error
Pandas Cannot Open an Excel (.Xlsx) File
Why Is My Pygame Display Not Responding While Waiting for Input