Write a NumPy program to calculate cumulative product of the elements along a given axis, sum over rows for each of the 3 columns and product over columns for each of the 2 rows of a given 3x3 array

  • برمجة بايثون

Write a NumPy program to calculate cumulative product of the elements along a given axis, sum over rows for each of the 3 columns and product over columns for each of the 2 rows of a given 3x3 array.

الأجوبة

import numpy as np
x = np.array([[1,2,3], [4,5,6]])
print("Original array: ")
print(x)
print("Cumulative product  of the elements along a given axis:")
r = np.cumprod(x)
print(r)
print("\nProduct over rows for each of the 3 columns:")
r = np.cumprod(x,axis=0) 
print(r)
print("\nProduct  over columns for each of the 2 rows:")
r = np.cumprod(x,axis=1) 
print(r)

Sample Output:

Original array: 
[[1 2 3]
 [4 5 6]]
Cumulative product  of the elements along a given axis:
[  1   2   6  24 120 720]

Product over rows for each of the 3 columns:
[[ 1  2  3]
 [ 4 10 18]]

Product  over columns for each of the 2 rows:
[[  1   2   6]
 [  4  20 120]]
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...