Có cách nào tốt hơn trong numpy
để xếp mảng một số nguyên không? Đây được công việc làm, nhưng thiếu uyển chuyển và không dễ dàng khái quát đến n-kích thước:numpy.tile số không phải là số nguyên
import numpy as np
arr = np.arange(6).reshape((2, 3))
desired_shape = (5, 8)
reps = tuple([x // y for x, y in zip(desired_shape, arr.shape)])
left = tuple([x % y for x, y in zip(desired_shape, arr.shape)])
tmp = np.tile(arr, reps)
tmp = np.r_[tmp, tmp[slice(left[0]), :]]
tmp = np.c_[tmp, tmp[:, slice(left[1])]]
sản lượng này:
array([[0, 1, 2, 0, 1, 2, 0, 1],
[3, 4, 5, 3, 4, 5, 3, 4],
[0, 1, 2, 0, 1, 2, 0, 1],
[3, 4, 5, 3, 4, 5, 3, 4],
[0, 1, 2, 0, 1, 2, 0, 1]])
EDIT: Hiệu suất kết quả
Một số thử nghiệm của ba câu trả lời đã được tổng quát thành n-kích thước. Những định nghĩa đã được đặt trong một tập tin newtile.py
:
import numpy as np
def tile_pad(a, dims):
return np.pad(a, tuple((0, i) for i in (np.array(dims) - a.shape)),
mode='wrap')
def tile_meshgrid(a, dims):
return a[np.meshgrid(*[np.arange(j) % k for j, k in zip(dims, a.shape)],
sparse=True, indexing='ij')]
def tile_rav_mult_idx(a, dims):
return a.flat[np.ravel_multi_index(np.indices(dims), a.shape, mode='wrap')]
Sau đây là các dòng bash:
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_pad(np.arange(30).reshape(2, 3, 5), (3, 5, 7))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_meshgrid(np.arange(30).reshape(2, 3, 5), (3, 5, 7))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_rav_mult_idx(np.arange(30).reshape(2, 3, 5), (3, 5, 7))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_pad(np.arange(2310).reshape(2, 3, 5, 7, 11), (13, 17, 19, 23, 29))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_meshgrid(np.arange(2310).reshape(2, 3, 5, 7, 11), (13, 17, 19, 23, 29))'
python -m timeit -s 'import numpy as np' 'import newtile' 'newtile.tile_rav_mult_idx(np.arange(2310).reshape(2, 3, 5, 7, 11), (13, 17, 19, 23, 29))'
Sau đây là các kết quả với mảng nhỏ (2 x 3 x 5):
pad: 10000 loops, best of 3: 106 usec per loop
meshgrid: 10000 loops, best of 3: 56.4 usec per loop
ravel_multi_index: 10000 loops, best of 3: 50.2 usec per loop
Đây là kết quả có mảng lớn hơn (2 x 3 x 5 x 7 x 11):
pad: 10 loops, best of 3: 25.2 msec per loop
meshgrid: 10 loops, best of 3: 300 msec per loop
ravel_multi_index: 10 loops, best of 3: 218 msec per loop
Vì vậy, phương pháp sử dụng np.pad
có lẽ là lựa chọn hiệu quả nhất.
hành vi sẽ như thế nào khi sử dụng 'np.tile()' với số nguyên không? –
@SaulloCastro: Có lẽ tiêu đề của tôi hơi gây hiểu nhầm. Theo ý kiến của tôi 'np.tile' nên * không * lấy các đối số phi số nguyên thành' reps'. Những gì tôi muốn đạt được là * tương tự * với những gì sẽ xảy ra nếu 'np.tile' thực hiện các đối số không phải số nguyên cho' reps' ** và ** nếu số nguyên không được truyền đạt được số nguyên của hàng/cột/v.v. trong mảng đầu ra. Ví dụ tương tự gần nhất mà tôi biết là đối số 'length.out' đối với hàm' rep() 'trong ngôn ngữ' R'. – drammock