Hướng dẫn can you divide two arrays in python? - bạn có thể chia hai mảng trong python không?

numpy.divide (ARR1, ARR2, OUT = Không, trong đó = true, casting = 'giống nhau_kind', order = 'k', dtype = none) khôn ngoan). Cả ARR1 và ARR2 phải có hình dạng và phần tử giống nhau trong ARR2 không được bằng không; Nếu không, nó sẽ gây ra lỗi.
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.

Thông số :

ARR1: [Array_like] mảng đầu vào hoặc đối tượng hoạt động như cổ tức.arr2: [mảng array_like] mảng đầu vào hoặc đối tượng hoạt động như chia ra. KWARGS: Cho phép bạn truyền từ khóa Độ dài biến của đối số cho một hàm. Nó được sử dụng khi chúng tôi muốn xử lý đối số được đặt tên trong một hàm. Giá trị sai có nghĩa là để lại giá trị trong đầu ra một mình.[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

import

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

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

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
1
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
6
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

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

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
1
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
5
arr1         :  [2, 27, 2, 21, 23]

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

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
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)
9
numpy.divide (arr_A, arr_B)
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)
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
numpy.divide (arr_A, arr_B)
3
numpy.divide (arr_A, arr_B)
4

numpy.divide (arr_A, arr_B)
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
numpy.divide (arr_A, arr_B)
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)
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
# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)
0
# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)
1

Đầ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

import

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

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

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
1
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
6
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

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

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
1
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
5
arr1         :  [2, 27, 2, 21, 23]

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

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
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)
9
numpy.divide (arr_A, arr_B)
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)
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
numpy.divide (arr_A, arr_B)
3
numpy.divide (arr_A, arr_B)
4

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
# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)
0
# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)
1

Đầ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

import

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

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

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
1
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
6
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

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

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
4
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
5
arr1         :  [2, 27, 2, 21, 23]

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

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
1
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
3
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
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)
5
arr1         :  [2, 27, 2, 21, 23]

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

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
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)
9
numpy.divide (arr_A, arr_B)
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)
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
numpy.divide (arr_A, arr_B)
3
numpy.divide (arr_A, arr_B)
4

numpy.divide (arr_A, arr_B)
5
arr1         :  [2, 27, 2, 21, 23]
arr2         :  [2, 3, 4, 5, 6]

Output array : 
 [ 1.          9.          0.5         4.2         3.83333333]
2
numpy.divide (arr_A, arr_B)
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)
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
# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)
0
# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)
1

Đầ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)

& nbsp; Mã 2: Các yếu tố của ARR1 chia cho chia
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.divide.html
.


Hướng dẫn can you divide two arrays in python? - bạn có thể chia hai mảng trong python không?

Bạn có thể chia một mảng cho một mảng khác trong Python không?np.divide() method returns a true division. True division adjusts the output type to present the best answer, regardless of input types.

Phần tử mảng từ mảng thứ nhất được chia cho các phần tử từ phần tử thứ hai (tất cả các phần tử xảy ra khôn ngoan). Cả ARR1 và ARR2 phải có hình dạng và phần tử giống nhau trong ARR2 không được bằng không; Nếu không, nó sẽ gây ra lỗi. Tham số: ARR1: [Array_like] Mảng đầu vào hoặc đối tượng hoạt động như cổ tức.np.divide() is a numpy library function used to perform division amongst the elements of the first array by the elements of the second array. The process of division occurs element-wise between the two arrays. The numpy divide() function takes two arrays as arguments and returns the same size as the input array.

Đối với sự phân chia phần tử khôn ngoan, hình dạng của cả hai mảng cần phải giống nhau. Đó là một thực tế nổi tiếng rằng sự phân chia của Zero là không thể. Vì vậy, các yếu tố trong mảng thứ hai phải là không khác. Nếu không, nó gây ra lỗi.It is a well-known fact that division by zero is not possible. So, the elements in the second array must be non-zero. Otherwise, it raises an error.

Cú pháp

numpy.divide (arr_A, arr_B)

