Debug School

rakesh kumar
rakesh kumar

Posted on

Explain numpy operation and universal function in django

Addition of Two NumPy Arrays:

import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array1 + array2
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

[5 7 9]
Enter fullscreen mode Exit fullscreen mode

Subtraction of Two NumPy Arrays:

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array2 - array1
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

[3 3 3]
Enter fullscreen mode Exit fullscreen mode

Element-wise Multiplication of Two NumPy Arrays:

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array1 * array2
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

[ 4 10 18]
Enter fullscreen mode Exit fullscreen mode

Element-wise Division of Two NumPy Arrays:

array1 = np.array([10, 20, 30])
array2 = np.array([2, 5, 3])
result = array1 / array2
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

[ 5.  4. 10.]
Enter fullscreen mode Exit fullscreen mode

Matrix Multiplication of Two NumPy Arrays:

array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
result = np.dot(array1, array2)
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

[[19 22]
 [43 50]]
Enter fullscreen mode Exit fullscreen mode

Transpose of a NumPy Array:


array = np.array([[1, 2, 3], [4, 5, 6]])
transposed_array = array.T
print(transposed_array)
Enter fullscreen mode Exit fullscreen mode

Output:

[[1 4]
 [2 5]
 [3 6]]
Enter fullscreen mode Exit fullscreen mode

Sum of All Elements in a NumPy Array:

array = np.array([1, 2, 3, 4, 5])
result = np.sum(array)
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

Maximum Value in a NumPy Array:

array = np.array([10, 5, 8, 12, 3])
result = np.max(array)
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

12
Enter fullscreen mode Exit fullscreen mode

Saving NumPy Array as a CSV File:

array = np.array([1, 2, 3, 4, 5])
np.savetxt('output.csv', array, delimiter=',')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)