Hướng dẫn how to divide matrices in python - cách chia ma trận trong python

numpy.divide (x1, x2, /, out = none, *, wher = true, casting = 'more_kind', order = 'k', dtype = nonedivide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])= 'divide'>#

Chia đối số yếu tố khôn ngoan.

Parametersx1array_likex1array_like

Mảng cổ tức.

x2array_likearray_like

Mảng chia. Nếu x1.shape != x2.shape, chúng phải được phát theo hình dạng chung (trở thành hình dạng của đầu ra).

outndarray, không có, hoặc tuple của ndarray và không có, tùy chọnndarray, None, or tuple of ndarray and None, optional

Một vị trí mà kết quả được lưu trữ. Nếu được cung cấp, nó phải có một hình dạng mà các đầu vào phát sóng. Nếu không được cung cấp hoặc không có, một mảng mới được phân bổ được trả lại. Một tuple (chỉ có thể là đối số từ khóa) phải có độ dài bằng số lượng đầu ra.

wherearray_like, tùy chọnarray_like, optional

Điều kiện này được phát trên đầu vào. Tại các vị trí mà điều kiện là đúng, mảng ra sẽ được đặt thành kết quả UFUNC. Ở những nơi khác, mảng ra sẽ giữ lại giá trị ban đầu của nó. Lưu ý rằng nếu một mảng ra không được tạo ra được tạo thông qua out=None mặc định, các vị trí trong đó điều kiện là sai sẽ vẫn không được cung cấp.

**kwargs

Đối với các đối số chỉ từ khóa khác, hãy xem các tài liệu UFUNC.ufunc docs.

Returnsyndarray hoặc vô hướngyndarray or scalar

Thương số x1/x2, yếu tố khôn ngoan. Đây là vô hướng nếu cả x1 và x2 là vô hướng.

Xem thêm

seterr

Đặt nên nâng hay cảnh báo về tràn, tăng và phân chia theo số 0.

Ghi chú

Tương đương với

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
0 /
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
1 về mặt mảng-broadcasting.

Hàm

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
2 là bí danh cho
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
3.

Ví dụ

>>> np.divide(2.0, 4.0)
0.5
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.divide(x1, x2)
array([[nan, 1. , 1. ],
       [inf, 4. , 2.5],
       [inf, 7. , 4. ]])

Toán tử

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
4 có thể được sử dụng làm tốc ký cho
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
5 trên ndarrays.

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])

chia ():- Hàm này được sử dụng để thực hiện phân chia ma trận yếu tố ..
Array element from first array is divided by elements from second element (all happens element-wise). Both arr1 and arr2 must have same shape and element in arr2 must not be zero; otherwise it will raise an error.

Làm thế nào để bạn chia một ma trận numpy?

Sử dụng hàm numpy.divide () để chia các phần tử mảng thứ nhất (ARR1) với các phần tử mảng thứ hai (ARR2). Cả ARR1 và ARR2 phải có cùng một hình dạng và phần tử trong ARR2 không được bằng không; Nếu không, nó sẽ gây ra lỗi.[array_like]Input array or object which works as dividend.
arr2 : [array_like]Input array or object which works as divisor.
out : [ndarray, optional]Output array with same dimensions as Input array,
placed with result.
**kwargs : allows you to pass keyword variable length of argument to a function.
It is used when we want to handle named argument in a function.
where : [array_like, optional]True value means to calculate the universal
functions(ufunc) at that position, False value means to leave the
value in the output alone.

Trở về :

An array with arr1/arr2(element-wise) as elements of output array.

& nbsp; mã 1: phân chia ARR1 cho các phần tử ARR2
Code 1 : arr1 divide by arr2 elements

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
6
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
7

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
8
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
An array with arr1/arr2(element-wise) as elements of output array.
0
An array with arr1/arr2(element-wise) as elements of output array.
1
An array with arr1/arr2(element-wise) as elements of output array.
2223
An array with arr1/arr2(element-wise) as elements of output array.
2221
An array with arr1/arr2(element-wise) as elements of output array.
22272722222929

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
1
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
An array with arr1/arr2(element-wise) as elements of output array.
0
An array with arr1/arr2(element-wise) as elements of output array.
1
An array with arr1/arr2(element-wise) as elements of output array.
222222222222222222222222222222222

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
6
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
7

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
0
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
1

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
2
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
4

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
7
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
8

Đầu ra:

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]

