riqitang
riqitang

Reputation: 3371

Concise way to declare a numpy matrix

What is a short, readable way to declare a 999x999 numpy matrix where each row is [1,2,3,...,999]? The final matrix should be:

[[1,2,3,...,999]
[1,2,3,...,999]
...
[1,2,3,...,999]]

Upvotes: 1

Views: 102

Answers (2)

AGN Gazer
AGN Gazer

Reputation: 8378

@jpp answer is elegant but the following solution is more efficient:

res = np.empty((nrows, ncols))
res[:, :] = np.arange(ncols)

Timing:

%timeit a = np.empty((1000,1000)); a[:, :] = np.arange(1000)
445 µs ± 9.08 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit np.tile(range(1000), (1000, 1))
1.43 ms ± 15.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Further timing tests:

Following @jpp comments I added one more test done in Python interpreter directly (unlike the original test that was run in a Jupyter notebook - because it was up and running at that moment):

>>> import sys
>>> print(sys.version)
3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
>>> import numpy as np
>>> print(np.__version__)
1.13.3
>>> import timeit
>>> t = timeit.repeat('res = np.empty((nrows, ncols)); res[:, :] = np.arange(ncols)', setup='import numpy as np; nrows=ncols=1000', number=100, repeat=50)
>>> print(min(t), max(t), np.mean(t), np.std(t))
0.04336756598786451 0.053294404002372175 0.0459639201409 0.00240180447219
>>> t = timeit.repeat('res = np.tile(range(ncols), (nrows, 1))', setup='import numpy as np; nrows=ncols=1000', number=100, repeat=50)
>>> print(min(t), max(t), np.mean(t), np.std(t))
0.05032560401014052 0.05859642301220447 0.0530669655403 0.00225117881195

The results with numpy 1.14.5 are virtually identical:

>>> import sys
>>> print(sys.version)
3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
>>> import numpy as np
>>> print(np.__version__)
1.14.5
>>> import timeit
>>> t = timeit.repeat('res = np.empty((nrows, ncols)); res[:, :] = np.arange(ncols)', setup='import numpy as np; nrows=ncols=1000', number=100, repeat=50)
>>> print(min(t), max(t), np.mean(t), np.std(t))
0.04360878499574028 0.05562149798788596 0.04657964294136036 0.0025253372244474614
>>> t = timeit.repeat('res = np.tile(range(ncols), (nrows, 1))', setup='import numpy as np; nrows=ncols=1000', number=100, repeat=50)
>>> print(min(t), max(t), np.mean(t), np.std(t))
0.05024543400213588 0.06169128899637144 0.05339125283906469 0.00276210097759817

Upvotes: 1

jpp
jpp

Reputation: 164713

You can use numpy.tile:

import numpy as np

res = np.tile(range(10), (5, 1))

print(res)

array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

Alternatively, you can add to an array of zeros:

res = np.zeros((5, 10)) + range(10)

Upvotes: 5

Related Questions