Hàm phân chia () mất tối đa hai tham số chính:

  1. ARR_A: giống như mảng, mảng đầu vào_a, đóng vai trò là cổ tức.: array-like, input array_A, which acts as the dividend.
  2. ARR_B: giống như mảng, mảng đầu vào_b, đóng vai trò là ước số.: array-like, input array_B, which acts as the divisor.

Giá trị trả về

Hàm phân chia () trả về một mảng có cùng kích thước với mảng đầu vào chứa các giá trị thương số khi các phần tử của ARR_A được chia phần tử khôn ngoan cho các phần tử của ARR_B.

Ví dụ lập trình

Chương trình để hiển thị hoạt động của numpy.divide ()

# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
# Shape of first array, arr_A is:
print("Shape of first array, arr_A is: ", arr_A.shape)

# Second Parameter
arr_B = np.array([2, 4, 6, 8])
# Shape of second array, arr_B is:
print("\nShape of second array, arr_B is:", arr_B.shape)

out = np.divide(arr_A, arr_B)
print("\nOutput obtained is: ", out)
# Shape of output array, out is:
print("\nShape of output array, out is: ", out.shape)

Đầu ra

Shape of first array, arr_A is:  (4,)

Shape of second array, arr_B is: (4,)

Output obtained is:  [4.  4.5  4.66666667 4.25]

Shape of output array, out is:  (4,)

Giải trình

Trong chương trình div1.py, chúng tôi đã lấy hai mảng numpy có tên ARR_A và ARR_B bao gồm các phần tử mảng khác nhau; Chúng tôi đã vượt qua hai mảng này dưới dạng các tham số bên trong phương thức np.divide (), tham số đầu tiên ARR_A đóng vai trò cổ tức mà các phần tử cần được chia và ARR_B đóng vai trò là người chia.

Các yếu tố của ARR_A được chia thành thời trang yếu tố theo các yếu tố của ARR_B. Kết quả của một bộ phận được lưu trữ trong một biến được đặt tên, một mảng numpy có hình dạng tương tự như các mảng đầu vào và chứa các giá trị thương số thu được sau khi phân chia.

Lập trình để xem phương thức np.divide () để chia một phần tử mảng có giá trị vô hướng.

Xem mã sau.

# importing the numpy module
import numpy as np

# First Parameter
arr_A = np.array([8, 18, 28, 34])
print("Elements in arr_A are: ", arr_A)

# Second Parameter is a scalar value
scal_val = 6
print("\nScalar value is: ", scal_val)

out = np.divide(arr_A, scal_val)
print("\nOutput obtained is: ", out)

Đầu ra

Elements in arr_A are: [ 8 18 28 34 ]

Scalar value is:  6

Output obtained is: [1.33333333   3.  4.66666667  5.66666667]

Giải trình

Trong chương trình Div2.py, chúng tôi đã lấy một mảng đầu vào không có tên ARR_A, thực hiện chức năng của cổ tức và giá trị vô hướng, tức là, 6, sẽ đóng vai trò là người chia Giá trị Là tham số thứ hai bên trong phương thức np.divide (), giá trị vô hướng sẽ được phát thành một mảng có hình dạng giống như ARR_A. Sau đó, bộ phận được thực hiện yếu tố khôn ngoan như bình thường.

Kết quả của bộ phận được lưu trữ trong một biến được đặt tên, đó là một mảng numpy có hình dạng giống như một mảng đầu vào và chứa các giá trị thương số thu được sau khi phân chia.

Đó là nó cho hướng dẫn này.

Xem thêm

Numpy Bincount ()

Numpy Float đến Int Array

Numpy Random Randn ()

Numpy isposinf ()

Numpy isneginf ()

Bạn có thể chia một mảng cho một mảng khác trong Python không?

Phần tử mảng từ mảng thứ nhất được chia cho các phần tử từ phần tử thứ hai (tất cả các phần tử xảy ra khôn ngoan).Cả ARR1 và ARR2 phải có hình dạng và phần tử giống nhau trong ARR2 không được bằng không;Nếu không, nó sẽ gây ra lỗi.Tham số: ARR1: [Array_like] Mảng đầu vào hoặc đối tượng hoạt động như cổ tức. (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. Parameters : arr1 : [array_like]Input array or object which works as dividend.

Bạn có thể chia hai mả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. (i.e., arrays with exactly the same number of rows and columns). 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.