Viết một chương trình Numpy để tổng hợp và tính toán sản phẩm của một phần tử mảng Numpy. Trình bày bằng hình ảnh: Giải pháp mẫu:-:- Mã Python: Đầu ra mẫu: Trình chỉnh sửa mã Python-Numpy: Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus. Trước: Viết một chương trình Numpy để chuyển đổi dữ liệu thô trong một mảng thành chuỗi nhị phân và sau đó tạo một mảng. Write a NumPy program to convert the raw data in an array to a binary string and then create an array.Numpy: ARRAY Đối tượng Bài tập-99 với giải pháp
import numpy as np
x = np.array[[10, 20, 30], float]
print["Original array:"]
print[x]
print["Sum of the array elements:"]
print[x.sum[]]
print["Product of the array elements:"]
print[x.prod[]]
Original array:
[ 10. 20. 30.]
Sum of the array elements:
60.0
Product of the array elements:
6000.0
Next: Write a NumPy program to take values from a source array and put them at specified indices of another array.
Mức độ khó của bài tập này là gì?
Kiểm tra kỹ năng lập trình của bạn với bài kiểm tra của W3Resource.
Python: Lời khuyên trong ngày
Cấu trúc Dict được đặt hàng [một lớp từ điển giữ thứ tự]:
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []
- Bài tập: Top 16 chủ đề phổ biến nhất hàng tuần
- Bài tập SQL, Thực hành, Giải pháp - Tham gia
- Bài tập SQL, Thực hành, Giải pháp - Quan sát phụ
- JavaScript Basic - Bài tập, Thực hành, Giải pháp
- Java Array: Bài tập, Thực hành, Giải pháp
- C Bài tập lập trình, Thực hành, Giải pháp: Tuyên bố có điều kiện
- Cơ sở dữ liệu nhân sự - Sắp xếp bộ lọc: Bài tập, Thực hành, Giải pháp
- C Bài tập lập trình, Thực hành, Giải pháp: Chuỗi
- Các loại dữ liệu Python: Từ điển - Bài tập, Thực hành, Giải pháp
- Câu đố lập trình Python - Bài tập, Thực hành, Giải pháp
- Mảng C ++: Bài tập, Thực hành, Giải pháp
- Báo cáo và vòng lặp có điều kiện JavaScript - Bài tập, Thực hành, Giải pháp
- Thuật toán cơ bản C# Sharp: Bài tập, Thực hành, Giải pháp
- Python Lambda - Bài tập, Thực hành, Giải pháp
- Python Pandas DataFrame: Bài tập, Thực hành, Giải pháp
- Công cụ chuyển đổi
- JavaScript: HTML Mẫu xác thực
Phương pháp 2: Sử dụng tổng chức năng tích hợp []. Python cung cấp một tổng số hàm sẵn [] tổng hợp các số trong danh sách. Có thể lặp lại: Có thể là bất cứ thứ gì danh sách, bộ dữ liệu hoặc từ điển, nhưng quan trọng nhất là nó nên được đánh số.
Làm cách nào để tổng hợp hai mảng numpy?
Để thêm hai mảng với nhau, chúng tôi sẽ sử dụng phương thức Numpy.ADD [ARR1, ARR2]. Để sử dụng phương pháp này, bạn phải đảm bảo rằng hai mảng có cùng chiều dài. Nếu độ dài của hai mảng không giống nhau, thì hãy phát kích thước của mảng ngắn hơn bằng cách thêm số 0 tại các chỉ mục bổ sung.
Bạn nhận được gì nếu bạn áp dụng Numpy Sum [] vào danh sách chỉ chứa các giá trị Boolean?numpy.sum[]
Sum nhận được một mảng booleans làm đối số của nó, nó sẽ tổng hợp từng phần tử [đếm đúng là 1 và sai là 0] và trả về kết quả. Ví dụ NP. SUM [[Đúng, Đúng, Sai]] sẽ xuất hiện 2 :] Hy vọng điều này sẽ giúp.numpy.sum[array_name, axis=None, dtype=None, out=None, keepdims=, initial=, where=]
Example:
Python3
import
numpy as np
def
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.00
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.04
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.07
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1____22221
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []4
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []6
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1__28____21__21
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]4
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
‘
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]5
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]7
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]9
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]9
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]1
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
import
1Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
import
4Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
import
8Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]9
numpy as np
7Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
numpy as np
9Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
numpy as np
7Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
numpy as np
9Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.005
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.011
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
numpy as np
9Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.018
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.024
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.026
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.031
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]7
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]8
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.040
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.042
numpy as np
9>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.045
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.045
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.048
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.049
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.050
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.053
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.054
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.056
Output:
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]
Lưu ý: Sử dụng numpy.sum trên các phần tử mảng bao gồm không phải là một số [nans] các phần tử gây ra lỗi, để tránh điều này, chúng tôi sử dụng numpy.nansum [] Các tham số tương tự như cái trước ngoại trừ cái sau không hỗ trợ ở đâu và ban đầu.using numpy.sum on array elements consisting Not a Number [NaNs] elements gives an error, To avoid this we use numpy.nansum[] the parameters are similar to the former except the latter doesn’t support where and initial.
Phương pháp số 2: Sử dụng numpy.cumsum []numpy.cumsum[]
Trả về tổng tích lũy của các phần tử trong mảng đã cho.
Cú pháp: numpy.cumsum [mảng_name, axis = none, dtype = none, out = none]numpy.cumsum[array_name, axis=None, dtype=None, out=None]
Example:
Python3
import
numpy as np
def
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.00
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.04
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.07
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1____22221
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []4
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []6
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1__28____21__21
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.085
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]4
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.092
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.095
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]7
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03____21
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03__
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []16
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []21
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
import
4Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.049
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.050
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.053
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.054
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.056
Output:
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]
Sản phẩm của mảng numpy
Sản phẩm của các mảng numpy có thể đạt được theo các cách sau & nbsp;
Phương thức số 1: & nbsp; sử dụng numpy.prod []numpy.prod[]
Cú pháp: numpy.prod [Array_Name, Axis = none, dtype = none, out = none, netchims =, ban đầu =, trong đó =]numpy.prod[array_name, axis=None, dtype=None, out=None, keepdims=, initial=, where=]
Example:
Python3
import
numpy as np
def
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.00
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.04
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.07
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1____22221
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []4
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []6
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1__28____21__21
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]4
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]7
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03____21
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03__
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []63
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]9
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []75
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
import
4Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []63
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
import
1Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
import
4Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
import
8Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []63
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]9
numpy as np
7Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
numpy as np
9Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []63
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
numpy as np
7Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
numpy as np
9Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.005
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]23
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
numpy as np
9Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.018
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]34
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]36
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.031
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []63
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.040
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.042
numpy as np
9>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.045
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.045
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.048
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.049
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.050
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.053
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.054
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.056
Output:
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]
Sản phẩm của mảng numpynumpy.cumprod[]
Sản phẩm của các mảng numpy có thể đạt được theo các cách sau & nbsp;
Phương thức số 1: & nbsp; sử dụng numpy.prod []numpy.cumsum[array_name, axis=None, dtype=None, out=None]axis = [integer,Optional]
Python3
import
numpy as np
def
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.00
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.04
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.07
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1____22221
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []4
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []6
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1__28____21__21
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.085
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.05
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array [[1 2 3] [4 5 6]] [ 6 15] [5 7 9] 21 [21] [21] with axis parameter [[5 7 9]] [[ 6] [15]] without axis parameter [[21]] using initial parameter in sum function 121 using where parameter [5 0 0]4
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]00
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]03
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]2
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Initialised array [[1 2 3] [4 5 6]] [ 6 120] [ 4 10 18] 720 [720] [720] with axis parameter [[ 4 10 18]] [[ 6] [120]] without axis parameter [[720]] using initial parameter in sum function 7200 using where parameter [4 1 1]7
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.09
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []1
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03____21
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []5
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []03__
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Initialised array original array [[1 2 3] [4 5 6]] [ 1 3 6 10 15 21] [[ 1 3 6] [ 4 9 15]] [[1 3 6] [4 9 15]]24
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []0
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]9
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
>>> import collections >>> m = collections.defaultdict[int] >>> m['a'] 0 >>> m = collections.defaultdict[str] >>> m['a'] '' >>> m['b'] += 'a' >>> m['b'] 'a' >>> m = collections.defaultdict[lambda: '[default value]'] >>> m['a'] '[default value]' >>> m['b'] '[default value]' >>> m = collections.defaultdict[list] >>> m['a'] []21
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.02
import
4Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.049
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.050
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.08
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.053
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.054
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.01
Original array: [ 10. 20. 30.] Sum of the array elements: 60.0 Product of the array elements: 6000.056
Output:
Initialised array original array [[1 2 3] [4 5 6]] [ 1 2 6 24 120 720] [[ 1 2 6] [ 4 20 120]] [[1 2 6] [4 20 120]]