Chia đối số yếu tố khôn ngoan.
Parametersx1array_likex1array_likeMảng cổ tức.
x2array_likearray_likeMả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].
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.
Đố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 scalarThươ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: //docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.divide.html.
//docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.divide.html
.