Hướng dẫn can you change array size in python? - bạn có thể thay đổi kích thước mảng trong python không?

Trong Python, nếu đầu vào là một mảng numpy, bạn có thể sử dụng np.lib.pad để pad zeros xung quanh nó -

import numpy as np

A = np.array([[1, 2 ],[2, 3]])   # Input
A_new = np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0)) # Output

Chạy mẫu -

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array

Nếu bạn không muốn thực hiện toán học về số 0 số không, bạn có thể để mã thực hiện nó cho bạn đã đưa ra kích thước mảng đầu ra -

In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])

Hoặc, bạn có thể bắt đầu với một mảng đầu ra khởi tạo bằng không và sau đó đặt lại các phần tử đầu vào đó từ

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
0 -

In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])

Trong Matlab, bạn có thể sử dụng

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
1 -

A_new  = padarray(A,[1 2],'post')

Chạy mẫu -

>> A
A =
     1     2
     2     3
>> A_new = padarray(A,[1 2],'post')
A_new =
     1     2     0     0
     2     3     0     0
     0     0     0     0

Cải thiện bài viết

Lưu bài viết

Với sự trợ giúp của numpy numpy.resize (), chúng ta có thể thay đổi kích thước của một mảng. Mảng có thể có bất kỳ hình dạng nào nhưng để thay đổi kích thước nó, chúng ta chỉ cần kích thước tức là (2, 2), (2, 3) và nhiều hơn nữa. Trong quá trình thay đổi kích thước các số không phụ lục Numpy nếu các giá trị tại một vị trí cụ thể bị thiếu.Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.

Các tham số: new_shape: [tuple of ints hoặc n ints] hình dạng của ArrayRefcheck thay đổi kích thước: [bool, tùy chọn] Tham số này được sử dụng để kiểm tra bộ đếm tham chiếu. Theo mặc định, nó là đúng.
new_shape : [tuple of ints, or n ints] Shape of resized array
refcheck : [bool, optional] This parameter is used to check the reference counter. By Default it is True.

Trả lại: Không có None

Hầu hết các bạn bây giờ đang nghĩ rằng sự khác biệt giữa định hình lại và thay đổi kích thước là gì. Khi chúng ta nói về việc định hình lại thì một mảng thay đổi hình dạng của nó là tạm thời nhưng khi chúng ta nói về thay đổi kích thước thì những thay đổi được thực hiện vĩnh viễn.reshape and resize. When we talk about reshape then an array changes it’s shape as temporary but when we talk about resize then the changes made permanently.

Ví dụ #1: Trong ví dụ này, chúng ta có thể thấy rằng với sự trợ giúp của phương pháp

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
2, chúng ta đã thay đổi hình dạng của một mảng từ 1 × 6 thành 2 × 3.
In this example we can see that with the help of
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
2 method, we have changed the shape of an array from 1×6 to 2×3.

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
3
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
4

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
5
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
6
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
7
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
8
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
9
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
0
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
9
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
222

In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
0
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
0
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
9
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
2
In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
4

In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
5
In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
6

Output:

[[1 2 3]
 [4 5 6]]

Ví dụ #2: Trong ví dụ này, chúng ta có thể thấy rằng, chúng ta đang cố gắng thay đổi kích thước mảng hình dạng đó là loại giá trị bị ràng buộc. Nhưng Numpy xử lý tình huống này để nối các số không khi các giá trị không tồn tại trong mảng.
In this example we can see that, we are trying to resize the array of that shape which is type of out of bound values. But numpy handles this situation to append the zeros when values are not existed in the array.

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
3
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
4

In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
5
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
6
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
7
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
8
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
9
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
0
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
9
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
222

In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
0
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
2
In [7]: A  # Input: A numpy array
Out[7]: 
array([[1, 2],
       [2, 3]])

In [8]: np.lib.pad(A, ((0,1),(0,2)), 'constant', constant_values=(0))
Out[8]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])  # Zero padded numpy array
9
In [29]: A
Out[29]: 
array([[1, 2],
       [2, 3]])

In [30]: new_shape = (3,4)

