"Klonowanie" wektorów wierszy lub kolumn

Czasami warto "sklonować" wektor wiersza lub kolumny do macierzy. Przez klonowanie mam na myśli konwersję wektora wiersza takiego jak

[1,2,3]

Do macierzy

[[1,2,3]
 [1,2,3]
 [1,2,3]
]

Lub wektor kolumnowy, np.

[1
 2
 3
]

Do

[[1,1,1]
 [2,2,2]
 [3,3,3]
]

W Matlabie lub octave można to zrobić dość łatwo:

 x = [1,2,3]
 a = ones(3,1) * x
 a =

    1   2   3
    1   2   3
    1   2   3

 b = (x') * ones(1,3)
 b =

    1   1   1
    2   2   2
    3   3   3

Chcę to powtórzyć w numpy, ale bezskutecznie

In [14]: x = array([1,2,3])
In [14]: ones((3,1)) * x
Out[14]:
array([[ 1.,  2.,  3.],
       [ 1.,  2.,  3.],
       [ 1.,  2.,  3.]])
# so far so good
In [16]: x.transpose() * ones((1,3))
Out[16]: array([[ 1.,  2.,  3.]])
# DAMN
# I end up with 
In [17]: (ones((3,1)) * x).transpose()
Out[17]:
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.],
       [ 3.,  3.,  3.]])

Dlaczego pierwsza metoda (w[16]) nie działała? Czy jest sposób, aby osiągnąć to zadanie w Pythonie w bardziej elegancki sposób?

Author: Saullo G. P. Castro, 2009-10-11

7 answers

Oto elegancki, Pythoniczny sposób na to:

>>> 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]])

Problem z {[2] } wydaje się być taki, że transpozycja nie ma wpływu na tablicę. prawdopodobnie chcesz zamiast tego Matrixa:

>>> 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]])
 55
Author: Peter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-11 07:59:23

Użycie numpy.tile:

>>> tile(array([1,2,3]), (3, 1))
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

Lub do powtarzania kolumn:

>>> tile(array([[1,2,3]]).transpose(), (1, 3))
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])
 214
Author: pv.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-08 09:02:55

Po pierwsze zauważ, że w przypadku operacji numpy broadcasting zwykle nie jest konieczne powielanie wierszy i kolumn. Zobacz to i to dla opisów.

Ale aby to zrobić, powtórz oraz newaxis są prawdopodobnie najlepszym sposobem

In [12]: x = array([1,2,3])

In [13]: repeat(x[:,newaxis], 3, 1)
Out[13]: 
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])

In [14]: repeat(x[newaxis,:], 3, 0)
Out[14]: 
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

Ten przykład dotyczy wektora wiersza, ale zastosowanie go do wektora kolumny jest miejmy nadzieję oczywiste. repeat wydaje się to dobrze pisać, ale można to również zrobić poprzez mnożenie jak w twój przykład

In [15]: x = array([[1, 2, 3]])  # note the double brackets

In [16]: (ones((3,1))*x).transpose()
Out[16]: 
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.],
       [ 3.,  3.,  3.]])
 30
Author: tom10,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-12 16:20:34

Myślę, że używanie transmisji w numpy jest najlepsze i szybsze

Zrobiłem porównanie w następujący sposób

import numpy as np
b = np.random.randn(1000)
In [105]: %timeit c = np.tile(b[:, newaxis], (1,100))
1000 loops, best of 3: 354 µs per loop

In [106]: %timeit c = np.repeat(b[:, newaxis], 100, axis=1)
1000 loops, best of 3: 347 µs per loop

In [107]: %timeit c = np.array([b,]*100).transpose()
100 loops, best of 3: 5.56 ms per loop

Około 15 razy szybciej używając broadcast

 8
Author: smartkevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-15 19:10:59

np.broadcast_to jest jeszcze szybszy niż np.tile:

x = np.arange(9)

%timeit np.broadcast_to(x, (6,9))
100000 loops, best of 3: 3.6 µs per loop

%timeit np.tile(x, (6,1))
100000 loops, best of 3: 8.4 µs per loop

Ale najszybsza jest metoda @ tom10:

%timeit np.repeat(x[np.newaxis, :], 6, axis=0) 
100000 loops, best of 3: 3.15 µs per loop
 5
Author: Mateen Ulhaq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-15 12:51:54

Możesz użyć

np.tile(x,3).reshape((4,3))

Kafelek wygeneruje powtórzenia wektora

And reshape will give it the shape you want

 2
Author: thebeancounter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-07 12:20:40
import numpy as np
x=np.array([1,2,3])
y=np.multiply(np.ones((len(x),len(x))),x).T
print(y)

[[ 1.  1.  1.]
 [ 2.  2.  2.]
 [ 3.  3.  3.]]
 0
Author: kibitzforu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-17 09:07:59