& nbsp; Mã 2: Các yếu tố của ARR1 chia cho chia
Code 2 : elements of arr1 divided by divisor

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
6
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
7

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
8
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
An array with arr1/arr2(element-wise) as elements of output array.
0
An array with arr1/arr2(element-wise) as elements of output array.
1
An array with arr1/arr2(element-wise) as elements of output array.
2223
An array with arr1/arr2(element-wise) as elements of output array.
2221
An array with arr1/arr2(element-wise) as elements of output array.
22272722222929

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
1
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
An array with arr1/arr2(element-wise) as elements of output array.
0
An array with arr1/arr2(element-wise) as elements of output array.
1
An array with arr1/arr2(element-wise) as elements of output array.
222222222222222222222222222222222

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
6
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
7

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
0
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
1

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
7
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
8

Đầu ra:

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]

& nbsp; Mã 2: Các yếu tố của ARR1 chia cho chia
Code 3 : warning if arr2 has element = 0

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
6
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
7

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
8
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
An array with arr1/arr2(element-wise) as elements of output array.
0
An array with arr1/arr2(element-wise) as elements of output array.
1
An array with arr1/arr2(element-wise) as elements of output array.
2223
An array with arr1/arr2(element-wise) as elements of output array.
2221
An array with arr1/arr2(element-wise) as elements of output array.
22272722222929

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
1
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
An array with arr1/arr2(element-wise) as elements of output array.
0
An array with arr1/arr2(element-wise) as elements of output array.
1
An array with arr1/arr2(element-wise) as elements of output array.
2222222222222

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
6
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
7

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
0
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
1

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
2
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
9
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
4

arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
4
arr1         :  [2, 27, 2, 21, 23]

Output array : 
 [ 0.66666667  9.          0.66666667  7.          7.66666667]
5
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = 2 * np.ones(3)
>>> x1 / x2
array([[0. , 0.5, 1. ],
       [1.5, 2. , 2.5],
       [3. , 3.5, 4. ]])
29
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)
8

Đầu ra:

arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 0, 5, 6]

Output array :  [ 1.          9.                 inf  4.2         3.83333333]
RuntimeWarning: divide by zero encountered in true_divide
  out = np.power(arr1, arr2)

Tài liệu tham khảo: https: //docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.divide.html.
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.divide.html
.


Làm thế nào để bạn chia một ma trận trong Python?

Trong ma trận Python có thể được triển khai dưới dạng danh sách 2D hoặc mảng 2D ...
Thêm ():- Hàm này được sử dụng để thực hiện bổ sung ma trận khôn ngoan ..
Trừ ():- Hàm này được sử dụng để thực hiện phép trừ Ma trận khôn ngoan ..
chia ():- Hàm này được sử dụng để thực hiện phân chia ma trận yếu tố ..

Làm thế nào để bạn chia một ma trận numpy?

Sử dụng hàm numpy.divide () để chia các phần tử mảng thứ nhất (ARR1) với các phần tử mảng thứ hai (ARR2).Cả ARR1 và ARR2 phải có cùng một hình dạng và phần tử trong ARR2 không được bằng không;Nếu không, nó sẽ gây ra lỗi. divide() function to divided the first array elements (arr1) with the second array elements (arr2). Both arr1 and arr2 must have the same shape and the element in arr2 must not be zero; otherwise, it will raise an error.

Làm thế nào để phân chia Numpy hoạt động?

Phân chia là với hai mảng có cùng kích thước (nghĩa là các mảng có cùng số lượng hàng và cột).Nếu hai mảng đầu vào có cùng hình dạng, thì sự phân chia Numpy sẽ phân chia các phần tử của mảng thứ nhất cho các phần tử của mảng thứ hai, theo kiểu yếu tố khôn ngoan.If the two input arrays have the same shape, then Numpy divide will divide the elements of the first array by the elements of the second array, in an element-wise fashion.

Làm thế nào để bạn chia từng phần tử trong một ma trận cho một số trong Python?

Để phân chia từng phần tử của một mảng cho một hằng số, hãy sử dụng toán tử số học phân chia /.Vượt qua mảng và hằng số dưới dạng toán hạng cho toán tử phân chia như được hiển thị bên dưới.trong đó A là mảng đầu vào và C là một hằng số.B là mảng kết quả.use division arithmetic operator / . Pass array and constant as operands to the division operator as shown below. where a is input array and c is a constant. b is the resultant array.