In [31]: shape_diff = np.array(new_shape) - np.array(A.shape)

In [32]: np.lib.pad(A, ((0,shape_diff[0]),(0,shape_diff[1])), 
                              'constant', constant_values=(0))
Out[32]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
4
In [38]: A
Out[38]: 
array([[1, 2],
       [2, 3]])

In [39]: A_new = np.zeros(new_shape,dtype = A.dtype)

In [40]: A_new[0:A.shape[0],0:A.shape[1]] = A

In [41]: A_new
Out[41]: 
array([[1, 2, 0, 0],
       [2, 3, 0, 0],
       [0, 0, 0, 0]])
4

Ví dụ #2: Trong ví dụ này, chúng ta có thể thấy rằng, chúng ta đang cố gắng thay đổi kích thước mảng hình dạng đó là loại giá trị bị ràng buộc. Nhưng Numpy xử lý tình huống này để nối các số không khi các giá trị không tồn tại trong mảng.

Output:

[[1 2 3 4]
 [5 6 0 0]
 [0 0 0 0]]

numpy.resize (a, new_shape) [nguồn]#resize(a, new_shape)[source]#

Trả về một mảng mới với hình dạng được chỉ định.

Nếu mảng mới lớn hơn mảng gốc, thì mảng mới được lấp đầy với các bản sao lặp lại của a. Lưu ý rằng hành vi này khác với a.resize (new_shape) lấp đầy bằng các số không thay vì các bản sao lặp lại của a.

Parametersaarray_likeaarray_like

Mảng để được thay đổi kích thước.

new_shapeint hoặc tuple của intint or tuple of int

Hình dạng của mảng thay đổi kích thước.

Returnsreshaped_arrayndarrayreshaped_arrayndarray

Mảng mới được hình thành từ dữ liệu trong mảng cũ, lặp lại nếu cần thiết để điền vào số lượng các phần tử cần thiết. Dữ liệu được lặp lại lặp lại trên mảng theo thứ tự C.

Ghi chú

Khi tổng kích thước của mảng không thay đổi

[[1 2 3]
 [4 5 6]]
1 nên được sử dụng. Trong hầu hết các trường hợp khác hoặc lập chỉ mục (để giảm kích thước) hoặc đệm (để tăng kích thước) có thể là một giải pháp phù hợp hơn.

Cảnh báo: Chức năng này không xem xét các trục riêng biệt, tức là nó không áp dụng phép nội suy/ngoại suy. Nó lấp đầy mảng trả về với số lượng các phần tử cần thiết, lặp lại trên một thứ tự C, coi thường các trục (và đạp xe trở lại từ đầu nếu hình dạng mới lớn hơn). Do đó, chức năng này không phù hợp để thay đổi kích thước hình ảnh hoặc dữ liệu trong đó mỗi trục đại diện cho một thực thể riêng biệt và riêng biệt.not consider axes separately, i.e. it does not apply interpolation/extrapolation. It fills the return array with the required number of elements, iterating over a in C-order, disregarding axes (and cycling back from the start if the new shape is larger). This functionality is therefore not suitable to resize images, or data where each axis represents a separate and distinct entity.

Ví dụ

>>> a=np.array([[0,1],[2,3]])
>>> np.resize(a,(2,3))
array([[0, 1, 2],
       [3, 0, 1]])
>>> np.resize(a,(1,4))
array([[0, 1, 2, 3]])
>>> np.resize(a,(2,4))
array([[0, 1, 2, 3],
       [0, 1, 2, 3]])

Tôi có thể thay đổi kích thước của một mảng không?

Bạn không thể thay đổi kích thước của mảng sau khi nó được xây dựng.Tuy nhiên, bạn có thể thay đổi số lượng phần tử trong một danh sách mảng bất cứ khi nào bạn muốn.. However, you can change the number of elements in an ArrayList whenever you want.

Bạn có thể sửa đổi một mảng trong Python không?

Python cũng cung cấp các cách sửa đổi nội dung của một mảng, cho dù bạn cần thêm các phần tử hoặc xóa chúng., whether you need to add elements or remove them.