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)
Output:
[5 7 9]
Subtraction of Two NumPy Arrays:
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array2 - array1
print(result)
Output:
[3 3 3]
Element-wise Multiplication of Two NumPy Arrays:
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array1 * array2
print(result)
Output:
[ 4 10 18]
Element-wise Division of Two NumPy Arrays:
array1 = np.array([10, 20, 30])
array2 = np.array([2, 5, 3])
result = array1 / array2
print(result)
Output:
[ 5. 4. 10.]
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)
Output:
[[19 22]
[43 50]]
Transpose of a NumPy Array:
array = np.array([[1, 2, 3], [4, 5, 6]])
transposed_array = array.T
print(transposed_array)
Output:
[[1 4]
[2 5]
[3 6]]
Sum of All Elements in a NumPy Array:
array = np.array([1, 2, 3, 4, 5])
result = np.sum(array)
print(result)
Output:
Maximum Value in a NumPy Array:
array = np.array([10, 5, 8, 12, 3])
result = np.max(array)
print(result)
Output:
12
Saving NumPy Array as a CSV File:
array = np.array([1, 2, 3, 4, 5])
np.savetxt('output.csv', array, delimiter=',')
Top comments (0)